Java set intersection, Difference set, and set __ "Java" Java collection

Source: Internet
Author: User
Tags addall
/** * Created by Yuhui on 2017/7/11 0011.
* * Import Java.util.HashSet;

Import Java.util.Set; public class Testset {public static void main (string[] args) {set<string> result = new Hashset<st
        Ring> ();
                set<string> Set1 = new Hashset<string> () {Add ("Glory of Kings");
                Add ("League of Heroes");
                Add ("Through the FireWire");
            Add ("Dungeon and Warrior");

        }   
        };
                set<string> Set2 = new Hashset<string> () {Add ("Glory of Kings");
                Add ("Dungeon and Warrior");
            Add ("World of Warcraft");

        }
        };
        Result.clear ();
        Result.addall (SET1);
        Result.retainall (Set2);

        System.out.println ("intersection:" + result);
        Result.clear ();
        Result.addall (SET1);
        Result.removeall (Set2);

        SYSTEM.OUT.PRINTLN ("Difference set:" + result);
        Result.clear ();
        Result.addall (SET1);
        Result.addall (Set2); SystEm.out.println ("and set:" + result);
 }

}

The results are as follows:

Intersection: [King Glory, Dungeon and Warrior]
Difference set: [League of Heroes, through the FireWire]
and set: [King Glory, League of Heroes, World of Warcraft, Dungeons and Warriors, through the FireWire]

If you like the blog I wrote, after reading the harvest is very big, may wish to small sponsor me, let me have the power to continue to write High-quality blog, thank you for your appreciation ...

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.