反射认识_06_ArrayList_HashSet区别
包01:
package ReflectionCollection; public class ReflectionConstructorPoint { private int x; public int y; public ReflectionConstructorPoint(int x, int y) { super(); this.x = x; this.y = y; } }
包02:
package ReflectionCollection; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; public class ReflectionConstructor { public static void main(String[] args) throws Exception { ReflectionConstructorPoint rcp1=new ReflectionConstructorPoint(1, 1); ReflectionConstructorPoint rcp2=new ReflectionConstructorPoint(2, 2); ReflectionConstructorPoint rcp3=new ReflectionConstructorPoint(3, 3); /*验证ArrayList,重复添加后大小,有序集合*/ Collection col1=new ArrayList(); col1.add(rcp1); col1.add(rcp1);//第二次添加 col1.add(rcp2); col1.add(rcp3); System.out.println(col1.size());//结果为4 /*验证HashSet,重复添加后大小,无序集合*/ Collection col2=new HashSet(); col2.add(rcp1); col2.add(rcp1);//第二次添加 col2.add(rcp2); col2.add(rcp3); System.out.println(col2.size());//结果为3 } }