How to find user-defined object from LinkedHashSet in Java?
LinkedHashSet is a class of Java Collection Framework that implements the Set interface and extends the HashSet class. It is a linked list type collection class. It stores and returns objects in the order of insertion, so duplicate objects are not allowed. In this article, we will use the built-in method 'contains()' to find user-defined objects from a LinkedHashSet. User-defined objects are created through constructors.
Java Program to get User-Defined Objects from LinkedHashSet
Let’s briefly introduce two important built-in methods that we will use in our sample program.
add()
It takes one parameter and adds it to the end of the collection. It is used with instances of LinkedHashSet class.
grammar
nameOfobject.add(argument)
Here, argument signifies the value that we are going to store in the set.
contains()
It accepts an instance of LinkedHashSet class and checks whether the passed instance is available in the set or not. If the set contains that instance then it returns true otherwise false. Its return type is Boolean.
grammar
nameOfobject.contains(Object)
Here,
Object represents the name of the object we need to verify
nameOfobject signifies the object of that class which conatins all the collections.
Example 1
The following example illustrates how we can use contains() method to find the user-defined objects from a LinkedHashSet collection.
Approach
First, define a class named 'LinkHset', declare two variables inside the class, and define a constructor with two parameters 'item' and 'price', respectively String and integer types.
In the main method, create some instances of the 'LinkHset' class. Then, declare a collection of LinkedHashSet and put the user-defined objects into the collection
Now, use the 'contains()' method to check whether the specified object is available or not.
import java.util.*; public class LinkHset { String item; int price; LinkHset(String item, int price) { // constructor // this keyword shows these variables belong to constructor this.item = item; this.price = price; } public static void main(String[] args) { // defining the objects LinkHset col1 = new LinkHset("TShirt", 59); LinkHset col2 = new LinkHset("Trouser", 60); LinkHset col3 = new LinkHset("Shirt", 45); LinkHset col4 = new LinkHset("Watch", 230); LinkHset col5 = new LinkHset("Shoes", 55); // Declaring collection of LinkedHashSet LinkedHashSet<LinkHset> linkHcollection = new LinkedHashSet<LinkHset>(); // Adding the user-defined objects to the collection linkHcollection.add(col1); linkHcollection.add(col2); linkHcollection.add(col3); linkHcollection.add(col4); linkHcollection.add(col5); // to print the all objects for (LinkHset print : linkHcollection) { System.out.println("Item: " + print.item + ", " + "Price: " + print.price); } // to find a specified objects if(linkHcollection.contains(col5)) { System.out.println("Set contains the specified collection: " + col5.item); } else { System.out.println("Sorry! couldn't find the object"); } } }
Output
Item: TShirt, Price: 59 Item: Trouser, Price: 60 Item: Shirt, Price: 45 Item: Watch, Price: 230 Item: Shoes, Price: 55 Set contains the specified collection: ShoesThe Chinese translation of
Example 2
is:Example 2
In the following example, we will use the contains() method and iterator to find user-defined methods from the LinkedHashSet collection.
import java.util.*; public class LinkHset { String item; int price; LinkHset(String item, int price) { // constructor // this keyword shows these variables belong to constructor this.item = item; this.price = price; } public static void main(String[] args) { // defining the objects LinkHset col1 = new LinkHset("TShirt", 59); LinkHset col2 = new LinkHset("Trouser", 60); LinkHset col3 = new LinkHset("Shirt", 45); LinkHset col4 = new LinkHset("Watch", 230); LinkHset col5 = new LinkHset("Shoes", 55); // Declaring collection of LinkedHashSet LinkedHashSet<LinkHset> linkHcollection = new LinkedHashSet< LinkHset>(); // Adding the user-defined objects to the collection linkHcollection.add(col1); linkHcollection.add(col2); linkHcollection.add(col3); linkHcollection.add(col4); linkHcollection.add(col5); // creating iterator interface to iterate through objects Iterator<LinkHset> itr = linkHcollection.iterator(); while (itr.hasNext()) { // to print the specified object only if(linkHcollection.contains(col5)) { System.out.println("Item: " + col5.item + ", " + "Price: " + col5.price); break; } else { System.out.println("Sorry! couldn't find the object"); break; } } } }
Output
Item: Shoes, Price: 55
in conclusion
We start this article by introducing the LinkedHashSet class that implements the Set interface and extends the HashSet class. In the next section, we discussed its built-in methods 'add()' and 'contains()' which help us to get user-defined objects from LinkedHashSet
The above is the detailed content of How to find user-defined object from LinkedHashSet in Java?. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)