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 instance then it returns true otherwise false. Its return type islean .
文法
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中文網其他相關文章!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具