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 infoAdvertise with us Next Article Initializing HashSet in Java N Nikita Tiwari Improve Article Tags : Java Java-Collections java-hashset Java-Set-Programs Practice Tags : JavaJava-Collections Similar Reads Initialize HashMap in Java HashMap in Java is a part of the java.util package and allows storing key-value pairs. Initializing a HashMap can be done in multiple ways, including static blocks, utility methods from the Collections class, and modern approaches provided by Java 8 and Java 9. This article will guide you through th 4 min read Double Brace Initialization in Java The combination of two separate processes in Java is known as Double Brace Initialization in Java. As the name suggests, there are two braces {{ included in it. A single brace { is nothing new for programmers. The first brace in the double brace initialization is used to create an anonymous inner cl 4 min read Internal working of Set/HashSet in Java As we know that a set is a well-defined collection of distinct objects. Each member of a set is called an element of the set. So in other words, we can say that a set will never contain duplicate elements. But how in java Set interface implemented classes like HashSet, LinkedHashSet, TreeSet etc. ac 3 min read Hashtable in Java Hashtable class, introduced as part of the Java Collections framework, implements a hash table that maps keys to values. Any non-null object can be used as a key or as a value. To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method an 12 min read Java HashSet HashSet in Java implements the Set interface of Collections Framework. It is used to store the unique elements and it doesn't maintain any specific order of elements. Can store the Null values.Uses HashMap (implementation of hash table data structure) internally.Also implements Serializable and Clon 12 min read Like