Java Set到列表

Java Set到列表

在Java中给定一个字符串的集合(HashSet或TreeSet),将其转换为一个字符串的列表(ArrayList或LinkedList)。在Java中, Set接口 存在于java.util包中,它扩展了Collection接口,是一个无序的对象集合,其中不能存储重复的值。 List接口 提供了一种存储有序集合的方法。它是Collection的一个子接口。它是一个对象的有序集合,其中可以存储重复的值。
输入 : Set hash_Set = new HashSet();
hash_Set.add(“Geeks”);
hash_Set.add(“For”);

输出 : ArrayList or Linked List with
following content
{“Geeks”, “for”}

Java 把集合转换为列表的方法

在Java中,有许多方法可以将集合转换为列表。下面列出了其中的几种。

  1. 使用集合遍历
  2. 使用ArrayList或LinkedList构造函数
  3. 使用addAll()方法
  4. 在Java中使用流
  5. 在Java中使用List.copyOf()方法

1.使用集合遍历

我们简单地创建一个列表。我们遍历给定的集合,并一个一个地将元素添加到列表中。

// Java program to demonstrate conversion of
// Set to array using simple traversal
 
import java.util.*;
 
class Test {
    public static void main(String[] args)
    {
 
        // Creating a hash set of strings
        Set<String> s = new HashSet<String>();
        s.add("Geeks");
        s.add("for");
 
        int n = s.size();
        List<String> aList = new ArrayList<String>(n);
        for (String x : s)
            aList.add(x);
 
        System.out.println("Created ArrayList is");
        for (String x : aList)
            System.out.println(x);
 
        // We can create LinkedList same way
    }
}

输出

Created ArrayList is
Geeks
for

2.使用ArrayList或LinkedList构造函数

我们可以简单地使用ArrayList或LinkedList的构造函数将Set转换成List。

// Java program to demonstrate conversion of
// Set to list using constructor
 
import java.util.*;
 
class Test {
    public static void main(String[] args)
    {
 
        // Creating a hash set of strings
        Set<String> s = new HashSet<String>();
        s.add("Geeks");
        s.add("for");
 
        // Creating an array list using constructor
        List<String> aList = new ArrayList<String>(s);
 
        System.out.println("Created ArrayList is");
        for (String x : aList)
            System.out.println(x);
 
        System.out.println("Created LinkedList is");
        List<String> lList = new LinkedList<String>(s);
        for (String x : lList)
            System.out.println(x);
    }
}

输出

Created ArrayList is
Geeks
for
Created LinkedList is
Geeks
for

3.使用addAll()方法

Java中Set到List的转换也可以使用Java List的addAll()方法来完成。

// Java program to demonstrate conversion of
// Set to array using addAll() method.
 
import java.util.*;
 
class Test {
    public static void main(String[] args)
    {
 
        // Creating a hash set of strings
        Set<String> s = new HashSet<String>();
        s.add("Geeks");
        s.add("for");
 
        List<String> aList = new ArrayList<String>();
        aList.addAll(s);
 
        System.out.println("Created ArrayList is");
        for (String x : aList)
            System.out.println(x);
 
        List<String> lList = new LinkedList<String>();
        lList.addAll(s);
 
        System.out.println("Created LinkedList is");
        for (String x : lList)
            System.out.println(x);
    }
}

输出

Created ArrayList is
Geeks
for
Created LinkedList is
Geeks
for

4.在Java中使用流

我们在Java中使用流,将给定的集合转换为蒸汽,然后再将流转换为列表。这只在Java 8或之后的版本中起作用。

// Java program to demonstrate conversion of
// Set to list using stream
 
import java.util.*;
import java.util.stream.*;
 
class Test {
    public static void main(String[] args)
    {
 
        // Creating a hash set of strings
        Set<String> s = new HashSet<String>();
        s.add("Geeks");
        s.add("for");
 
        List<String> aList
            = s.stream().collect(Collectors.toList());
 
        for (String x : aList)
            System.out.println(x);
    }
}

输出

Geeks
for

5.使用List.copyOf()方法

我们可以通过在java中使用List.copyOf(object)将集合转换为列表。为此,我们必须导入java.util.List.*包,它是一个静态的工厂方法,可以从另一个集合或对象创建列表对象。

import java.io.*;
import java.util.*;
 
class GFG {
    public static void main(String[] args)
    {
        Set<String> s = new HashSet<String>();
        s.add("geeks");
        s.add("forgeeks");
        s.add("learning");
        s.add("platform");
 
        List<String> aList = new ArrayList<>();
        aList = List.copyOf(s);
 
        System.out.println("Created ArrayList is :"
                           + aList);
    }
}

输出

Created ArrayList is :[geeks, forgeeks, learning, platform]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程