Java and intersection operations

Source: Internet
Author: User

In the process of the interview, forget the list can also do orthogonal set operation, this is also a common data problem ah, this is also a common data structure problem---collection, the process of interview has not thought of this data structure

An operation in Java that has integrated and intersected the API.

Code: (from http://pengyan5945.iteye.com/blog/1311597)

Intersection API: Use Retainall to

The API of the RemoveAll: first and then AddAll

Importjava.util.ArrayList;ImportJava.util.HashSet;Importjava.util.List;ImportJava.util.Set; Public classListtest { Public Static voidMain (string[] args) {testintersection ();         TestUnion ();    Testintersectionset (); }    Private Static voidtestintersection () {List<String> List1 =NewArraylist<string>(); List<String> List2 =NewArraylist<string>(); List1.add ("ABC"); List2.add ("abc")); List1.add ("123"); List2.add ("123"); List1.add ("ABC"); List2.add ("XYZ");         List1.removeall (LIST2);         List1.addall (LIST2); System.out.println ("The number of elements in the set is:" +list1.size ()); }    Private Static voidTestintersectionset () {List<String> List1 =NewArraylist<string>(); List<String> List2 =NewArraylist<string>(); List1.add ("ABC"); List2.add ("abc")); List1.add ("123"); List2.add ("123"); List1.add ("ABC"); List2.add ("XYZ"); Set<String> set =NewHashset<string>();         Addlist2set (set, List1);         Addlist2set (set, List2); System.out.println ("The number of elements in the set is:" +set.size ()); }    Private Static voidtestunion () {List<String> List1 =NewArraylist<string>(); List<String> List2 =NewArraylist<string>(); List1.add ("ABC"); List2.add ("abc")); List1.add ("123"); List2.add ("123"); List1.add ("ABC"); List2.add ("XYZ");         List1.retainall (LIST2); System.out.println ("The number of intersection elements is:" +list1.size ()); }    Private Static voidAddlist2set (set<string> set,list<string>list) {         for(String str:list) {set.add (str); }    }}
View Code

Java and intersection operations

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.