集合(Collections)

1. 概念

因为数组存在弊端,为了方便多个对象的内存存储,可以动态的把多个对象的引用放入容器中

  • 初始化以后,其长度就不可修改。
  • 数组中提供的方法非常有限,对添加、删除、插入数据等操作,非常不便,同时效率不高。
  • 获取数组实际元素的个数的需求,数组没有现成的属或方法可用
  • 数组存储数据的特点:有序、可重复,对于无序、不可重复的需求,不能满足

Java中,集合类的基本接口是Collection接口和Map接口

请添加图片描述

2. Collection接口

单列数据

2.1. List

有序、可重复 (Vector、ArrayList、LinkedList)

  1. Vector:List 接口的古老的实现类,线程安全,效率低,底层使用Object[ ]数组存储

  2. ArrayList:List 接口的主要实现类,线程不安全,效率低,底层使用Object[ ]数组存储

  3. LinkedList:底层使用双向链表存储,对于频繁的插入删除操作使用此类效率比较高

public static void main(String[] args) {

    Collection collection = new ArrayList();

    collection.add("AA");
    collection.add("BB");
    collection.add(123);//自动装箱
    collection.add(new Date());

    Collection collection1 = new ArrayList();
    collection1.add("CC");
    collection1.add(456);//自动装箱
    collection1.add(new Persion("Tom",20));

    //添加其它集合
    collection.addAll(collection1);

    //判断当前集合中是否存在相应的对象值
    System.out.println(collection.contains(456));
    System.out.println("***此时需重写Persion类中的equals方法才是true***"+collection.contains(new Persion("Tom", 20)));


    Collection collection2 = new ArrayList();
    collection2.add("CC");
    collection2.add(456);
    collection2.add(new Persion("Tom",20));

    System.out.println("判断collection2中的所有对象是否在collection1中"+collection1.containsAll(collection2));

    System.out.println("返回是否移除数据成功"+collection2.remove(456));
    System.out.println(collection2);

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

    //判断当前集合是否为空
    System.out.println(collection.isEmpty());//false

    collection.clear();//清空集合元素
    System.out.println(collection.isEmpty());//true


}
  Collection collection = new ArrayList();

        collection.add("AA");
        collection.add("BB");
        collection.add(123);
        collection.add(new Date());

// Iterator迭代器接口实现集合元素遍历,每次调用都需要新创建iterator对象
        Iterator iterator = collection.iterator();

        for (int i = 0; i < collection.size(); i++) {
            System.out.println(iterator.next());
        }
//       hasNext指针下移,判断是否还存在下一个元素
        while (iterator.hasNext()){
            System.out.println("开发中常用"+iterator.next());
        }

// 集合--->数组

        for (Object o : collection.toArray()) {
            System.out.println(o);
        }
// 数组--->集合
        String[] strs = {"AA","BB","CC"};
        List<String> list = Arrays.asList(strs);
        System.out.println(list);

2.1.1 源码分析

// 底层创建长度为10的Object[] elementData
ArrayList arrayList = new ArrayList();
arrayList.add(123);//相当于elementData[0] = new Integer(123);
arrayList.add(123456);//若此时elementData数组长度不够,则按照原容量的1.5倍进行扩容,并将原数组的数据复制到扩容后的数组

/*
 * 建议开发中确定长度后使用带参构造器,尽量避免扩容来提高效率
 **/
ArrayList list = new ArrayList();

list.add(123);
list.add(456);
//按照索引添加
list.add(1,"AA");

System.out.println(list+"长度是:"+list.size());//[123, 456, AA]长度是:3

List list1 = Arrays.asList(1, 2, 3);
list.addAll(list1);//添加集合中的元素
System.out.println(list.size());

//按照索引获取
System.out.println(list.get(1));
//返回此元素首次+末次出现的索引,不存在返回-1,
System.out.println(list.indexOf(123)+" "+list.lastIndexOf(123));
//指定索引位置元素值
list.set(1,"tylt");
//截取一定索引区间的元素[左闭右开)
System.out.println(list.subList(1, 3));

