从 LinkedHashSet 中获取第一个元素的Java程序
LinkedHashSet 是集合抽象数据类型 (ADT) 的实现。它从 HashSet 类扩展而来,后者又实现了 Set 接口。 LinkedHashSet 和 HashSet 的区别在于维护元素排序的属性。 LinkedList 只是一个包含元素序列的容器。根据定义,Set 不应包含重复元素。此属性不需要对 LinkedList ADT 保持良好。当元素应该被复制并且必须保持顺序时,应该使用 LinkedHashSet。
方法:
基本上有三种标准方法可以从 LinkedHashSet 获取元素而不将其更改为不同的数据结构。
- 将其转换为数组或列表
- 使用迭代器
- 使用流
实现:从 LinkedHashSet 中获取第一个元素
方法一:通过将其转换为数组或列表
例子
Java
// Java Program to Get the First Element from LinkedHashSet
// by converting it to an array or List
// Array is demonstrated below so do with List
// Importing generic java packages
import java.io.*;
import java.lang.*;
import java.util.*;
// Class
public class GFG {
// Main driver method
public static void main(String[] args)
throws java.lang.Exception
{
// Creating a LinkedHashMap object
// Declaring object of Integer type
LinkedHashSet hashSet
= new LinkedHashSet();
// Adding elements to LinkedHashMap
hashSet.add(2);
hashSet.add(1);
hashSet.add(4);
hashSet.add(6);
hashSet.add(8);
// Condition check using isEmpty() method which
// holds
// True till there is a single element in an object
// is remaining False, when there is no object left
// or if initially there was no element added
if (!hashSet.isEmpty()) {
// Converting the above Map to an array
Integer arr[] = new Integer[hashSet.size()];
arr = hashSet.toArray(arr);
// Accessing the first element by passing 0
// as an argument which by default
// accesses and prints out first element
System.out.println("First element: " + arr[0]);
}
}
}
Java
// Java Program to Get the First Element from LinkedHashSet
// Using Iterators
// Importing generic java packages
import java.util.*;
import java.lang.*;
import java.io.*;
// Class
public class GFG {
// Main driver method
public static void main(String[] args)
throws java.lang.Exception
{
// Creating a LinkedHashMap
LinkedHashSet hashSet
= new LinkedHashSet();
// Adding elements to LinkedHashMap
hashSet.add(1);
hashSet.add(2);
hashSet.add(3);
hashSet.add(4);
hashSet.add(5);
// Iterator over LinkedHashMap
Iterator iter = hashSet.iterator();
if (iter != null && iter.hasNext()) {
// Display the first element of Map using next()
// ethod
System.out.println(
"First element in LinkedHashSet: "
+ iter.next());
}
}
}
Java
// Java Program to Get the First Element from LinkedHashSet
// Using Streams
// Importing generic java packages
import java.util.*;
import java.lang.*;
import java.io.*;
// Class
class GFG {
// Main driver method
public static void main(String[] args) throws java.lang.Exception
{
// Creating a LinkedHashMap
LinkedHashSet hashSet
= new LinkedHashSet();
// Adding elemens to LinkedHashMap
hashSet.add(1);
hashSet.add(2);
hashSet.add(3);
hashSet.add(4);
hashSet.add(5);
// Checking whether Map is empty or not
if (hashSet.size() == 0)
// Display message
System.out.println("The Set is Empty!");
else {
// Using stream() through findFirst() method
// over the elements of LinkedHashMap
int first = hashSet.stream().findFirst().get();
// Printing the first element of LinkedHashMap
System.out.println(
"First element in LinkedHashSet: " + first);
}
}
}
输出
First element: 2
方法 2:使用迭代器
例子
Java
// Java Program to Get the First Element from LinkedHashSet
// Using Iterators
// Importing generic java packages
import java.util.*;
import java.lang.*;
import java.io.*;
// Class
public class GFG {
// Main driver method
public static void main(String[] args)
throws java.lang.Exception
{
// Creating a LinkedHashMap
LinkedHashSet hashSet
= new LinkedHashSet();
// Adding elements to LinkedHashMap
hashSet.add(1);
hashSet.add(2);
hashSet.add(3);
hashSet.add(4);
hashSet.add(5);
// Iterator over LinkedHashMap
Iterator iter = hashSet.iterator();
if (iter != null && iter.hasNext()) {
// Display the first element of Map using next()
// ethod
System.out.println(
"First element in LinkedHashSet: "
+ iter.next());
}
}
}
输出
First element in LinkedHashSet: 1
方法 3:使用流
例子:
Java
// Java Program to Get the First Element from LinkedHashSet
// Using Streams
// Importing generic java packages
import java.util.*;
import java.lang.*;
import java.io.*;
// Class
class GFG {
// Main driver method
public static void main(String[] args) throws java.lang.Exception
{
// Creating a LinkedHashMap
LinkedHashSet hashSet
= new LinkedHashSet();
// Adding elemens to LinkedHashMap
hashSet.add(1);
hashSet.add(2);
hashSet.add(3);
hashSet.add(4);
hashSet.add(5);
// Checking whether Map is empty or not
if (hashSet.size() == 0)
// Display message
System.out.println("The Set is Empty!");
else {
// Using stream() through findFirst() method
// over the elements of LinkedHashMap
int first = hashSet.stream().findFirst().get();
// Printing the first element of LinkedHashMap
System.out.println(
"First element in LinkedHashSet: " + first);
}
}
}
输出
First element in LinkedHashSet: 1