- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a particular element exists in Java LinkedHashSet
Use the contains() method to check if a specific element exists in LinkedHashSet or not.
Let us first create a LinkedHashSet and add some elements −
LinkedHashSet<String> l = new LinkedHashSet<String>(); l.add(new String("1")); l.add(new String("2")); l.add(new String("3")); l.add(new String("4")); l.add(new String("5")); l.add(new String("6")); l.add(new String("7"));
Now, check whether it contains element “5” or not −
l.contains("5")
The following is an example to check if a particular element exists in LinkedHashSet −
Example
import java.util.*; public class Demo { public static void main(String[] args) { LinkedHashSet<String> l = new LinkedHashSet<String>(); l.add(new String("1")); l.add(new String("2")); l.add(new String("3")); l.add(new String("4")); l.add(new String("5")); l.add(new String("6")); l.add(new String("7")); System.out.println("LinkedHashSet elements..."); System.out.println(l); System.out.println("Does 5 exist in the LinkedHashSet elements? "+l.contains("5")); } }
Output
LinkedHashSet elements... [1, 2, 3, 4, 5, 6, 7] Does 5 exist in the LinkedHashSet elements? True
- Related Articles
- Java Program to check if a particular element exists in HashSet
- Check if a particular key exists in Java LinkedHashMap
- Check if a particular value exists in Java LinkedHashMap
- Check if a particular value exists in Java TreeSet
- Java Program to check if a particular key exists in TreeMap
- Java Program to check if a particular value exists in TreeMap
- Check if a file exists in Java\n
- Remove specified element from Java LinkedHashSet
- Check if a given key exists in Java HashMap
- Check if a Point Exists in Circle Sector in Java
- Check if element exists in list of lists in Python
- How to check if event exists on element in jQuery?
- How to check if onClick exists on element in jQuery?
- How to check if element exists in the visible DOM?
- How to check if Element exists in c# Selenium drivers?

Advertisements