Java中将ArrayList转换为HashSet
要将ArrayList转换为HashSet,首先创建一个ArrayList-
List<String> l = new ArrayList<String>();
向ArrayList-添加元素
l.add("Accent");
l.add("Speech");
l.add("Diction");
l.add("Tone");
l.add("Pronunciation");
现在将ArrayList转换为HashSet-
Set<String> s = new HashSet<String>(l);
下面是一个在Java中将ArrayList转换为HashSet的示例。
示例
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
List<String> l = new ArrayList<String>();
l.add("Accent");
l.add("Speech");
l.add("Diction");
l.add("Tone");
l.add("Pronunciation");
Set<String> s = new HashSet<String>(l);
System.out.println("HashSet elements...");
for (Object ob : s)
System.out.println(ob);
}
}
输出结果HashSet elements...
Tone
Pronunciation
Accent
Speech
Diction
热门推荐
10 妈妈给宝贝祝福语简短
11 嫂子生日英语祝福语简短
12 空军大学开学祝福语简短
13 同事离别祝福语简短的
14 学生祝福语简短古诗大全
15 女神节日简短祝福语大全
16 优秀男士祝福语大全简短
17 娶嫂子的祝福语简短
18 大寿爸爸祝福语简短英文