首页 > Java > java教程 > 正文

如何在Java中从LinkedHashSet中查找用户定义的对象?

WBOY
发布: 2023-09-04 08:21:10
转载
1315人浏览过

如何在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',分别为字符串和整数类型。

    百度文心百中
    百度文心百中

    百度大模型语义搜索体验中心

    百度文心百中 22
    查看详情 百度文心百中

    立即学习Java免费学习笔记(深入)”;

  • 在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速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号