How to Check If a Particular Element Exists in LinkedHashSet in Java


In this example we will show how to check whether an elements is contained in Java LinkedHashSet using contains method.

Source Code

package com.beginner.examples;

import java.util.LinkedHashSet;

public class CheckElementLinkedHashSe {

	public static void main(String[] args) {
		
		//create LinkedHashSet.
		LinkedHashSet linkedHashSet = new LinkedHashSet();
		
		linkedHashSet.add("A");
		linkedHashSet.add("B");
		linkedHashSet.add("C");
		linkedHashSet.add("D");
		
		/*
		 * To check whether a particular value exists in LinkedHashSet use contains(Object value) method of LinkedHashSet.
		 */
		boolean result = linkedHashSet.contains("D");
		System.out.println("D exists in LinkedHashSet ? : " + result);
	}

}

Output:

D exists in LinkedHashSet ? : true

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments