Java - LinkedHashSet example

Posted on October 23, 2016


This tutorial shows how we can perform various operations on the LinkedHashSet like

  • Adding an element into the LinkedHashSet.
  • Removing an element from the LinkedHashSet.
  • Printing the size of the LinkedHashSet.
  • Checking if an element exists in the LinkedHashSetor not.
  • Removing all elements from the LinkedHashSet.

LinkedHashSetExample.java

package com.boraji.tutorial;

import java.util.LinkedHashSet;
import java.util.Set;

/**
 * @author imssbora
 *
 */
public class LinkedHashSetExample {
	public static void main(String[] args) {
		Set<String> set=new LinkedHashSet<>();
		
		/*Adding elements into the LinkedHashSet*/
		set.add("A");
		set.add("B");
		set.add("C");
		set.add("D");
		set.add("E");
		
		System.out.println("LinkedHashSet elements:"+set);
		
		/*Removing an element from the LinkedHashSet*/
		set.remove("A");
		System.out.println("LinkedHashSet elements after removal:"+set);
		
		/*Printing the size of the LinkedHashSet*/
		System.out.println("LinkedHashSet size:"+set.size());
		
		/*Checking if an elements exist in the LinkedHashSet or not*/
		System.out.println("Element 'D' exist in the LinkedHashSet:"+set.contains("D"));
		
		/*Removing all elements from the LinkedHashSet*/
		set.clear();
		System.out.println("Element in LinkedHashSet:"+set);
	}
}

Output

LinkedHashSet elements:[A, B, C, D, E]
LinkedHashSet elements after removal:[B, C, D, E]
LinkedHashSet size:4
Element 'D' exist in the LinkedHashSet:true
Element in LinkedHashSet:[]