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]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM