Java Initialize Set

In this tutorial we will learn how to initialize set in java using various methods . We can create Set in java using constructor , add() method , collections.addAll() method , Anonymous Subclass etc.

1. Java Initialize Set – Using Constructor

We can initialize set in java using constructor . Firstly we initialize array of String then convert array to List and then provide this to TreeSet constructor that accepts another collection . Let’s see java program for java set initialization using constructor .

import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;

public class JavaInitializeSet {
	
	public static void main(String[] args) {
		
		   //initialize array of String 
		   String [] arr = {"php","html","android"};   
		 
		   Set<String> set = new TreeSet<>(Arrays.asList(arr));
		
		   //print HashSet element 
	       System.out.println("Set Elements : "+ set);
	}

}

Output:

Set Elements : [android, html, php]

2. Java Initialize Set Using add() Method

In this example, We will initialize Set in java using add() method . We create Set by empty constructor and then add elements in this using add() method .

import java.util.Set;
import java.util.TreeSet;

public class JavaInitializeSet1 {
	
	public static void main(String[] args) {
		
		Set<String>	set = new TreeSet<String>();
		   
		   //add element in it 
		set.add("php");
		set.add("html");
		set.add("android");
		   
		//print the element of Set 
		System.out.println("Set Elements : "+ set);
	}

}
Output:
Set Elements : [android, html, php]

3. Initialize Set In Java using Collections.addAll()

We create empty set first . We will create TreeSet and add some elements in it . Now we will provides TreeSet to Collections.addAll() method and it will do set initialization in java with the element of collection (TreeSet).

import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;

public class JavaInitializeSet2 {
	
	public static void main(String[] args) {
		
		Set<String>	set = new TreeSet<String>();
		
		TreeSet<String> treeSet = new TreeSet<>();
		   
		//add element in treeSet 
		treeSet.add("php");
		treeSet.add("html");
		treeSet.add("android");
		
		System.out.println("TreeSet Elements : "+ treeSet);
		
		Collections.addAll(set = treeSet);
		
		System.out.println("Set Elements : "+ set);
		
	}

}

Output:

TreeSet Elements : [android, html, php]
Set Elements : [android, html, php]

4. Java Set Initialization Using Collections.singleton()

We can use Collections.singleton() method to create a set containing only a one element.  Collections.singleton() method In java returns an immutable set containing that only one element.
import java.util.Collections;
import java.util.Set;

public class JavaInitializeSet3 {
	
	public static void main(String[] args) {
		
		Set<String> set = Collections.singleton("JavaVogue");
        System.out.println("Singleton Set : "+ set);
		
	}

}
Output:
Singleton Set : [JavaVogue]

5. Initialize Set Java Using Anonymous Subclass

We can initialize Set in java with the help of anonymous subclass . But we should avoid this way for java set initialization because first we create anonymous class and then initialize set , it might me create problem of memory leak.
import java.util.Set;
import java.util.TreeSet;

public class JavaInitializeSet4 {
	
	public static void main(String[] args) {
		
		Set<String> set = new TreeSet<String>() {{
		    add("php");
		    add("html");
		    add("android");
		}};
		
		System.out.println("Set Elements : "+ set);
		
	}
	
}
Output:
Set Elements : [android, html, php]
In this tutorial we leaned about java set initialization using various example . You can learn more java TreeSet example for practice .

Leave a Reply

Your email address will not be published. Required fields are marked *

− 2 = 3