Java集合数据存储的总结

一、集合框架

集合是容纳数据的容器,java常用的集合体系图如下。以集合中是否运行重复元素来分,主要有List和Set接口,List集合中可以有重复元素,Set集合集合中的元素不可重复,Iterator和List Iterator是遍历集合的2个迭代器,Map是存储Key/Value键值对的容器。

这里写图片描述

java集合体系图

二、迭代器

迭代器的用法写在后面,这里说明Iterator和ListIterator的区别:

  1. Iterator在遍历一个集合的过程中不能修改集合中的对象,ListIterator可以修改对象
  2. ListIterator从后往前遍历,有hasPrevious()方法,Iterator没有
  3. ListIterator可以在遍历过程中进行添加元素操作,Iterator不能,否则会报java.util.ConcurrentModificationException异常。

三、List集合

List集合存储不唯一、有序的对象,可以操作角标。

3.1 ArrayList

3.1.1内部实现

ArrayList也叫变长数组,数组的长度是固定的,ArrayList可以随着元素的增多长度增加,内部实现为数组。ArrayList在添加元素时首先会判断长度,长度不够时,长度扩展到原来到1.5倍,原数组复制到新数组。

3.1.2关键属性

属性取值或结论
是否允许重复允许
是否有序有序
是否允许为空允许
是否线程安全
使用场景多查询、少增删

3.1.3 测试示例

创建一个ArrayList对象,添加5个元素,其中一个为null:

        arrayList.add("a1");
        arrayList.add("a2");
        arrayList.add(null);
        arrayList.add("a3");
        arrayList.add("a4");
        System.out.println(arrayList);
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

打印结果:

[a1, a2, null, a3, a4]
 
 
  • 1

使用迭代器for循环方式遍历:

        for (Iterator iter=arrayList.iterator();iter.hasNext();){
            System.out.println(iter.next());
        }
 
 
  • 1
  • 2
  • 3

使用迭代器while循环方式遍历:

        Iterator iter=arrayList.iterator();
        while (iter.hasNext()){
            System.out.println(iter.next());
        }
 
 
  • 1
  • 2
  • 3
  • 4

存储自定义对象:

public class Boy {

    private String name;
    private int age;
    //省略setter、getter、构造器和toString()方法
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

添加Boy对象到ArrayList中并打印:

 ArrayList<Boy> boys=new ArrayList<>();

        Boy b1=new Boy("Tom",12);
        Boy b2=new Boy("Jack",11);
        Boy b3=new Boy("Mike",15);

        boys.add(b1);
        boys.add(b2);
        boys.add(b3);

        System.out.println(boys);
        Iterator<Boy> iter=boys.iterator();
        while (iter.hasNext()){
            Boy b=iter.next();
            System.out.println(b.getName()+"----"+b.getAge());
        }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

结果:

[Boy{name='Tom', age=12}, Boy{name='Jack', age=11}, Boy{name='Mike', age=15}]
Tom----12
Jack----11
Mike----15

 
 
  • 1
  • 2
  • 3
  • 4
  • 5

3.1.4 转成线程安全

非线程安全的ArrayList

ArrayList<String> arrayList = new ArrayList();

 
 
  • 1
  • 2

线程安全的ArrayList

List arrayList =Collections.synchronizedList(new ArrayList<String>()) ;
 
 
  • 1

3.1.5 常用方法

  • clear():移除列表中的所有元素
  • contains(Object o): 包含指定元素
  • get(int index):返回列表中指定位置上的元素
  • indexOf(Object o): 返回列表中首次出现指定元素的索引,如果列表不包含,则返回-1
  • isEmpty():列表为空返回true
  • lastIndexOf(Object o):返回列表中最后一次出现指定元素的索引,如果列表不包含元素则返回-1
  • remove(int index):移除列表中指定位置的元素
  • remove(Object o):移除列表中首次出现的指定元素
  • removeRange(int fromIndex,int toIndex):移除列表中索引在fromIndex(包括)和toIndex(不包括)之间的所有元素。
  • size():返回集合的元素个数
  • set(int index,E element):修改指定位置上的元素
  • toArray():按顺序返回包含此列表中所有元素的数组

3.2 LinkedList

3.2.1内部实现

内部实现为双向链表,删除和增加速度快,查找速度慢。

3.2.2关键属性

属性取值或结论
是否允许重复允许
是否有序有序
是否允许为空允许
是否线程安全
使用场景多增删、少查询

3.2.3 测试示例

LinkedList当作FIFO的队列使用,也就是常用的add方法添加元素:

