Using Java to find the intersection and the assembly of a string array

Source: Internet
Author: User

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

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.