Java | 集合(Collection)和迭代器(Iterator)

集合(Collection)

集合就是Java中提供的一种 空器,可以用来存储多个数据。
集合和数组都是一个容器,它们有什么区别呢?

  • 数组的长度是固定的,集合的长度是可变的。
  • 数组中存储的是同一类型的数据,并且也可以存储基本类型数据,但是集合中存储的都是对象,并且没有泛型的时候,还可以存储不同的对象。

Collection集合框架

Collection是一个集合类的顶级接口,所有的集合类的接口都是他的子类,或者实现类。

List接口:

1、在序的集合
2、允许存储生活重复的元素
3、有索引,可以使用普通的for循环遍历

set接口:

1、不允许有重复的元素
2、没有索引,不能进行普通的for循环
3、LinkedHashSet是有序的集合

Collection里面的方法

为什么要学习Collection里面的方法,是因为在用所有的Collection的实现类的时候,都可以用这些方法,所以学习这些方法就相当于学习了所有子类的方法,在以后学习子类方法的时候,只用学习一部分子类特有的就行了。

public boolean add(E e); 把给定的对象添加到当前集合中
public void clear(E e);清空集合中所有的元素
public boolean remove(E e);把给定的对象在当前集合中删除
public boolean contains(E e);判断当前集合中是否包含给定对象
public boolean isEmpty();判断当前集合是否为空
public int size();返回集合中元素的个数
public Object[] toArray();把集合中的元素,存储到数组中

使用:
创建一个集合:
public boolean add(E e);` 把给定的对象添加到当前集合中

    //使用多态
    Collection<String> coll = new ArrayList<>();
    coll.add("a");
    coll.add("b");
    coll.add("c");
    coll.add("d");
    //输出
    System.out.println(coll);   //[a, b, c, d]

public void clear(E e);清空集合中所有的元素

    coll.clear();
    System.out.println(coll);   //[]

public boolean remove(E e);把给定的对象在当前集合中删除

    coll.remove("a");
    System.out.println(coll);   //[b, c, d]

public boolean contains(E e);判断当前集合中是否包含给定对象

    //包含返回true,没有返回false
    System.out.println(coll.contains("c")); //true

public boolean isEmpty();判断当前集合是否为空

    //判断当前集合是否为空,如果为空则为true,否则为false
    System.out.println(coll.isEmpty());     //false

public int size();返回集合中元素的个数

    System.out.println(coll.size());    //4

public T[] toArray(T[] a);把集合中的元素,存储到数组中

    //创建一个数组,这个数组的长度就是集合的长度
    String[] strArr = new String[coll.size()];
    //然后把集合中的元素全都存到数组中
    coll.toArray(strArr);
    for (String s : strArr) {
        System.out.println(s);
        /*  循环输出:
            a
            b
            c
            d
         */
    }

迭代器(Iterator)

我们在开发中经常需要遍厉集合,所以JDK专门提供了一个接口java.util.Iterator,这个接口的作用主要是用来迭代访问Collection中的元素的,所以换为迭代器。

迭代器(Iterator)的原理和使用

因为Collection继承了Iterator,所以可以使用Collection里面的iterator(),来获取迭代器。
最初的迭器的使用:

    //使用多态
    Collection<String> coll = new ArrayList<>();
    coll.add("a");
    coll.add("b");
    coll.add("c");
    coll.add("d");
    System.out.println(coll);   //[a, b, c, d]
    //通过集合获取迭代器
    Iterator<String> iterator = coll.iterator();
    //用hasNext()方法来判断有没有下一个元素,如果有返回true
    while (iterator.hasNext()) {
        //用next()来获取下一个元素
        //并且下标也移动到了当前元素下面,以方便上面的判断 
        String s = iterator.next();
        System.out.println(s);
        /*  迭代的结果:
            a
            b
            c
            d
         */
    }

使用增强for循环:foreach来遍历集合,其实foreach里面也是使用Iterator来实现的。

    for (String s : coll) {
        System.out.println(s);
        /*  循环的结果:
            a
            b
            c
            d
         */
    }

使用Java8里面的新特性Lambda表达式循环集合,其实这个里面封装的是增强for循环。

    coll.forEach(x -> System.out.println(x));
    /*  循环的结果:
        a
        b
        c
        d
     */

forEach里面的方法:

    default void forEach(Consumer<? super T> action) {
        Objects.requireNonNull(action);
        for (T t : this) {
            action.accept(t);
        }
    }

这个方法是在Iterator里面,是一个默认方法,因为Collection继承了这个方法,所以可以使用这个方法。

集合工具(Collections)

java.utils.Collections集合工具类,就是用来简化对集合进行操作的。

常用的方法

  • pubic stativc <T> boolean addAll(Collection<T> c,T... elements);往集合中批量添加元素。
  • public static void shuffle(List<?> list); 乱序。
  • public static <T> void sort(List<T> list); 将集合中的元素按昭默认规则排序。
  • public static <T> void sort(List<T> list, Comparator<? super T>);将集合中的元素按照指定规则进行排序。

使用

pubic stativc <T> boolean addAll(Collection<T> c,T... elements);往集合中批量添加元素。

    List<String> list = new ArrayList<>();
    Collections.addAll(list, "a", "b", "c", "d");
    System.out.println(list);   //[a, b, c, d]

public static void shuffle(List<?> list); 乱序。

    List<String> list = new ArrayList<>();
    Collections.addAll(list, "a", "b", "c", "d");
    System.out.println(list);   //[a, b, c, d]
    Collections.shuffle(list);
    System.out.println(list);   //[d, b, a, c]随机的

public static <T> void sort(List<T> list); 将集合中的元素按昭默认规则排序。

    List<String> list = new ArrayList<>();
    //无序的
    Collections.addAll(list, "b", "d", "g", "o");
    Collections.sort(list);
    System.out.println(list);   //[b, d, g, o]

public static <T> void sort(List<T> list, Comparator<? super T>);将集合中的元素按照指定规则进行排序。

    List<Integer> list = new ArrayList<>();
    list.add(9);
    list.add(3);
    list.add(4);
    list.add(2);
    Collections.sort(list, new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            //return o2 - o1; //[9, 4, 3, 2] 降序
            return o1 - o2;   //[2, 3, 4, 9] 升序
        }
    });
    System.out.println(list);


关注公众号,随时获取最新资讯

细节决定成败!
个人愚见,如有不对,恳请斧正!

posted @ 2020-04-02 13:34  一点浩然气~  阅读(808)  评论(0编辑  收藏  举报