集合的归纳

本文详细介绍了Java集合框架,包括Collection接口、List接口、Set接口及其具体实现类如ArrayList、LinkedList、HashSet、TreeSet等。还讨论了Map接口及其实现类HashMap、TreeMap、Properties等,强调了各个接口的主要API和特性,以及如何进行排序和线程安全。此外,提到了Iterator接口和Collections、Arrays工具类在操作集合和数组时的作用。
摘要由CSDN通过智能技术生成

一方面,面向对象语言对事务的体现都是以对象的形式,为了方便多个对象的操作,就要对对象进行存储。另一方面,使用Array存储对象具有一些弊端,而Java集合就像一种容器,可以动态的把多个对象的引用存入集合中

1.1 Java集合的组织结构

 

集合工具类

  • Collections
  • Arrays

排序接口

  • Comparable
  • Comparator

1.2 Collection接口

主要API:

  • size()返回集合长度

  • add(Object object) 添加 返回boolean

  • addAll(Collection collection) 把集合中的所有元素添加到另一个集合里

  • isEmpty() 返回boolea是否为空

  • clear() 清空集合

  • contains(Object obj) 集合是否包含某对象

  • containsALL(Collection coll) 是否包含集合中的所有元素

  • retainAll(Collection coll) 保留两个集合共同的元素,交集

  • remove(Object obj) 移除一个元素

  • equals()

  • hashcode()

  • toArray() 将集合转化为数组

  • iterator() 返回Iterator接口实现类的对象,遍历集合对象

public class TestCollection {

    @Test
    public void testCollcetion1() {
        //因为Collection是接口,所以用List接口下的实现类 ArrayList来创建对象
        Collection collection = new ArrayList();

        //1.size()返回集合长度
        System.out.println(collection.size());

        //2.add(Object object)添加  返回boolean
        collection.add(123);
        collection.add("AA");
        collection.add(new Date());
        collection.add("BB");

        //3.addAll(Collection collection) 把集合中的所有元素添加到另一个集合里
        //Arrays数组转集合
        Collection collection2 = Arrays.asList(1, 2, 3);
        collection.addAll(collection2);
        System.out.println(collection.size());

        System.out.println(collection);

        //4.isEmpty() 返回boolea是否为空
        System.out.println(collection.isEmpty());

        //5.clear() 清空集合
        collection.clear();
        System.out.println(collection.isEmpty());
    }

    @Test
    public void testCollection2() {
        Collection collection = new ArrayList();
        collection.add(123);
        collection.add("AA");
        collection.add(new Date());
        collection.add("BB");
        collection.add(new Person("班", 18));

        //6.contains(Object obj)
        //根据元素所在类的equals方法,重写,判断成员变量的值而不是判断地址值
        boolean flag = collection.contains(new Person("班", 18));
        System.out.println(flag);

        //7.containsALL(Collection coll) 是否包含集合中的所有元素
        Collection collection2 = new ArrayList();
        collection2.add(123);
        collection2.add(new String("AA"));
        boolean flag2 = collection.containsAll(collection2);
        System.out.println(flag2);

        //8 retainAll(Collection coll)  保留两个集合共同的元素,交集
        //结果返回到调用集合
        collection.retainAll(collection2);
        System.out.println(collection);    // [123, AA]

        //9.remove(Object obj)
        boolean flag4 = collection.remove("bb");
        System.out.println(flag4);

        //10.removeAll(Collection coll)
        boolean flag5 = collection.retainAll(collection2);

        //11.equals

        //12.hashcode() 计算集合中的Hash值
        System.out.println(collection.hashCode());

        //13.toArray 将集合转化为数组
        Object[] obj = collection.toArray();
        for(Object o:obj){
            System.out.println(o);
        }

        //14.iterator() 返回Iterator接口实现类的对象,遍历集合对象
        Iterator iterator = collection.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}

 1.3 List接口

List也是接口,继承自Collection接口,Collection的15个方法皆可用

  • 元素有序可重复
  • ArrayList(动态数组):List的主要实现类
    • 基于数组实现,增删慢,查询快,线程不安全
  • LinkedList(链表):对于频繁的插入和删除建议选择
    • 基于双向链表实现,增删快,查询慢,线程不安全
  • Vector: 古老的实现类,线程安全
    • 基于数组实现,增删慢,查询快,线程安全
    • 为了线程安全损失了性能,总体上性能不如ArrayList

      List集合里添加了一些根据索引来操作集合元素的方法

    • void add(int index,Object ele) 在指定的位置添加

    • boolean addALL(int index,Collection eles) 在指定位置加上好多元素,以集合的方式传入

    • Object get(int index) 根据下标获得一个元素

    • int indexOf(Object obj) 获得一个元素的下标

    • int lastIndexOf(Object obj) 返回最后一个元素的下标
    • Object remove(int index) 删除指定位置的元素
    • Object set(int index,Object ele) 设定指定位置的元素变成一个新值
    • List subList(int fromIndex,int toIndex) 返回从fromIndex到 toIndex之间的子list !左闭右开
    • List常用方法

    • 增 add or 插入 add(index,Object obj)
    • 删 remove 元素/下标
    • 改 set
    • 查 get(index)
      @Test
      public void testList1() {
          //List是接口 , ArrayList是实现类
          List list = new ArrayList();
          list.add(123
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值