Set集合重复性?

今天学习了一下集合类的知识,练习Set的时候发现

1*Set集合允许重复的值插入,但是重复的值会被覆盖;

2*List集合允许重复的值插入,但是重复的值会不会被覆盖;

import java.util.*;
public class Test14702 {

    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int len=in.nextInt();
        int ar[]=new int[len];
        Set<Integer> s=new TreeSet<>();
        for(int i=0;i<4;i++) {
            int d=in.nextInt();
            s.add(d);
            ar[i]=d;
        }
        Iterator<Integer> it=s.iterator();
        System.out.println("All Sets:");
        while(it.hasNext()) {
            System.out.println(it.next());
        }
    }
}

console:

4
7 7 3 2
All Sets:
2
3
7

原文地址:https://www.cnblogs.com/RorinL/p/12907257.html