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) { // Test union string[] arr1 = { "ABC" , "DF", "ABCD" }; string[] arr2 = { "abc", "CC", "DF", "D", "abc" }; &nBsp String[] result_union = union (ARR1,&NBSP;ARR2); &NBSP;&NBSP;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,&NBSP;ARR2); &NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("The result of the intersection is as follows:"); for (StRing str : result_insect) { system.out.println (str); } system.out.println ("---------------------Cute split Line------------------------"); } // two a set of arrays of strings, using the element uniqueness of Set public static string[] union (STRING[]&NBSP;ARR1,&NBSP;STRING[]&NBSP;ARR2) { Set<String> set = new HashSet<String> (); for (STRING&NBSP;STR&NBSP;:&NBSP;ARR1) { set.add (str); } for (string str &NBSP;:&NBSP;ARR2) { set.add (str); } string[] result = {}; return set.toarray (Result); } // find the intersection of two arrays public static string[] intersect (string[] arr1, string[] &NBSP;ARR2) { Map<string, boolean&gT; map = new hashmap<string, boolean> (); List<String> list = new ArrayList<String> (); for (STRING&NBSP;STR&NBSP;:&NBSP;ARR1) { if (!map.containskey (str)) { map.put (Str, boolean.false); } } for (STRING&NBSP;STR&NBSP;:&NBSP;ARR2) &nbSp; { 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[] {}); } }
Printing results:
The results of the request for the Assembly are as follows:
D
Abc
Df
Abcd
Cc
---------------------Cute Split-line------------------------
The result of the intersection is as follows:
Abc
Df
---------------------Cute Split-line------------------------
Using Java to find the intersection and the assembly of a string array