Java Basics Review (16)-string array intersection, set and difference set

Source: Internet
Author: User



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&LT;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

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.