List Set 区别

List和Set中 我们要熟记:

List:有序,重复

有序:指按照一定的顺序输出。||   重复:指可以向list中添加相同的值

Set:无序,唯一

无序:指输出是没有顺序。  ||  唯一:指不添加可以向set中添加相同的元素,如果你添加相同的元素,最后输出的结果也是唯一的。

如下例子:

public class Demo{
@SuppressWarnings("unchecked")
public static void main(String[] args) {
People p1=new People("AA",144,12.6);
People p2=new People("BB",1,16.6);
People p3=new People("CC",13,11.6);
People p4=new People("DD",2,10.3);
HashSet<People> set=new HashSet<People>();
set.add(p4);
set.add(p4);
set.add(p1);
set.add(p3);
for (People people : set) {
System.out.println(people);
    }
  }
}

 

控制台结果:体现出无序和唯一的特性

[name:CC,age:13,height:11.6]
[name:DD,age:2,height:10.3]
[name:AA,age:144,height:12.6]

 

public class Demo{
@SuppressWarnings("unchecked")
public static void main(String[] args) {
People p1=new People("AA",144,12.6);
People p2=new People("BB",1,16.6);
People p3=new People("CC",13,11.6);
People p4=new People("DD",2,10.3);
ArrayList<People> list=new ArrayList<People>();
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);
list.add(p4);
list.add(p4);
list.add(p4);
for (People people : list) {
System.out.println(people);
    }
  }
}

控制台结果:体现有序,重复的特性

[name:AA,age:144,height:12.6]
[name:BB,age:1,height:16.6]
[name:CC,age:13,height:11.6]
[name:DD,age:2,height:10.3]
[name:DD,age:2,height:10.3]
[name:DD,age:2,height:10.3]
[name:DD,age:2,height:10.3]

 

posted @ 2018-08-20 15:26  呼丿  阅读(5617)  评论(0编辑  收藏  举报