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: Shoes
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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is
