如何在Java中从 LinkedHashSet 中查找用户定义的对象?
LinkedHashSet 用于存储元素的插入顺序。当需要维护迭代顺序时,使用这个类。当遍历 HashSet 时,顺序是不可预测的,而 LinkedHashSet 允许我们按照元素插入的顺序遍历元素。当使用迭代器循环遍历 LinkedHashSet 时,元素将返回到它们插入的顺序。
我们有一个 Student 类和一个 Student 类型的 LinkedHashSet,我们想看看 LinkedHashSet 是否包含该对象。
// Student class
class Student {
int rollNo;
String name;
Student(int r, String s) {
rollNo = r;
name = s;
}
}
// LinkedHashSet of type Student
LinkedHashSet set = new LinkedHashSet<>();
有两种方法可以在 LinkedHashSet 中找到对象:
- 使用 contains() 方法
- 使用迭代 在 LinkedHashSet 上
方法一:使用contains()方法
我们可以使用Java中的 contains() 方法在 LinkedHashSet 中找到对象。如果对象存在于 LinkedHashSet 中, Java中的 contains() 方法返回 true,否则返回 false。
Java
// Java Program to find user defined objects from
// LinkedHashSet
import java.util.*;
// Student class
class Student {
int rollNo;
String name;
Student(int r, String s)
{
rollNo = r;
name = s;
}
}
public class GFG {
public static void main(String[] args)
{
// Objects of Student
Student stu1 = new Student(1, "Akshay");
Student stu2 = new Student(2, "Bina");
Student stu3 = new Student(3, "Chintu");
Student stu4 = new Student(4, "Dheeraj");
// New empty LinkedHashSet of type Student
LinkedHashSet set = new LinkedHashSet<>();
// Add objects to set
set.add(stu1);
set.add(stu2);
set.add(stu3);
// Return true if set contains the obj otherwise,
// false
if (set.contains(stu4))
{
System.out.println(stu4.name+ " is present in set.");
}
else
{
System.out.println(stu4.name+ " is not present in set.");
}
}
}
Java
// Java Program to find user defined objects from
// LinkedHashSet
import java.util.*;
// Student class
class Student {
int rollNo;
String name;
Student(int r, String s)
{
rollNo = r;
name = s;
}
}
public class GFG {
public static void main(String[] args)
{
// Objects of Student
Student stu1 = new Student(1, "Akshay");
Student stu2 = new Student(2, "Bina");
Student stu3 = new Student(3, "Chintu");
Student stu4 = new Student(4, "Dheeraj");
// New empty LinkedHashSet of type Student
LinkedHashSet set = new LinkedHashSet<>();
// Add objects to set
set.add(stu1);
set.add(stu2);
set.add(stu3);
// Using iterate LinkedHashSet
for (Student stu : set)
{
if (stu == stu2)
{
System.out.println(stu.name+ " present in set.");
}
}
}
}
输出
Dheeraj is not present in set.
方法 2:在 LinkedHashSet 上使用迭代:
我们还可以使用迭代在 LinkedHashSet 中找到该对象。
Java
// Java Program to find user defined objects from
// LinkedHashSet
import java.util.*;
// Student class
class Student {
int rollNo;
String name;
Student(int r, String s)
{
rollNo = r;
name = s;
}
}
public class GFG {
public static void main(String[] args)
{
// Objects of Student
Student stu1 = new Student(1, "Akshay");
Student stu2 = new Student(2, "Bina");
Student stu3 = new Student(3, "Chintu");
Student stu4 = new Student(4, "Dheeraj");
// New empty LinkedHashSet of type Student
LinkedHashSet set = new LinkedHashSet<>();
// Add objects to set
set.add(stu1);
set.add(stu2);
set.add(stu3);
// Using iterate LinkedHashSet
for (Student stu : set)
{
if (stu == stu2)
{
System.out.println(stu.name+ " present in set.");
}
}
}
}
输出
Bina present in set.