Home > Java > javaTutorial > body text

How to find user-defined object from LinkedHashSet in Java?

WBOY
Release: 2023-09-04 08:21:10
forward
1195 people have browsed it

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)
Copy after login

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)
Copy after login

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");
      }
   }
}
Copy after login

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
Copy after login
The 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;
         }
      }
   }
}
Copy after login

Output

Item: Shoes, Price: 55
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!