ArrayList新new法,以前没这么写过

List<String> l1 = new ArrayList<String>();
  l1.add("a");
  l1.add("a");
  l1.add("b");
  l1.add("b");
  l1.add("c");
  l1.add("c");
  l1.add("d");
  l1.add("d");
  
  //()括号里直接把l1传进去,s1就有了l1的元素
  Set<String> s1 = new LinkedHashSet<String>(l1);
  
  //同理l2里有了s1的元素
  List<String> l2 = new ArrayList<String>(s1);
  
  System.out.println(l1);
  System.out.println(l2);

output:

[a, a, b, b, c, c, d, d]
[a, b, c, d]

 

你可能感兴趣的:(ArrayList新new法,以前没这么写过)