list接口它的实现类,比如arraylist里面的值有序,并且可以重复。(有序指的是插入进去的顺序)

set无序,且不可重复。(这里的无序就是指不是插入进去的顺序,但其实也不是真的无序,它会按照自己的逻辑进行排序,比如hashset会按照hash值进行排序,treeset会按照自然顺序进行排序)

list  set都可以插入null

public static void test(){
List a = new ArrayList();
a.add(2);
a.add(1);
a.add(3);
a.add(4);
a.add(4);
a.add(null);
a.add(null);
// for(int i=0; i<a.size(); i++){
// System.out.println(a.get(i));
// }
// for(Object i : a){
// System.out.println(i);
// }
Iterator it = a.iterator();
while(it.hasNext()){
Object i = it.next();
System.out.println(i);
}
}

输出:

2
1
3
4
4
null
null

 

public static void testSet(){
Set a = new HashSet();
a.add("123");
a.add("124");
a.add("125");
a.add("126");
// for(Object i : a){
// System.out.println(i);
// }
Iterator it = a.iterator();
while(it.hasNext()){
Object i = it.next();
System.out.println(i);
}
}

输出:

125
126
123
124

 

public static void testTreeSet(){
Set a = new TreeSet();
a.add("123");
a.add("124");
a.add("125");
a.add("126");
// for(Object i : a){
// System.out.println(i);
// }
Iterator it = a.iterator();
while(it.hasNext()){
Object i = it.next();
System.out.println(i);
}
}

输出:

123
124
125
126

 

public static void testTreeSet(){
Set a = new TreeSet();
a.add("124");
a.add("123");
a.add("125");
a.add("126");
// for(Object i : a){
// System.out.println(i);
// }
Iterator it = a.iterator();
while(it.hasNext()){
Object i = it.next();
System.out.println(i);
}
}

输出:

123
124
125
126

相关文章:

  • 2022-12-23
  • 2022-03-01
  • 2021-08-07
  • 2022-12-23
  • 2021-07-17
  • 2021-07-22
  • 2021-10-28
  • 2021-04-24
猜你喜欢
  • 2022-01-18
  • 2021-12-01
  • 2022-01-14
  • 2022-12-23
  • 2022-02-04
  • 2022-01-09
  • 2021-10-27
相关资源
相似解决方案