2.2. Set

无序、不可重复 (HashSet、LinkedHashSet、TreeSet)

  1. HashSet:Set接口的主要实现类,线程不安全,可以存储null,底层是数组+链表结构,其实是个HashMap

  2. LinkedHashSet:HashSet的子类,可按照添加的顺序进行遍历(非有序)因为多了双向链表进行指向,效率高于HashSet

  3. TreeSet:底层使用红黑树结构,可以按照添加的对象按执行属性排序,要求数据必须是同一类型的,例如String Integer

无序性不等同于随机性,无序性在添加时不是按照数组索引的方式添加(线性探测进入),而是按照数据的Hash值进行位置确定,遍历时依旧按照原加入顺序输出

不可重复性就是添加的元素按照equals方法比较,即相同元素唯一

3. Map接口

双列数据,具有映射关系key-value(键值对)

  • Map中的key是无序的、不可重复的,使用Set存储所有key;value也是无序的、不可重复的,使用Collection存储所有的value
  • 一个键值对构成了一个Entry对象,是无序的不可重复的,使用Set存储,所以自定义的类要重写equals和hashcode方法
  1. HashMap:作为Map接口的主要实现类,线程不安全的,效率高,可存储null的key和value,底层是数组+链表+红黑树

  2. LinkedHashMap:HashMap的子类,双向链表结构,可以按照顺序实现频繁遍历操作,效率高

  3. TreeMap:存储有序键值对数据,可实现按照key排序,所以要求key必须由同一类创建,底层使用红黑树

  4. Hashtable:古老的实现类,线程安全

  5. Properties:Hashtable子类,常用来处理配置文件,key,value都是String类型

HashMap map = new HashMap();
//Insert
map.put("A", 1);
map.put("B", 1);
map.put("C", 2);
map.put("D", 3);

//Update
map.put("C", 10);

HashMap map1 = new HashMap();
map.put("E", 4);
map.put("F", 5);
map.putAll(map1);

System.out.println(map);//{A=1, B=1, C=10, D=3, E=4, F=5}

//是否包含指定的key
boolean containsKey = map.containsKey("A");
//是否包含指定的value
boolean containsValue = map.containsValue(10);


//Map的遍历
//遍历所有的key集合
Set keySet = map.keySet();
Iterator iterator = keySet.iterator();
while (iterator.hasNext()) {
    Object key = iterator.next();
    System.out.println(key+"--"+map.get(key));
}

//遍历所有的value集合
Collection values = map.values();
for (Object obj : values) {
    System.out.println(obj);
}
//遍历匹配好的key-value
Set entrySet = map.entrySet();
for (Object obj : entrySet) {
    System.out.println(obj);

//强转后分别获取
    Map.Entry entry = (Map.Entry) obj;
    System.out.println(entry.getKey()+"---->"+entry.getValue());
}

3.1. 源码分析

  • HashMap 底层实现原理
//底层创建长度为16的Entry[] table数组
HashMap map = new HashMap();
//调用key所在类的hashCode方法计算哈希值,经过某种算法得到Entry数组中的存放位置,
//若此位置为空则存放,不为空则进行哈希值比较,若不相同则添加成功,若相同的话则调用key所在类的equals方法比较
//若equals返回false则添加成功,返回true则进行value替换
map.put("key","value");
/*
 * JDK 8.0 中底层数组是Node[],在首次调用put时才会创建长度为16的数组
 * 当数组的某一个索引位置上的元素以链表形式存在的个数 >8 且当前数组长度 >64时,此索引位置上的所有数据改为红黑树存储
 **/
  • Properties 配置文件的理解
   FileInputStream fileInputStream = null;
        try {
            Properties properties = new Properties();
            fileInputStream = new FileInputStream("test.properties");
            properties.load(fileInputStream);
            String name = properties.getProperty("name");
            String password = properties.getProperty("password");

            System.out.println("名称:" + name + "---密码:" + password);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

4. Collections工具类

操作Set、List、Map的静态工具类

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天宇龙腾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值