1 Packagestring; 2 3 ImportJava.util.HashMap; 4 ImportJava.util.HashSet; 5 Importjava.util.LinkedList; 6 ImportJava.util.Map; 7 ImportJava.util.Map.Entry; 8 ImportJava.util.Set; 9 Ten Public classStringarray { One Public Static voidMain (string[] args) { A //Test Union -String[] arr1 = {"abc", "DF", "ABC"}; -String[] arr2 = {"abc", "CC", "DF", "D", "ABC"}; thestring[] Result_union =Union (ARR1, ARR2); -SYSTEM.OUT.PRINTLN ("The result of the set is as follows:"); - for(String str:result_union) { - System.out.println (str); + } -System.out.println ("---------------------cute split-line------------------------"); + A //Test Insect atstring[] Result_insect =intersect (arr1, ARR2); -SYSTEM.OUT.PRINTLN ("The result of the intersection is as follows:"); - for(String str:result_insect) { - System.out.println (str); - } - inSystem.out.println ("---------------------Crazy split Line------------------------"); - //Test minus tostring[] Result_minus =minus (arr1, arr2); +SYSTEM.OUT.PRINTLN ("The result of the differential set is as follows:"); - for(String str:result_minus) { the System.out.println (str); * } $ } Panax Notoginseng - //A set of two string arrays, using the element uniqueness of Set the Public Staticstring[] Union (string[] arr1, string[] arr2) { +Set<string> set =NewHashset<string>(); A for(String str:arr1) { the Set.add (str); + } - for(String str:arr2) { $ Set.add (str); $ } -String[] result = {}; - returnSet.toarray (Result); the } - Wuyi //find the intersection of two arrays the Public Staticstring[] intersect (string[] arr1, string[] arr2) { -map<string, boolean> map =NewHashmap<string, boolean>(); Wulinkedlist<string> list =NewLinkedlist<string>(); - for(String str:arr1) { About if(!Map.containskey (str)) { $ map.put (str, boolean.false); - } - } - for(String str:arr2) { A if(Map.containskey (str)) { + map.put (str, boolean.true); the } - } $ the for(Entry<string, boolean>E:map.entryset ()) { the if(E.getvalue (). Equals (Boolean.true)) { the List.add (E.getkey ()); the } - } in theString[] result = {}; the returnList.toarray (Result); About } the the //Find the difference set of two arrays the Public Staticstring[] Minus (string[] arr1, string[] arr2) { +linkedlist<string> list =NewLinkedlist<string>(); -Linkedlist<string> history =NewLinkedlist<string>(); thestring[] Longerarr =arr1; Bayistring[] Shorterarr =arr2; the //find a longer array to reduce the short array the if(Arr1.length >arr2.length) { -Longerarr =arr2; -Shorterarr =arr1; the } the for(String str:longerarr) { the if(!list.contains (str)) { the List.add (str); - } the } the for(String str:shorterarr) { the if(List.contains (str)) {94 History.add (str); the list.remove (str); the}Else { the if(!history.contains (str)) { 98 List.add (str); About } - } 101 } 102 103String[] result = {}; 104 returnList.toarray (Result); the } 106}
program run, print output:
The results of the request for the Assembly are as follows:
D
Abc
Df
Cc
---------------------Cute Split-line------------------------
The result of the intersection is as follows:
Abc
Df
---------------------Crazy Split-line------------------------
The results of the differential set are as follows:
Cc
D
Original quote: http://blog.sina.com.cn/s/blog_4934a04a0100fqmf.html
Java string array intersection, set, and difference set