Java string array intersection and set difference set to repeat and set

Source: Internet
Author: User
Tags addall

System method

Package com;

Import java.util.ArrayList;
Import Java.util.Iterator;
Import java.util.List;

public class Test {

public static voidMain (String[] (args) {
List list1 =new ArrayList ();
List1.add ("1111");
List1.add ("2222");
List1.add ("3333");

List list2 =new ArrayList ();
List2.add ("3333");
List2.add ("4444");
List2.add ("5555");

and set
List1.addall (LIST2);
Intersection
List1.retainall (LIST2);
Subtraction
List1.removeall (LIST2);
No duplicate and set
List2.removeall (List1);
List1.addall (LIST2);

Iterator<string> It=list1.iterator ();
while (It.hasnext ()) {
System.out.println (It.next ());

}

System.out.println ("-----------------------------------\ n");
Printstr (List1);

}

public static void Printstr (List list1) {
for (int i = 0; i < list1.size (); i++) {
System.out.println (List1.get (i));
}
}
}

Self simulation

packagecom.array;     importjava.util.ArrayList;  importjava.util.HashMap;  importjava.util.HashSet;   import java.util.Iterator;  importjava.util.List;   importjava.util.Map;   importjava.util.Set;  import java.util.Map.Entry;     publicclassStringArray  {         publicstatic voidmain(String[] args)      {          // 测试union  String[] arr1 = { "abc""df""abcd" };          String[] arr2 = { "abc""cc""df""d""abc"};          String[] result_union = union(arr1, arr2);          System.out.println("求并集的结果如下:");          for(String str : result_union)          {              System.out.println(str);          }          System.out.println("---------------------可爱的分割线------------------------");          // 测试insect  String[] result_insect = intersect(arr1, arr2);          System.out.println("求交集的结果如下:");          for(String str : result_insect)          {              System.out.println(str);          }          System.out.println("---------------------可爱的分割线------------------------");      }         // 求两个字符串数组的并集,利用set的元素唯一性   publicstaticString[] union(String[] arr1, String[] arr2)      {          Set<String> set = newHashSet<String>();          for(String str : arr1)          {              set.add(str);          }          for(String str : arr2)          {              set.add(str);          }          String[] result = {};          returnset.toArray(result);      }         // 求两个数组的交集   publicstaticString[] intersect(String[] arr1, String[] arr2)      {          Map<String, Boolean> map = newHashMap<String, Boolean>();          List<String> list = newArrayList<String>();          for(String str : arr1)          {              if(!map.containsKey(str))              {                  map.put(str, Boolean.FALSE);              }          }          for(String str : arr2)          {              if(map.containsKey(str))              {                  map.put(str, Boolean.TRUE);              }          }              for(Iterator<Entry<String, Boolean>> it = map.entrySet().iterator();it.hasNext();)          {              Entry<String,Boolean> e = (Entry<String,Boolean>)it.next();              if(e.getValue().equals(Boolean.TRUE))              {                  list.add(e.getKey());              }          }          returnlist.toArray(newString[] {});      }         }

Java string array intersection and set difference set to repeat and set

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.