检查Java中两个HashSet是否相等
首先,创建第一个HashSet并向其中添加元素-
//创建哈希集1-
HashSet hs1 = new HashSet();
hs1.add("G");
hs1.add("H");
hs1.add("I");
hs1.add("J");
hs1.add("K");创建第二个HashSet并向其中添加元素-
//创建哈希集2-
HashSet hs2 = new HashSet();
hs2.add("G");
hs2.add("H");
hs2.add("I");
hs2.add("J");
hs2.add("K");要检查两个HashSet是否相等,请使用equals()方法-
hs1.equals(hs2)
以下是检查两个HashSet是否相等的示例-
示例
import java.util.*;
public class Demo {
public static void main(String args[]) {
//创建哈希集1-
HashSet hs1 = new HashSet();
hs1.add("G");
hs1.add("H");
hs1.add("I");
hs1.add("J");
hs1.add("K");
System.out.println("Elements in set 1 = "+hs1);
//创建哈希集2-
HashSet hs2 = new HashSet();
hs2.add("G");
hs2.add("H");
hs2.add("I");
hs2.add("J");
hs2.add("K");
System.out.println("Elements in set 2 = "+hs2);
System.out.println("Are both the HashSet equal? "+hs1.equals(hs2));
}
}输出结果
Elements in set 1 = [G, H, I, J, K] Elements in set 2 = [G, H, I, J, K] Are both the HashSet equal? True
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短