How to Create LinkedHashSet in Java


This example focuses on how to use LinkedHashSet in Java. It also explains how to add something to LinkedHashSet object with add method.

Source Code

package com.beginner.examples;

import java.util.LinkedHashSet;

public class LinkedHashSetExample {

	public static void main(String[] args) {
		
		//create LinkedHashSet.
		LinkedHashSet linkedHashSet = new LinkedHashSet();
		
		//Use the method add of LinkedHashSet to add elements.
		linkedHashSet.add("A");
		linkedHashSet.add("B");
		linkedHashSet.add("C");
		linkedHashSet.add("D");
		
		System.out.println("LinkedHashSet contains " + linkedHashSet); 
	
	}

}

Output:

LinkedHashSet contains [A, B, C, D]

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments