Equals method in Java Collection framwork

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.