Java 如何从LinkedHashSet中找到用户定义的对象

Java 如何从LinkedHashSet中找到用户定义的对象

LinkedHashSet是Java集合框架的一种类,它实现了Set接口并扩展了HashSet类。它是一种链表类型的集合类。它按照插入的顺序存储并返回对象,因此不允许重复对象。在本文中,我们将使用内置的方法“contains()”从LinkedHashSet中找到用户定义的对象。用户定义的对象是通过构造函数创建的。

从LinkedHashSet中获取用户定义的对象的Java程序

让我们简要介绍一下我们将在示例程序中使用的两个重要的内置方法。

add()

它接受一个参数并将其添加到集合的末尾。它与LinkedHashSet类的实例一起使用。

语法

nameOfobject.add(argument)

在这里, argument 表示我们要存储在集合中的值。

contains()

它接受一个LinkedHashSet类的实例,并检查传递的实例是否在集合中可用。如果集合包含该实例,则返回true;否则返回false。它的返回类型是Boolean。

语法

nameOfobject.contains(Object)

在这里,

Object 表示我们要验证的对象的名称。

nameOfobject 表示包含所有集合的类的对象。

示例1

下面的示例说明了我们如何使用contains()方法从LinkedHashSet集合中查找用户定义的对象。

方法

  • 首先,定义一个名为’LinkHset’的类,类中声明两个变量,并定义一个带有两个参数’string’和’integer’类型的构造函数。

  • 在主方法中,创建一些’LinkHset’类的实例。然后,声明一个LinkedHashSet集合,并将用户定义的对象放入此集合中。

  • 现在,使用’contains()’方法检查指定的对象是否可用。

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

输出

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

示例2

在下面的示例中,我们将使用contains()方法和iterator来从LinkedHashSet集合中查找一个用户定义的方法。

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;
         }
      }
   }
}

输出

Item: Shoes, Price: 55

结论

我们在本文中介绍了实现Set接口并扩展HashSet类的LinkedHashSet类。在下一节中,我们讨论了它的内置方法’add()’和’contains()’,这些方法帮助我们从LinkedHashSet获取用户定义的对象。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程