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]

 

转载于:https://www.cnblogs.com/doudou-123/p/9505766.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值