How to Find User Defined Objects 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 of collection class. It stores and returns the objects in the order in which they were inserted, therefore it does not allow duplicate objects. In this article, we will use the built-in method 'contains()'' to find user-defined objects from LinkedHashSet. The user-defined objects are created with the help of constructors.

Java Program to get User-Defined Objects from LinkedHashSet

Let's have a brief introduction of two important in-built methods that we will use in our example program.

add()

It takes a single argument and adds it to the end of collection. It is used along with the instance of LinkedHashSet class.

Syntax

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.

Syntax

nameOfobject.contains(Object)

Here,

Object indicates the name of that object which we have 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' and inside it, declare two variables and define a constructor of this class along with two parameters 'item' and 'price' of type string and integer respectively.

  • In the main method, create some instances of the 'LinkHset' class. Then, declare a collection of LinkedHashSet and the user-defined objects into this 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

In the following example we will use the contains() method with the iterator to find a user-defined method from a 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

Conclusion

We started this article by introducing the LinkedHashSet class that implements Set Interface and extends HashSet class. In the next section, we discussed its built-in method 'add()' and 'contains()' that helped us to get the user-defined objects from LinkedHashSet.

Updated on: 19-Jul-2023

134 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements