Compare whether two lists are equal, the length and content are equal, and the List Length

Source: Internet
Author: User

Compare whether two lists are equal, the length and content are equal, and the List Length

/**
* Duplicate data exists in the list, and the order is different.
*/
Import java. util. arrayList; import java. util. collections; import java. util. list; public class CompareList {public static void main (String [] args) {List <String> list1 = new ArrayList <String> (); list <String> list2 = new ArrayList <String> (); list1.add ("zhang"); list1.add ("li"); list1.add ("zhangs "); list1.add ("zhangs"); list2.add ("li"); list2.add ("zhangs"); list2.add ("zhang"); list2.add ("zhangs "); // list for sorting Collections. sort (list1); Collections. sort (list2 );
// Output the comparison result System. out. println (list1.equals (list2 ));}}

Second:

/**
* Duplicate data exists in the list, and the order is different.
*/
Import java. util. arrayList; import java. util. comparator; import java. util. list; public class CompareList {public static void main (String [] args) {List <String> list1 = new ArrayList <String> (); list <String> list2 = new ArrayList <String> (); list1.add ("zhang"); list1.add ("li"); list1.add ("zhangs "); list1.add ("zhangs"); list2.add ("li"); list2.add ("zhangs"); list2.add ("zhangs"); list2.add ("zhang ");
List1.sort (new Comparator <String> () {@ Override public int compare (String o1, String o2) {return o1.compareTo (o2 );}}); list2.sort (new Comparator <String> () {@ Override public int compare (String o1, String o2) {return o1.compareTo (o2) ;}}); if (list1.size () = list2.size () & list1.containsAll (list2) {System. out. println ("true ");}}}

 

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.