We just saw that the equals method of set has no more methods from collection to set, but there are more restrictions, for example, the elements in the set cannot be repeated (neither E1 nor E2 belong to a set, and e1.equals (E2), and then there is a limit on the equals method, two set equals returns true if and only if they have the same elements (that is, for E1 in any set1, there must be an E2 in set2, e1.equals (E2, at the same time, for any E2 in set2, the values E1 and e2.equals (E1) in set1 return true. Here I do not consider the case where some sets allow null.) An example is as follows:
Import Java. util. arrays; import Java. util. collection; import Java. util. hashset; import Java. util. linkedhashset; import Java. util. treeset; public class test {public static void main (string [] ARGs) {string [] SA = {"ABC", "spam", "ham", "egg ", "dog"}; collection <string> cs = arrays. aslist (SA); hashset <string> set1 = new hashset <string> (CS); treeset <string> set2 = new treeset <string> (CS ); linkedhashset <string> set3 = new linkedhashset <string> (CS); system. out. println (set1.equals (set2); system. out. println (set1.equals (set3); // set1.add (null); // set2.add (null); // set3.add (null); system. out. println (set2.equals (set3 ));}}
Some sets allow up to one null (such as hashset and javashashset), and some do not allow null (such as treeset. (Null is different from null)