Package String;import Java.util.hashmap;import java.util.hashset;import java.util.linkedlist;import java.util.Map; Import Java.util.map.entry;import Java.util.set;public class Stringarray {public static void main (string[] args) { Test Union string[] arr1 = {"abc", "DF", "abc"}; String[] arr2 = {"abc", "CC", "DF", "D", "abc"}; string[] 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------------------------"); Test insect string[] 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); } System.out.println ("---------------------Crazy split Line------------------------"); Test minus string[] Result_minus = Minus (arr1, arr2); System.out.println ("The results of the differential set are as follows:"); for (String Str:result_minus) {System.out.println (str); }}//Ask for a union of two string arrays, using the element uniqueness of set public static string[] Union (string[] arr1, string[] arr2) {SET<STRING&G T set = new Hashset<string> (); for (String str:arr1) {set.add (str); } for (String str:arr2) {set.add (str); } string[] result = {}; return Set.toarray (Result); }//ask for the intersection of two arrays public static string[] intersect (string[] arr1, string[] arr2) {map<string, boolean> Map = new hashmap<string, boolean> (); linkedlist<string> list = new linkedlist<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.tru E); }} for (entry<string, boolean> e:Map.entryset ()) {if (E.getvalue (). Equals (Boolean.true)) {List.add (E.getkey ()); }} string[] result = {}; return List.toarray (Result); }//Ask for the difference set of two arrays public static string[] minus (string[] arr1, string[] arr2) {linkedlist<string> list = new Linkedlist<string> (); linkedlist<string> history = new linkedlist<string> (); string[] Longerarr = arr1; string[] Shorterarr = arr2; Find a longer array to reduce the short array if (Arr1.length > Arr2.length) {longerarr = arr2; Shorterarr = arr1; } for (String Str:longerarr) {if (!list.contains (str)) {list.add (str); }} for (String Str:shorterarr) {if (List.contains (str)) {history.add (str); List.remove (str); } else {if (!history.contains (str)) {list.add (str); }}} string[] result = {}; return List.toarray (Result); }}
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
Java Basics Review (16)-string array intersection, set and difference set