如何在Java中从LinkedHashSet中查找用户定义的对象?
LinkedHashSet是Java Collection Framework的一个类,它实现了Set接口并扩展了HashSet类。它是一种链表类型的集合类。它按照插入的顺序存储和返回对象,因此不允许重复的对象。在本文中,我们将使用内置方法'contains()'从LinkedHashSet中查找用户定义的对象。用户定义的对象是通过构造函数创建的。
Java Program to get User-Defined Objects from LinkedHashSet
让我们简要介绍一下我们在示例程序中将使用的两个重要的内置方法。
add()
它接受一个参数并将其添加到集合的末尾。它与LinkedHashSet类的实例一起使用。
语法
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.
语法
nameOfobject.contains(Object)
Here,
Object 表示我们需要验证的对象的名称
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
首先,定义一个名为 'LinkHset' 的类,在类内部声明两个变量,并定义一个构造函数,该构造函数带有两个参数 'item' 和 'price',分别为字符串和整数类型。
在main方法中,创建一些'LinkHset'类的实例。然后,声明一个LinkedHashSet的集合,并将用户定义的对象放入该集合中
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"); } } }
输出
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
的中文翻译为:示例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中从LinkedHashSet中查找用户定义的对象?的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

胶囊是一种三维几何图形,由一个圆柱体和两端各一个半球体组成。胶囊的体积可以通过将圆柱体的体积和两端半球体的体积相加来计算。本教程将讨论如何使用不同的方法在Java中计算给定胶囊的体积。 胶囊体积公式 胶囊体积的公式如下: 胶囊体积 = 圆柱体体积 两个半球体体积 其中, r: 半球体的半径。 h: 圆柱体的高度(不包括半球体)。 例子 1 输入 半径 = 5 单位 高度 = 10 单位 输出 体积 = 1570.8 立方单位 解释 使用公式计算体积: 体积 = π × r2 × h (4

Spring Boot简化了可靠,可扩展和生产就绪的Java应用的创建,从而彻底改变了Java开发。 它的“惯例惯例”方法(春季生态系统固有的惯例),最小化手动设置
