Java to re-merge List__java

Source: Internet
Author: User

Before writing the C # project, it is convenient to redo the list, using List1. Union (List2). ToList () can (enumerable.union). And, if you want to remove the List2 item from the List1, it is also convenient, list2. Except (List1). ToList ()

Now write Java project, it does not have such a native method can be directly invoked, operation, a little more than a few steps.

    public static void Main (string[] args) {list<string> lista = arrays.aslist ("Unicorn", "Penguin", "Koala", "
        Hippo "," unicorn ");
        list<string> Listb = arrays.aslist ("Swan", "Penguin", "Ape");
        The Merge lists and remove duplicated entries set<string> Set = new linkedhashset<> ();
        Set.addall (Lista);
        Set.addall (LISTB);
        System.out.println ("Merge and remove Duplicates:" + set);
        Intersection list<string> intersection = new arraylist<> (lista);
        Intersection.retainall (LISTB);
        System.out.println ("Intersection of two lists:" + intersection);
        Concat list<string> Concat = new arraylist<> (lista);
        Concat.addall (LISTB);
        System.out.println ("Concat of two lists:" + Concat);
        Lista-listb list<string> minus = new arraylist<> (lista);
        Minus.removeall (LISTB); System.out.println ("Remove items that" aRe in Listb from Lista: "+ minus"); }

Program output:
Merge and remove duplicates: [Unicorn, Penguin, Koala, Hippo, Swan, Ape]
intersection of two lists: [Pe Nguin]
Concat of two lists: [Unicorn, Penguin, Koala, Hippo, Unicorn, Swan, Penguin, Ape]
Remove items that are In Listb from Lista: [Unicorn, Koala, Hippo, Unicorn]

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.