集合之Collection

集合之Collection

/**
* @author ccchai
* @create 2022-02-22 14:11
*
*
* 1.数组在存储多个数据方面的缺点:
*     > 一旦初始化以后,其长度就不可修改
*     > 数组中提供的方法非常有限,对于添加、删除、修改、插入数据等操作,非常不便,同时效率也不高。
*     > 获取数组中实际元素的个数的需求,数组没有现成的属性或方法可用
*     > 数组存储数据的特点:有序、可重复。对于无序、不可重复的需求,不能满足
*
* 2.集合框架
*     |----Collection接口:单列集合,用来存储一个一个大的对象
*         |----List接口:存储有序的、可重复的数据。 --> "动态"数组
*             |----ArrayList、LinkedList、Vector
*
*         |----Set接口:存储无序的、不可重复的数据   --> 高中讲的"集合"
*             |----HashSet、LinkedHashSet、TreeSet
*     |----Map接口:双列集合,用来存储一对(key - value)一对的数据 -->高中函数: y=f(x)
*         |----HashMap、LinkedHashMap、TreeMap、HashTable、Properties
*/
public class CollectionTest {

   @Test
   public void test(){
       Collection coll = new ArrayList();
       //add(Object e):将元素e添加到集合coll中
       coll.add("abc");
       coll.add("hello");
       coll.add(123);//自动装箱
       coll.add(new Date());

       //size():获取添加的元素的个数
       System.out.println(coll.size());//4

       Collection coll1 = new ArrayList();
       coll1.add("AA");
       coll1.add(33);

       //size():获取添加的元素的个数
       coll.addAll(coll1);
       System.out.println(coll.size());//6
       System.out.println(coll.toString());//[abc, hello, 123, Tue Feb 22 14:41:11 CST 2022, AA, 33]

       //clear():清空集合中的元素
       coll.clear();

       //isEmpty():判断当前集合是否为空
       System.out.println(coll.isEmpty());
  }

   /*
   向Collection接口的实现类的对象中添加数据obj时,要求obj所在类要重写equals()
    */
   @Test
   public void test2(){
       Collection coll = new ArrayList();
       coll.add(123);
       coll.add(456);
       coll.add(false);
       coll.add(new String("hello"));
       coll.add(new Person("Mike",24));

       //1.contains():判断但前集合中是否包含obj
       //我们在判断时会调用obj对象所在类的equals()
       boolean contains = coll.contains(123);
       System.out.println(contains);//true

       System.out.println(coll.contains(new String("hello")));//true
       System.out.println(coll.contains(new Person("Mike",24)));//true

       //2.containsAll(Collection coll):判断形参coll中的所有元素是否都存在于当前集合中
       Collection coll1 = Arrays.asList(123,4567);
       System.out.println(coll.containsAll(coll1));//false
  }

   @Test
   public void test3(){
       Collection coll = new ArrayList();
       coll.add(123);
       coll.add(456);
       coll.add(false);
       coll.add(new String("hello"));
       coll.add(new Person("Mike",24));

       //3.remove(Object obj):从当前集合中移除obj元素
       coll.remove(1234);
       System.out.println(coll);
       coll.remove(new Person("Mike",24));
       System.out.println(coll);

       //4.removeAll(Collection coll):差集:从当前集合中移除coll1中所有的元素
       Collection coll1 = Arrays.asList(123,456);
       coll.removeAll(coll1);
       System.out.println(coll);


  }

   @Test
   public void test4(){
       Collection coll = new ArrayList();
       coll.add(123);
       coll.add(456);
       coll.add(false);
       coll.add(new String("hello"));
       coll.add(new Person("Mike",24));

       Collection coll1 = Arrays.asList(123,456);

       //5.retainAll(Collection coll):交集:获取当前集合和coll1集合的交集,并返回给当前集合
       coll.retainAll(coll1);
       System.out.println(coll);//[123, 456]

       Collection coll2 = new ArrayList();
       coll2.add(456);
       coll2.add(123);
       coll2.add(false);
       coll2.add(new String("hello"));
       coll2.add(new Person("Mike",24));

       //6.equals(Object obj):要想返回true,需要当前集合和形参集合的元素都相同,顺序也要相同
       System.out.println(coll.equals(coll2));//false,ArrayList是有序的,顺序不一样也返回false
  }

   @Test
   public void test5(){
       Collection coll = new ArrayList();
       coll.add(123);
       coll.add(456);
       coll.add(false);
       coll.add(new String("hello"));
       coll.add(new Person("Mike",24));
       //7.hashCode():返回当前对象的哈希值
       System.out.println(coll.hashCode());

       //8.集合 ---> 数组:toArray()
       Object[] array = coll.toArray();
       for (int i = 0; i < array.length; i++) {
           System.out.println(array[i]);
      }

       //数组 --> 集合:调用Arrays类的静态方法asList()
       List<String> list = Arrays.asList(new String[]{"AA", "BB", "CC"});
       System.out.println(list.toString());

       //注意:
       List list1 = Arrays.asList(new int[]{123, 456});
       System.out.println(list1.size());//1

       List list2 = Arrays.asList(new Integer[]{123, 456});
       System.out.println(list2.size());//2

  }
}
posted @ 2022-02-23 11:04  阳光真好的博客  阅读(171)  评论(0)    收藏  举报