Initializing HashSet in Java Last Updated : 17 Mar, 2022 Comments Improve Suggest changes Like Article Like Report Set in Java is an interface which extends Collection. It is an unordered collection of objects in which duplicate values cannot be stored. Basically, set is implemented by HashSet, LinkedHashSet or TreeSet (sorted representation). Set has various methods to add, remove clear, size, etc to enhance the usage of this interface.Method 1: Using Constructor: In this method first we create an array then convert it to a list and then pass it to the HashSet constructor that accepts another collection. Integer elements of the set are printed in sorted order. Java // Java code for initializing a Set import java.util.*; public class Set_example { public static void main(String[] args) { // creating and initializing an array (of non // primitive type) Integer arr[] = { 5, 6, 7, 8, 1, 2, 3, 4, 3 }; // Set demonstration using HashSet Constructor Set<Integer> set = new HashSet<>(Arrays.asList(arr)); // Duplicate elements are not printed in a set. System.out.println(set); } } Method 2 using Collections: Collections class consists of several methods that operate on collections. a) Collection.addAll() : adds all the specified elements to the specified collection of the specified type. b) Collections.unmodifiableSet() : adds the elements and returns an unmodifiable view of the specified set. Java // Java code for initializing a Set import java.util.*; public class Set_example { public static void main(String[] args) { // creating and initializing an array (of non // primitive type) Integer arr[] = { 5, 6, 7, 8, 1, 2, 3, 4, 3 }; // Set demonstration using Collections.addAll Set<Integer> set = Collections.<Integer> emptySet(); Collections.addAll(set = new HashSet<Integer>(Arrays.asList(arr))); // initializing set using Collections.unmodifiable set Set<Integer> set2 = Collections.unmodifiableSet(new HashSet<Integer> (Arrays.asList(arr))); // Duplicate elements are not printed in a set. System.out.println(set); } } Method 3: using .add() each time Create a set and using .add() method we add the elements into the set Java // Java code for initializing a Set import java.util.*; public class Set_example { public static void main(String[] args) { // Create a set Set<Integer> set = new HashSet<Integer>(); // Add some elements to the set set.add(1); set.add(2); set.add(3); set.add(4); set.add(5); set.add(6); set.add(7); set.add(8); // Adding a duplicate element has no effect set.add(3); System.out.println(set); } } Output: [1, 2, 3, 4, 5, 6, 7, 8] Comment More info N Nikita tiwari Improve Article Tags : Java Java-Collections java-hashset Java-Set-Programs Explore Java BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOP & InterfacesClasses and Objects in Java10 min readAccess Modifiers in Java6 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking15+ min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like