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
package
com.array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet; import java.util.Iterator;
import java.util.List; import java.util.Map; import java.util.Set;
import java.util.Map.Entry;
public class
StringArray
{
public static void
main(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的元素唯一性 public
static
String[] union(String[] arr1, String[] arr2)
{ Set<String> set = new HashSet<String>();
for (String str : arr1)
{ set.add(str); } for (String str : arr2)
{ set.add(str); } String[] result = {}; return set.toArray(result);
}
// 求两个数组的交集 public
static
String[] intersect(String[] arr1, String[] arr2)
{ Map<String, Boolean> map = new HashMap<String, Boolean>();
List<String> list = new ArrayList<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()); } } return list.toArray( new String[] {});
}
}
|
Java string array intersection and set difference set to repeat and set