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

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay