Java Program to Convert Array to Set and Vice-Versa

Program to Convert Array to Set and Vice-Versa

What are Sets?

In Java, a set is a collection of unique elements, meaning that no element can be repeated. Sets are implemented in the Java Collection Framework and are a part of the Java standard library.

In this Article, we will write a program to Calculate union of two sets.

Sets in Java :

The Java Set interface, which is a part of the Java Collections Framework, defines the behavior of a set. The interface is implemented by several classes, including HashSet, LinkedHashSet, and TreeSet. These classes provide different implementations of the set interface, each with their own unique characteristics and performance characteristics.

Sets are a type of data structure that stores a collection of unique elements. They are used to store and manipulate a collection of distinct items, such as a list of names or a list of unique words in a document. Sets are commonly implemented in computer programming languages, and the behavior of sets is typically defined by a set interface or abstract class.

Program to Convert Array to Set :

Run
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class Main{
    public static void main(String[] args){
        // Initializing an array of strings
        String[] array = {"apple", "banana", "orange"};

        // Convert the array to a set
        Set set = new HashSet<>(Arrays.asList(array));

        // Print the set
        System.out.println("Set: " + set);
    }
}
Output:

Set: [banana, orange, apple]

This program creates an array, and adds some elements to it. Then it converts the array to a set using the Arrays.asList(array) method which returns a list containing the elements of the specified array in the order they are returned by the array’s iterator. This list is then passed to the constructor of the HashSet class to create a set. Finally, it prints the set.

It’s worth noting that the HashSet class implements the Set interface, so the return type of the variable set can be set to Set

The Arrays.asList(T… a) method returns a fixed-size list backed by the specified array. This method acts as bridge between array-based and collection-based APIs, in other words it enables us to use an array in Collection APIs.

Program to Convert Set to Array :

Run
import java.util.HashSet;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        // Initialize a set
        Set < Integer > mySet = new HashSet<>();
        mySet.add(1);
        mySet.add(2);
        mySet.add(3);

        // Convert set to array
        Integer[] myArray = mySet.toArray(new Integer[mySet.size()]);

        // Print the array
        for (Integer i : myArray) {
            System.out.print(i + " ");
        }
    }
}
Output:

1, 2, 3

In this example, the set mySet is initialized with three elements. The toArray() method is then used to convert the set to an array. The resulting array is then printed to the console. The method toArray() takes an array as argument to copy the elements of the set into this array. If the array passed is not big enough, it creates a new array of the same type and size of the set and returns it.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription