Java – HashSet contains not applicable to integer

I don't understand why inclusion doesn't work (in fact, if I have passed the custom class, I can revisit my hascode and equals methods, but this is integer) So instead of including what I can use? Please help.

Set<Integer> st = new HashSet<>();
st.add(12);
Set<Integer> st1 = new HashSet<>();
st1.add(12);
System.out.println(st.contains(st1));

Solution

St. contains (ST1) returns false because the type of ST1 (set < integer >) is different from the element type (integer) in St

However, you can use the set#containsall (collection ) method:

System.out.println(st.containsAll(st1));

This checks whether the element in ST1 exists in St

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>