目錄
文法
contains()
Example 2
Approach
輸出
範例2
結論
首頁 Java java教程 如何在Java中從LinkedHashSet中尋找使用者定義的物件?

如何在Java中從LinkedHashSet中尋找使用者定義的物件?

Sep 04, 2023 am 08:21 AM
java 尋找 linkedhashset

如何在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 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中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Java 中的完美數 Java 中的完美數 Aug 30, 2024 pm 04:28 PM

Java 完美數指南。這裡我們討論定義,如何在 Java 中檢查完美數?

Java 中的隨機數產生器 Java 中的隨機數產生器 Aug 30, 2024 pm 04:27 PM

Java 隨機數產生器指南。在這裡,我們透過範例討論 Java 中的函數,並透過範例討論兩個不同的生成器。

Java中的Weka Java中的Weka Aug 30, 2024 pm 04:28 PM

Java 版 Weka 指南。這裡我們透過範例討論簡介、如何使用 weka java、平台類型和優點。

Java 中的史密斯數 Java 中的史密斯數 Aug 30, 2024 pm 04:28 PM

Java 史密斯數指南。這裡我們討論定義,如何在Java中檢查史密斯號?帶有程式碼實現的範例。

Java Spring 面試題 Java Spring 面試題 Aug 30, 2024 pm 04:29 PM

在本文中,我們保留了最常被問到的 Java Spring 面試問題及其詳細答案。這樣你就可以順利通過面試。

突破或從Java 8流返回? 突破或從Java 8流返回? Feb 07, 2025 pm 12:09 PM

Java 8引入了Stream API,提供了一種強大且表達力豐富的處理數據集合的方式。然而,使用Stream時,一個常見問題是:如何從forEach操作中中斷或返回? 傳統循環允許提前中斷或返回,但Stream的forEach方法並不直接支持這種方式。本文將解釋原因,並探討在Stream處理系統中實現提前終止的替代方法。 延伸閱讀: Java Stream API改進 理解Stream forEach forEach方法是一個終端操作,它對Stream中的每個元素執行一個操作。它的設計意圖是處

Java 中的時間戳至今 Java 中的時間戳至今 Aug 30, 2024 pm 04:28 PM

Java 中的時間戳記到日期指南。這裡我們也結合範例討論了介紹以及如何在java中將時間戳記轉換為日期。

Java程序查找膠囊的體積 Java程序查找膠囊的體積 Feb 07, 2025 am 11:37 AM

膠囊是一種三維幾何圖形,由一個圓柱體和兩端各一個半球體組成。膠囊的體積可以通過將圓柱體的體積和兩端半球體的體積相加來計算。本教程將討論如何使用不同的方法在Java中計算給定膠囊的體積。 膠囊體積公式 膠囊體積的公式如下: 膠囊體積 = 圓柱體體積 兩個半球體體積 其中, r: 半球體的半徑。 h: 圓柱體的高度(不包括半球體)。 例子 1 輸入 半徑 = 5 單位 高度 = 10 單位 輸出 體積 = 1570.8 立方單位 解釋 使用公式計算體積: 體積 = π × r2 × h (4

See all articles