English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Collection objects in Java are objects that store references to other objects. The java.util package provides classes and interfaces for collections. There are four main collection interfaces, namely Collection, List, Queue, and Map.
Set - A set object is a collection that stores a group of elements, it grows dynamically, and does not allow duplicate elements.
HashSet and LinkedHashSet are classes that implement the Set interface. You can create a Set object by implementing one of these classes.
import java.util.HashSet; public class SetExample { public static void main(String args[]) { //Instantiate HashSet HashSet<String> hashSet = new HashSet<String>(); //Fill the HashSet hashSet.add("Mango"); hashSet.add("Apple"); hashSet.add("Cherries"); hashSet.add("Banana"); System.out.println(hashSet); } }
Output Result
[Apple, Mango, Cherries, Banana]
You can convert collection objects to arrays in many ways-
Add each element-You can use a foreach loop to add each element of the Set object to the array.
import java.util.HashSet; import java.util.Set; public class SetExample { public static void main(String args[]) { //Instantiate HashSet Set<Integer> hashSet = new HashSet<Integer>(); //Fill the HashSet hashSet.add(1124); hashSet.add(3654); hashSet.add(7854); hashSet.add(9945); System.out.println(hashSet); //Create an empty integer array Integer[] array = new Integer[hashSet.size()]; //Convert collection object to integer array int j = 0; for (Integer i: hashSet) { array[j++] = i; } } }
Output Result
[1124, 3654, 9945, 7854]
Using the toArray() method -The toArray() method of the Set interface accepts an array, fills it with all the elements of the current set object, and then returns it. Using this method, you can convert a Set object to an array.
import java.util.HashSet; import java.util.Set; public class SetExample { public static void main(String args[]) { //Instantiate HashSet Set<Integer> hashSet = new HashSet<Integer>(); //Fill the HashSet hashSet.add(1124); hashSet.add(3654); hashSet.add(7854); hashSet.add(9945); //Create an empty integer array Integer[] array = new Integer[hashSet.size()]; //Convert Set object to integer array hashSet.toArray(array); System.out.println(Arrays.toString(array)); } }
Output Result
[1124, 3654, 9945, 7854]
Using Java8Because of the introduction of Java8Streams, and these streams provide methods to convert collection objects to arrays.
import java.util.Arrays; import java.util.HashSet; import java.util.Set; public class SetExample { public static void main(String args[]) { //Instantiate HashSet Set<Integer> hashSet = new HashSet<Integer>(); //Fill the HashSet hashSet.add(1124); hashSet.add(3654); hashSet.add(7854); hashSet.add(9945); System.out.println(hashSet); //Create an empty integer array Integer[] array = hashSet.stream().toArray(Integer[]::new); System.out.println(Arrays.toString(array)); } }
Output Result
[1124, 3654, 9945, 7854]