下面是一个小例子,细节决定成败,与君共勉。
import java.util.Collection; import java.util.HashSet; public class CollectionInterface { public static void main(String[] args) { int[] arrays = new int[]{1,2,3,4,5,6,7,8,9,10,1,2,3,4,5}; Collection<Integer> set = new HashSet<Integer>(); int counter = 0; for(int temp :arrays){ if(set.add(temp)){ counter++; }else{ System.out.println("The date "+temp+" has already been existent,discard it."); } } System.out.println("counter=> "+counter); System.out.println("set.size()=> "+set.size()); } }
CoreJava_Collection接口中的add是有返回值的!,布布扣,bubuko.com
CoreJava_Collection接口中的add是有返回值的!
原文:http://blog.csdn.net/ziwen00/article/details/38660125