首頁  >  文章  >  Java  >  如何在Java中從LinkedHashSet中尋找使用者定義的物件?

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

WBOY
WBOY轉載
2023-09-04 08:21:101205瀏覽

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

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除