        LinkedList quene=new LinkedList();
        quene.add("a");
        quene.add("b");
        quene.add("c");
        quene.add("d");

        System.out.println("打印队列:"+quene);
        System.out.println("获取队头:"+quene.getFirst());
        System.out.println("获取队尾:"+quene.getLast());
        System.out.println("移除队头:"+quene.pop());
        System.out.println("移除队头之后的队列:"+quene);
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

打印结果:

打印队列:[a, b, c, d]
获取队头:a
获取队尾:d
移除队头:a
移除队头之后的队列:[b, c, d]
 
 
  • 1
  • 2
  • 3
  • 4
  • 5

LinkedList当作FILO的栈使用:

        LinkedList stack = new LinkedList();

        stack.push("1");
        stack.push("2");
        stack.push("3");
        stack.push("4");

        System.out.println("打印栈:"+stack);

        System.out.println("获取栈顶元素:"+stack.peek());
        System.out.println("打印栈:"+stack);
        System.out.println("取出栈顶元素:"+stack.pop());
        System.out.println("打印栈:"+stack);

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

打印结果:

打印栈:[4, 3, 2, 1]
获取栈顶元素:4
打印栈:[4, 3, 2, 1]
取出栈顶元素:4
打印栈:[3, 2, 1]
 
 
  • 1
  • 2
  • 3
  • 4
  • 5

除了ArrayList中包含的基本方法以为,LinkedList中多了getFirst()、getLast()、addFirst()、addLast()、peek()、peekFirst()、peekLast()、removeFirst()、removeLast()等方法。

3.2.4 转成线程安全

 List<Integer> linkedList=Collections.synchronizedList(new LinkedList<Integer>());
 
 
  • 1

3.3 Vector

Vector内部是数组结构,线程安全,速度较慢,几乎不用。

四、Set集合

Set集合中的元素唯一、无序,没有角标。

4.1HashSet

4.1.1内部实现

内部结构是哈希表,对象存入HashSet之前,要先确保对象重写equals()和hashCode()方法,这样才能比较对象的值是否相等,确保set中没有存储相同的对象。判断两个元素是否相同,首先通过hashCode()方法判断的是2个元素的哈希值是否相同,再根据equals()方法判断值是否相同,只有2者都相同才是统一元素。

4.1.2 基本属性

属性取值或结论
是否允许重复不允许
是否有序无序
是否允许为空允许(只有一个null)
是否线程安全
使用场景对象不重复和需要快速查找的场景

4.1.3 测试示例

        HashSet<String> hashSet=new HashSet();
        hashSet.add("abc");
        hashSet.add("张三");
        hashSet.add("李四");
        hashSet.add("tim");
        hashSet.add(null);
        hashSet.add(null);

        System.out.println("HashSet 大小:"+hashSet.size());
        Iterator iter=hashSet.iterator();
        while (iter.hasNext()){
            System.out.println(iter.next());
        }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

打印结果:

HashSet 大小:5
null
李四
张三
abc
tim
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

添加自定义对象,仍然添加Boy类中的对象

      Boy b1=new Boy("神乐",12);
      Boy b2=new Boy("神乐",12);
      HashSet<Boy> boys=new HashSet<>();
      boys.add(b1);
      boys.add(b2);
      System.out.println(boys);
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

结果:

[Boy{name='神乐', age=12}, Boy{name='神乐', age=12}]
 
 
  • 1

这时候b1和b2其实是一个对象,在Boy类中重写hashCode()和equals()方法:

public class Boy {

    private String name;
    private int age;
    //省略setter、getter、构造器和toString()方法
        @Override
    public int hashCode() {

        return name.hashCode()+age;
    }

