如何在 Java 中从 LinkedHashSet 中查找用户定义的对象?
LinkedHashSet 是 Java 集合框架的一个类,它实现了 Set 接口并扩展了 HashSet 类。它是一种链表类型的集合类。它按插入对象的顺序存储和返回对象,因此它不允许重复的对象。在本文中,我们将使用内置方法“contains()”从 LinkedHashSet 中查找用户定义的对象。用户定义的对象是在构造函数的帮助下创建的。
从 LinkedHashSet 获取用户定义对象的 Java 程序
让我们简要介绍一下将在示例程序中使用的两个重要的内置方法。
add()
它接受一个参数并将其添加到集合的末尾。它与 LinkedHashSet 类的实例一起使用。
语法
nameOfobject.add(argument)
这里,参数表示我们将存储在集合中的值。
contains()
它接受 LinkedHashSet 类的实例并检查传递的实例是否在集合中。如果集合包含该实例,则返回 true,否则返回 false。它的返回类型是布尔型。
语法
nameOfobject.contains(Object)
这里,
对象表示我们必须验证的对象的名称。
对象名表示包含所有集合的类的对象。
示例 1
以下示例说明了如何使用 contains() 方法从 LinkedHashSet 集合中查找用户定义的对象。
方法
首先,定义一个名为“LinkHset”的类,并在其中声明两个变量,并定义该类的构造函数以及两个参数“item”和“price”,类型分别为字符串和整数。
在 main 方法中,创建一些“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() 方法从 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 中获取用户定义的对象。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP