DEV Community

TheCodeAlchemist
TheCodeAlchemist

Posted on

Java HashSet Cheat sheet

Demo — https://youtu.be/2tLxcaopKR4

public class SetDemo {
    public static void main(String[] args) {
        //Initializing a new HashSet
        HashSet<String> mySet = new HashSet<>();

        //Adding values to set
        mySet.add("One");
        mySet.add("Two");
        mySet.add("Two");
        mySet.add("Three");

        //Printing set
        System.out.println(mySet);

        //Adding values in Bulk
        //Add values from myList to mySet maintaing unique constraints
        ArrayList<String> myList = new ArrayList<>();
        myList.add("Four");
        myList.add("Five");
        myList.add("Three");

        mySet.addAll(myList);
        System.out.println(mySet);

        //Accessing values using for-each loop
        System.out.println("=====for-each========");
        for(String element : mySet) {
            System.out.println(element);
        }

        //Accessing values using iterator
        System.out.println("=====Iterator========");
        Iterator<String> it = mySet.iterator();
        while(it.hasNext()) {
            System.out.println(it.next());
        }

        //Accessing values using forEach
        System.out.println("Using forEach and Consumer========");
        mySet.forEach(System.out::println);

        //Clearing
        //mySet.clear();

        //Check size
        System.out.println(mySet.size() + ", " + mySet.isEmpty());

        //Contains check -- constant time performance
        System.out.println(mySet.contains("Two"));

        //Removing elements
        mySet.remove("Three");
        System.out.println(mySet);

        //Retain elements
        ArrayList<String> keepThese = new ArrayList<>();
        keepThese.add("Four");
        keepThese.add("Two");

        mySet.retainAll(keepThese);
        System.out.println(mySet);

        //Converting set to Array
        Object[] arr = mySet.toArray();
        String[] strArray = mySet.toArray(new String[0]);

        //Using Stream
        List<String> fromSet = mySet.stream().collect(Collectors.toList());
        System.out.println(fromSet);

        //----------------------------------------------------------------
        //----------------- LinkedHashSet --------------------------------
        //----------------------------------------------------------------

        //Initializing LinkedHashSet
        System.out.println("=========LinkedHashSet==========");
        LinkedHashSet<String> lSet = new LinkedHashSet<>();

        lSet.add("One");
        lSet.add("Two");
        lSet.add("Four");
        lSet.add("Three");
        lSet.add("Three");

        System.out.println(lSet);

        for (String element : lSet) {
            System.out.println(element);
        }
    }


}
Enter fullscreen mode Exit fullscreen mode
👋 Kindness is contagious

Please leave your appreciation by commenting on this post!

It takes one minute and is worth it for your career.

Get started

Thank you!

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay