1 package jihe; 2 3 import java.util.*; 4 5 public class Test { 6 7 public static void main(String[] args) 8 { 9 //创建set类的实例10 Setset = new TreeSet<>();11 12 //添加元素13 set.add("A");14 set.add("a");15 set.add("c");16 set.add("C");17 set.add("a");18 19 //遍历集合20 for(String x : set)21 {22 System.out.println("set = " + x);23 }24 25 26 27 28 29 //创建List类的实例30 List list = new ArrayList<>();31 32 //添加元素33 list.add("A");34 list.add("a");35 list.add("c");36 list.add("C");37 list.add("a");38 39 //遍历集合40 for(String x : list)41 {42 System.out.println("list = " + x);43 }44 45 46 }47 48 }
运行结果
set = A
set = Cset = aset = clist = Alist = alist = clist = Clist = a