Java 如何使用List创建TreeSet
TreeSet在Java中以有序的方式存储唯一元素。它实现了SortedSet接口。TreeSet接口在内部使用一种称为Red-Black tree的平衡树。List在Java中是一种用于按添加顺序存储元素的数据结构。我们可以使用多种方式在Java中使用List创建TreeSet。本文介绍了使用List在Java中创建TreeSet的方法。
使用List在Java中创建TreeSet的方法
有三种方法可以使用List在Java中创建TreeSet。它们如下:
- 从现有的列表中逐个将元素添加到TreeSet中。
-
创建一个TreeSet对象,通过构造函数初始化,并将列表传递给它。
-
使用Collections.addAll()方法将List中的元素添加到TreeSet中。
让我们深入了解这些方法。
从现有的列表中逐个将元素添加到TreeSet中
这是一种使用现有列表创建TreeSet的基本方法。首先,我们将创建一个列表并向其中添加元素。然后,我们将创建一个TreeSet对象,逐个从列表中添加元素。我们将遍历列表,并将列表的元素添加到TreeSet中。通过这种方式,可以使用List创建TreeSet。
示例
下面是一个示例,通过从现有列表逐个添加元素到TreeSet中来创建TreeSet:
import java.util.*;
public class Example{
public static void main(String args[])
{
//Create a new List
ArrayList<String> list = new ArrayList<String>();
list.add("India");
list.add("USA");
list.add("France");
list.add("India");
//Display ArrayList
System.out.println("The list is " + list);
//Create a TreeSet
TreeSet<String> ts= new TreeSet<String>();
//add elements to the TreeSet from the list one by one
for(String str:list){
ts.add(str);
}
// Print TreeSet
System.out.println("The TreeSet obtained from the list is " + ts);
}
}
输出
下面是上述代码的输出结果
The list is [India, USA, France, India]
The TreeSet obtained from the list is [France, India, USA]
解释
在上述代码中,我们创建了一个列表,添加了元素,并打印了同样的列表。列表可以包含重复的元素。然后我们创建了一个 TreeSet。我们通过遍历列表将列表的元素添加到 TreeSet 中。这样 TreeSet 将包含来自列表的唯一元素。
创建一个 TreeSet 对象,用构造函数进行初始化,并将列表传递给它
下一种方法是创建一个列表并向其添加元素。然后我们创建一个 TreeSet 对象并用构造函数进行初始化。然后,我们将列表作为参数传递给 TreeSet 的构造函数,以便将列表中的元素添加到 TreeSet 中。
示例
以下是一个通过将列表传递给 TreeSet 构造函数来创建 TreeSet 的示例-
import java.util.*;
public class Example{
public static void main(String args[])
{
//Create a new List
ArrayList<String> list = new ArrayList<String>();
list.add("India");
list.add("USA");
list.add("France");
list.add("India");
//Display ArrayList
System.out.println("The list is " + list);
//Create a TreeSet and pass list as a parameter
TreeSet<String> ts= new TreeSet<String>(list);
// Print TreeSet
System.out.println("The TreeSet obtained from the list is " + ts);
}
}
输出
以下是输出结果
The list is [India, USA, France, India]
The TreeSet obtained from the list is [France, India, USA]
解释
在上面的代码中,我们创建了一个列表并向其添加元素。接着创建了一个TreeSet对象,并将该列表作为参数传递给TreeSet构造函数。正如之前提到的,列表可以包含重复的元素,但是TreeSet不可以。然后我们打印了该列表。
addAll()方法
Java中的Collections类的addAll()方法用于将一个集合的元素添加到另一个集合中。因此,我们将创建一个列表,向其添加元素,并创建一个TreeSet。使用addAll()方法,我们将列表中的元素添加到TreeSet中。
示例
以下是一个使用addAll()方法从列表创建TreeSet的示例-
import java.util.*;
public class Example{
public static void main(String args[])
{
//Create a new List
ArrayList<String> list = new ArrayList<String>();
list.add("India");
list.add("USA");
list.add("France");
list.add("India");
//Display ArrayList
System.out.println("The list is " + list);
//Create a TreeSet and pass list as a parameter
TreeSet<String> ts= new TreeSet<String>(list);
//addAll method
ts.addAll(list);
// Print TreeSet
System.out.println("The TreeSet obtained from the list is " + ts);
}
}
输出
以下是上述代码的输出结果
The list is [India, USA, France, India]
The TreeSet obtained from the list is [France, India, USA]
解释
在上面的代码中,我们创建了一个列表,向其中添加了元素,并创建了一个TreeSet。因此,addAll()方法将会将列表中的元素添加到TreeSet中。然后,我们打印了TreeSet。