    @Override
    public boolean equals(Object obj) {
        if (this==obj) return  true;
        if (!(obj instanceof Boy))  throw new ClassCastException("类型错误");
        Boy boy=(Boy) obj;

        return this.name.equals(boy.name)&&(this.age==boy.age);
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

重写equals和hashCode方法以后,上述集合中就会只添加一个对象:

[Boy{name='神乐', age=12}]
 
 
  • 1

4.1.4 转成线程安全

Set<String> hSet=Collections.synchronizedSet(new HashSet<String>());
 
 
  • 1

4.1.5 唯一且有序

LinkedHashSet集合中都元素唯一且有序,这里都有序是指添加顺序。

        LinkedHashSet<String> lHashSet= new LinkedHashSet<String>();
        lHashSet.add("abc");
        lHashSet.add("张三");
        lHashSet.add("李四");
        lHashSet.add("tim");
        lHashSet.add(null);
        iter=lHashSet.iterator();
        while (iter.hasNext()){
            System.out.println(iter.next());
        }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

打印结果:

abc
张三
李四
tim
null
 
 
  • 1
  • 2
  • 3
  • 4
  • 5

4.2 TreeSet

4.2.1 内部实现

TreeSet内部实现为二叉树,可以对元素进行排序

4.2.2 基本属性

属性取值或结论
是否允许重复不允许
是否有序无序
是否允许为空允许(只有一个null)
是否线程安全
使用场景去重且排序

4.2.3 测试示例


        Boy b1=new Boy("定春",16);
        Boy b2=new Boy("神乐",12);
        Boy b3=new Boy("桑巴",13);

        TreeSet<Boy> treeSet=new TreeSet<>(new Comparator<Boy>() {
            @Override
            public int compare(Boy o1, Boy o2) {
                return o1.getAge()-o2.getAge();
            }
        });

        treeSet.add(b1);
        treeSet.add(b2);
        treeSet.add(b3);
        System.out.println(treeSet);
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

打印结果:

[Boy{name='神乐', age=12}, Boy{name='桑巴', age=13}, Boy{name='定春', age=16}]
 
 
  • 1

4.2.4 转成线程安全

        TreeSet<Boy> treeSet=new TreeSet<>(new Comparator<Boy>() {
            @Override
            public int compare(Boy o1, Boy o2) {
                return o1.getAge()-o2.getAge();
            }
        });

        Set<Boy> treeSet1=Collections.synchronizedSet(treeSet);
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

五、Map集合

Map只存储的是键值对,Key必须唯一且不为空,key允许为null。

5.1HashMap

5.1.1基本属性

属性取值或结论
是否允许重复key重复会被覆盖,value可重复
是否有序无序
是否允许为空key和value都允许为空
是否线程安全

5.1.2 测试示例

词频统计:

        String[] arr={"Hadoop","Lucene","ES","ES","ES","Hadoop","Java","JS"};
        //TreeMap<String,Integer> map=new TreeMap<>();
        HashMap<String,Integer> map=new HashMap<>();
        for (String str:arr){
            if (map.containsKey(str)){
                map.put(str,map.get(str)+1);
            }else{
                map.put(str,1);
            }
        }

        Set<String> keys=map.keySet();
        Iterator<String> iter=keys.iterator();
        while (iter.hasNext()){
            String str=iter.next();
            System.out.println(str+"---"+map.get(str));
        }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

打印结果:

Java---1
Hadoop---2
JS---1
Lucene---1
ES---3
 
 
  • 1
  • 2
  • 3
  • 4
  • 5

如果想按value排序,可以改成TreeMap();

六、性能对比

ArrayList遍历性能对比

ArrayList的遍历可以用一般for循环、foreach循环、Iterator迭代器三种方式实现,为了测试它们的性能,先创建一个ArrayList对象,添加5万个字符串:

ArrayList<String > a = new ArrayList();
for (int i = 0; i < 60000; i++) {
    a.add(""+i);
}
 
 
  • 1
  • 2
  • 3
  • 4

for 循环打印并记录耗时:

long start = System.currentTimeMillis();

for (int i = 0; i < a.size(); i++) {
     System.out.print(a.get(i));
}

long end = System.currentTimeMillis();

System.out.println("\n下标for循环:" + (end - start));

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

结果:

150
 
 
  • 1

foreach循环打印并记录耗时:

 start = System.currentTimeMillis();

 for (String i : a) {
      System.out.print(i);
 }
 end = System.currentTimeMillis();

 System.out.println("\nforeach:" + (end - start));

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

耗时:

95
 
 
  • 1

Iterator循环打印:

start=System.currentTimeMillis();
Iterator iter=a.iterator();
while (iter.hasNext()){
     System.out.print(iter.next());
}
end=System.currentTimeMillis();
System.out.println("\nIter:"+(end-start));
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

耗时:

60
 
 
  • 1

结论:一般for 循环最慢、foreach次之、Iterator最快。

另外,Iterator遍历还有另外一种变形方式,效率和while形式一样。

for(Iterator it=a.iterator();it.hasNext();){
    System.out.print(it.next());
}
 
 
  • 1
  • 2
  • 3

七、参考文章

有些文章总结的很好,列出来供参考学习:
Java集合框架

  • 4
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值