package ArrayList;
import java.util.*;
public class SimpleCollection {
public static void main(String[] args) {
Collection<Integer> c = new ArrayList<Integer>();
//Set<Integer> c = new HashSet<Integer>();
for(int i=0;i<10;i++)
c.add(i);
c.add(9);
for(Integer i:c)
if(i == 10)
System.out.print(i);
else {
System.out.print(i+", ");
}
}
}
输出结果:
Collection:0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9,
Set:0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
显然Collection是将一个新元素放置到Collection中,允许重复。而Set只有元素不存在的情况下才会添加,所以不允许重复