如何在 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 中獲取使用者定義的物件。

更新於:2023-07-19

170 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.