1.Basic java example program to check particular element is exists in linkedhashset
Output:
- boolean contains(Object o) This method Returns true if this set contains the specified element
- package com.checkelementhashset;
- import java.util.LinkedHashSet;
- import java.util.Iterator;
- public class LinkedHashsetExample{
- public static void main(String[] args) {
- LinkedHashSet<String> linkedhashset = new LinkedHashSet<>();
- linkedhashset.add("Java Interview Questions");
- linkedhashset.add("Java interview program");
- linkedhashset.add("Concept and example program");
- linkedhashset.add("Concept and interview questions");
- linkedhashset.add("Java Quiz");
- /*
- To check whether a particular value exists in LinkedHashSet we need to use
- boolean contains(Object value) method of HashSet class.
- this method returns true if the LinkedHashSet contains the value, otherwise returns false.
- */
- boolean isExists = linkedhashset.contains("Java Quiz");
- System.out.println("Java Quiz exists in LinkedHashSet? : " + isExists);
- Iterator it=linkedhashset.iterator();
- while(it.hasNext()){
- System.out.println(it.next());
- }
- }
- }
Output:
- Java Quiz exists in LinkedHashSet ? : true
- Java Interview Questions
- Java interview program
- Concept and example program
- Concept and interview questions
- Java Quiz
Bagikan
Check if a particular element exists in Java LinkedHashSet Example
4/
5
Oleh
Kris Kimcil