美文网首页
Java集合-1

Java集合-1

作者: Statham_Jessie | 来源:发表于2020-02-14 23:40 被阅读0次

    概述

        为了保存数量不定的数据,和具有映射关系的数据,Java提供了一系列的集合类。集合类也可以称为容器类,主要保存其他数据,主要在java.util下。还有基于多线程安全的容器在java.util.concurrent包下。

    Java的容器包括Set/Queue/List/SortedSet、HashSet/Deque/PriorityQueue/ArrayList/Vector/TreeSet/LinkedHashSet/ArrayQueue/LinkedList/Stack.

    Collection类

    Collection是List、Set、Queue的父接口

    Java8为Iterable接口新增了forEach方法,该方法所需方法是一个函数式接口。所以可以使用lambda表达式遍历集合。

    Collection list=new HashSet();

    list.add(“dd”);

    list.add(“aa”);

    list.forEach(t->System.out.println(t));

    使用Iterator遍历集合

    Iterator it=list.iterator();

    while(it.hasNext()){

    String st=(String)it.next();

    }

    HashSet 类

    HashSet类是set类的实现,它使用哈希算法类存储元素,具有很好的存储和查找性能。

    HashSet类具有如下特点:

    存储顺序与添加顺序未必相同了;

    不是线程安全的;

    元素可以为null;

    LinkedHashSet类

    LinkedHashSet类也是通过哈希算法来存取数据,但内部还维护一个链表来保持添加顺序。因此插入性能会受影响,但遍历所有元素会按插入顺序并有很好的性能。

    相关文章

      网友评论

          本文标题:Java集合-1

          本文链接:https://www.haomeiwen.com/subject/xfpsfhtx.html