如何在Java中将数组转换为 LinkedHashSet?
数组是存储在连续内存位置的项目的集合。根据数组的定义,数组可以包含原始数据类型以及类的对象。而 LinkedHashSet 仅包含唯一元素,并且它们的存储顺序与它们插入的顺序相同。
例子:
Input: arr = {"A", "B", "C", "D", "A"}
Output: LinkedHashSet = {"A", "B", "C", "D"}
LinkedHashSet does not contain repeated elements.
Input: arr = {1, 3, 5, 3, 1}
Output: LinkedHashset = {1, 3, 5}
有几种方法可以将数组转换为Java中 LinkedHashSet 的对象,如下所示:
1.使用asList方法和LinkedHashSet构造函数
- LinkedHashSet 类提供了一个接受集合对象的构造函数。
- 但是要使用它,我们需要使用 Arrays 类的 asList 方法将数组转换为 List。
Java
// Java program to convert an array to LinkedHashSet
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
public class ArrayToLinkedHashSet {
public static void main(String[] args)
{
// array of string
String[] names
= { "John", "Devid", "Smith", "Joe", "Stark" };
// First convert the name array to List and then
// use the LinkedHashSet constructor to convert
// array to linkedhasset
Set linkedhasset_name
= new LinkedHashSet(Arrays.asList(names));
System.out.println(
"LinkedHashSet contains element: " + linkedhasset_name);
}
}
Java
// Java program to convert an array to LinkedHashSet
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
public class ArrayToLinkedHashSet {
public static void main(String[] args) {
String[] names = {"John", "Devid", "Smith", "Joe", "Stark"};
//create new empty LinkedHashSet object to store array element
Set linkhasset_name = new LinkedHashSet();
//add all elements of list to LinkedHashSet
linkhasset_name.addAll( Arrays.asList(names) );
System.out.println("LinkedHashSet contains: " + linkhasset_name);
}
}
Java
// Java program to convert an array to LinkedHashSet
import java.io.*;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.*;
import java.util.stream.*;
import java.util.Set;
public class ArrayToLinkedHashSet {
public static void main(String[] args) {
String[] names = {"John", "Devid", "Smith", "Joe", "Stark"};
//create new empty LinkedHashSet object
Set linkhasset_name = new LinkedHashSet();
//add all elements of list to LinkedHashSet
linkhasset_name = Arrays.stream(names).collect( Collectors.toCollection( LinkedHashSet::new ) );
System.out.println("LinkedHashSet contains: " + linkhasset_name);
}
}
Java
// Java program to convert an array to LinkedHashSet
import java.io.*;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.*;
import java.util.Set;
public class ArrayToLinkedHashSet {
public static void main(String[] args) {
String[] names = {"John", "Devid", "Smith", "Joe", "Stark"};
//create new empty LinkedHashSet object
Set linkhasset_name = new LinkedHashSet();
//iterate an array and add elements one by one to the linkedhasset
for(String name : names){
linkhasset_name.add(name);
}
System.out.println("LinkedHashSet contains: " + linkhasset_name);
}
}
输出
LinkedHashSet contains element: [John, Devid, Smith, Joe, Stark]
2.使用asList方法和addAll方法
- 与前面的方法类似,我们首先将数组转换为列表
- 但是这次我们将使用 addAll 方法将数组中的元素添加到链接的哈希集,而不是使用构造函数。
Java
// Java program to convert an array to LinkedHashSet
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
public class ArrayToLinkedHashSet {
public static void main(String[] args) {
String[] names = {"John", "Devid", "Smith", "Joe", "Stark"};
//create new empty LinkedHashSet object to store array element
Set linkhasset_name = new LinkedHashSet();
//add all elements of list to LinkedHashSet
linkhasset_name.addAll( Arrays.asList(names) );
System.out.println("LinkedHashSet contains: " + linkhasset_name);
}
}
输出
LinkedHashSet contains: [John, Devid, Smith, Joe, Stark]
3. 使用Java 8 Stream API:
- 与 HashSet 类似,您也可以使用 Collectors 类的 toCollection() 方法将 Stream 转换为 LinkedHashSet。
- 首先,将数组转换为流,然后使用仅适用于Java 8 版本的集合流将流转换为 LinkedHashSet。
collect( Collectors.toCollection( LinkedHashSet::new ) );
Java
// Java program to convert an array to LinkedHashSet
import java.io.*;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.*;
import java.util.stream.*;
import java.util.Set;
public class ArrayToLinkedHashSet {
public static void main(String[] args) {
String[] names = {"John", "Devid", "Smith", "Joe", "Stark"};
//create new empty LinkedHashSet object
Set linkhasset_name = new LinkedHashSet();
//add all elements of list to LinkedHashSet
linkhasset_name = Arrays.stream(names).collect( Collectors.toCollection( LinkedHashSet::new ) );
System.out.println("LinkedHashSet contains: " + linkhasset_name);
}
}
输出
LinkedHashSet contains: [John, Devid, Smith, Joe, Stark]
4. 使用循环或蛮力
- 我们可以使用 for 循环遍历数组的所有元素,并将元素一一添加到 LinkedHashSet 对象,如下所示。
Java
// Java program to convert an array to LinkedHashSet
import java.io.*;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.*;
import java.util.Set;
public class ArrayToLinkedHashSet {
public static void main(String[] args) {
String[] names = {"John", "Devid", "Smith", "Joe", "Stark"};
//create new empty LinkedHashSet object
Set linkhasset_name = new LinkedHashSet();
//iterate an array and add elements one by one to the linkedhasset
for(String name : names){
linkhasset_name.add(name);
}
System.out.println("LinkedHashSet contains: " + linkhasset_name);
}
}
输出
LinkedHashSet contains: [John, Devid, Smith, Joe, Stark]