Day15_集合与迭代器(Collection,Iterator迭代器,List接口,Set接口)

Day15 集合与迭代器

学习目标

  • 能够熟练使用Collection集合的API
  • 能够使用Iterator迭代器遍历Collection系列的集合
  • 能够使用foreach遍历Collection系列的集合
  • 能够说出foreach循环与Iterator迭代器的联系与区别
  • 掌握三种遍历集合的方式
  • 掌握三种Collection集合的元素删除方式
  • 理解迭代器Iterator的工作原理
  • 了解迭代器Iterator的快速失败机制

1 Collection集合

1.1 集合的概念

集合是java中提供的一种容器,可以用来存储多个数据。

集合和数组既然都是容器,它们有啥区别呢?

  • 数组的长度是固定的。集合的长度是可变的。
  • 数组中可以存储基本数据类型值,也可以存储对象,而集合中只能存储对象

集合主要分为两大系列:Collection和Map,Collection 表示一组对象,Map表示一组映射关系或键值对。

1.2 Collection接口

Collection 层次结构中的根接口。Collection 表示一组对象,这些对象也称为 collection 的元素。一些 collection 允许有重复的元素,而另一些则不允许。一些 collection 是有序的,而另一些则是无序的。JDK 不提供此接口的任何直接实现:它提供更具体的子接口(如 Set 和 List、Queue)实现。此接口通常用来传递 collection,并在需要最大普遍性的地方操作这些 collection。

Collection是所有单列集合的父接口,因此在Collection中定义了单列集合(List和Set)通用的一些方法,这些方法可用于操作所有的单列集合。方法如下:

1、添加元素

(1)add(E obj):添加元素对象到当前集合中

(2)addAll(Collection<? extends E> other):添加other集合中的所有元素对象到当前集合中,即this = this ∪ other

2、删除元素

(1) boolean remove(Object obj) :从当前集合中删除第一个找到的与obj对象equals返回true的元素。

(2)boolean removeAll(Collection<?> coll):从当前集合中删除所有与coll集合中相同的元素。即this = this - this ∩ coll

(3)boolean retainAll(Collection<?> coll):从当前集合中删除两个集合中不同的元素,使得当前集合仅保留与c集合中的元素相同的元素,即当前集合中仅保留两个集合的交集,即this = this ∩ coll;

(4)void clear():清空集合

(5)boolean removeIf(Predicate<? super E> filter) :删除满足给定条件的此集合的所有元素。removeIf方法是Java8引入的。

3、查询与获取元素

(1)boolean isEmpty():判断当前集合是否为空集合。

(2)boolean contains(Object obj):判断当前集合中是否存在一个与obj对象equals返回true的元素。

(3)boolean containsAll(Collection<?> c):判断c集合中的元素是否在当前集合中都存在。即c集合是否是当前集合的“子集”。

(4)int size():获取当前集合中实际存储的元素个数

(5)Object[] toArray():返回包含当前集合中所有元素的数组

1.3 API演示

1、演示添加

注意:add和addAll的区别

package com.atguigu.collection;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;

public class TestCollectionAdd {
    @Test
    public void testAdd(){
        //ArrayList是Collection的子接口List的实现类之一。
        Collection coll = new ArrayList();
        coll.add("小李广");
        coll.add("扫地僧");
        coll.add("石破天");
        System.out.println(coll);
    }

    @Test
    public void testAddAll(){
        Collection c1 = new ArrayList();
        c1.add(1);
        c1.add(2);
        System.out.println("c1集合元素的个数:" + c1.size());//2
        System.out.println("c1 = " + c1);

        Collection c2 = new ArrayList();
        c2.add(1);
        c2.add(2);
        System.out.println("c2集合元素的个数:" + c2.size());//2
        System.out.println("c2 = " + c2);

        Collection other = new ArrayList();
        other.add(1);
        other.add(2);
        other.add(3);
        System.out.println("other集合元素的个数:" + other.size());//3
        System.out.println("other = " + other);
        System.out.println();

        c1.addAll(other);
        System.out.println("c1集合元素的个数:" + c1.size());//5
        System.out.println("c1.addAll(other) = " + c1);

        c2.add(other);
        System.out.println("c2集合元素的个数:" + c2.size());
        System.out.println("c2.add(other) = " + c2);
    }
}

注意:coll.addAll(other);与coll.add(other);

在这里插入图片描述

2、演示删除

注意几种删除方法的区别

package com.atguigu.collection;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Predicate;

public class TestCollectionRemove {
    @Test
    public void test01(){
        Collection coll = new ArrayList();
        coll.add("小李广");
        coll.add("扫地僧");
        coll.add("石破天");
        coll.add("佛地魔");
        System.out.println("coll = " + coll);

        coll.remove("小李广");
        System.out.println("删除元素\"小李广\"之后coll = " + coll);

        coll.removeIf(new Predicate() {
            @Override
            public boolean test(Object o) {
                String str = (String) o;
                return str.contains("地");
            }
        });
        System.out.println("删除包含\"地\"字的元素之后coll = " + coll);

        coll.clear();
        System.out.println("coll清空之后,coll = " + coll);
    }

    @Test
    public void test02() {
        Collection coll = new ArrayList();
        coll.add("小李广");
        coll.add("扫地僧");
        coll.add("石破天");
        coll.add("佛地魔");
        System.out.println("coll = " + coll);

        Collection other = new ArrayList();
        other.add("小李广");
        other.add("扫地僧");
        other.add("尚硅谷");
        System.out.println("other = " + other);

        coll.removeAll(other);
        System.out.println("coll.removeAll(other)之后,coll = " + coll);
        System.out.println("coll.removeAll(other)之后,other = " + other);
    }

    @Test
    public void test03() {
        Collection coll = new ArrayList();
        coll.add("小李广");
        coll.add("扫地僧");
        coll.add("石破天");
        coll.add("佛地魔");
        System.out.println("coll = " + coll);

        Collection other = new ArrayList();
        other.add("小李广");
        other.add("扫地僧");
        other.add("尚硅谷");
        System.out.println("other = " + other);

        coll.retainAll(other);
        System.out.println("coll.retainAll(other)之后,coll = " + coll);
        System.out.println("coll.retainAll(other)之后,other = " + other);
    }

}
3、演示查询与获取元素
package com.atguigu.collection;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

public class TestCollectionContains {
    @Test
    public void test01() {
        Collection coll = new ArrayList();
        System.out.println("coll在添加元素之前,isEmpty = " + coll.isEmpty());
        coll.add("小李广");
        coll.add("扫地僧");
        coll.add("石破天");
        coll.add("佛地魔");
        System.out.println("coll的元素个数" + coll.size());
        Object[] objects = coll.toArray();
        System.out.println("用数组返回coll中所有元素:" + Arrays.toString(objects));
        System.out.println("coll在添加元素之后,isEmpty = " + coll.isEmpty());
        coll.clear();
        System.out.println("coll在clear之后,isEmpty = " + coll.isEmpty());
    }

    @Test
    public void test02() {
        Collection coll = new ArrayList();
        coll.add("小李广");
        coll.add("扫地僧");
        coll.add("石破天");
        coll.add("佛地魔");
        System.out.println("coll = " + coll);
        System.out.println("coll是否包含“小李广” = " + coll.contains("小李广"));
        System.out.println("coll是否包含“宋红康” = " + coll.contains("宋红康"));

        Collection other = new ArrayList();
        other.add("小李广");
        other.add("扫地僧");
        other.add("尚硅谷");
        System.out.println("other = " + other);

        System.out.println("coll.containsAll(other) = " + coll.containsAll(other));
    }

    @Test
    public void test03(){
        Collection c1 = new ArrayList();
        c1.add(1);
        c1.add(2);
        System.out.println("c1集合元素的个数:" + c1.size());//2
        System.out.println("c1 = " + c1);

        Collection c2 = new ArrayList();
        c2.add(1);
        c2.add(2);
        System.out.println("c2集合元素的个数:" + c2.size());//2
        System.out.println("c2 = " + c2);

        Collection other = new ArrayList();
        other.add(1);
        other.add(2);
        other.add(3);
        System.out.println("other集合元素的个数:" + other.size());//3
        System.out.println("other = " + other);
        System.out.println();

        c1.addAll(other);
        System.out.println("c1集合元素的个数:" + c1.size());//5
        System.out.println("c1.addAll(other) = " + c1);
        System.out.println("c1.contains(other) = " + c1.contains(other));
        System.out.println("c1.containsAll(other) = " + c1.containsAll(other));
        System.out.println();

        c2.add(other);
        System.out.println("c2集合元素的个数:" + c2.size());
        System.out.println("c2.add(other) = " + c2);
        System.out.println("c2.contains(other) = " + c2.contains(other));
        System.out.println("c2.containsAll(other) = " + c2.containsAll(other));
    }

}

2 Iterator迭代器

2.1 Iterator接口

在程序开发中,经常需要遍历集合中的所有元素。针对这种需求,JDK专门提供了一个接口java.util.IteratorIterator接口也是Java集合中的一员,但它与CollectionMap接口有所不同,Collection接口与Map接口主要用于存储元素,而Iterator主要用于迭代访问(即遍历)Collection中的元素,因此Iterator对象也被称为迭代器。

想要遍历Collection集合,那么就要获取该集合迭代器完成迭代操作,下面介绍一下获取迭代器的方法:

  • public Iterator iterator(): 获取集合对应的迭代器,用来遍历集合中的元素的。

下面介绍一下迭代的概念:

  • 迭代:即Collection集合元素的通用获取方式。在取元素之前先要判断集合中有没有元素,如果有,就把这个元素取出来,继续在判断,如果还有就再取出出来。直到把集合中的所有元素全部取出。这种取出方式专业术语称为迭代。

Iterator接口的常用方法如下:

  • public E next():返回迭代的下一个元素。
  • public boolean hasNext():如果仍有元素可以迭代,则返回 true。

接下来我们通过案例学习如何使用Iterator迭代集合中元素:

package com.atguigu.iterator;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class TestIterator {
    @Test
    public void test01(){
        Collection coll = new ArrayList();
        coll.add("小李广");
        coll.add("扫地僧");
        coll.add("石破天");

        Iterator iterator = coll.iterator();
        System.out.println(iterator.next());
        System.out.println(iterator.next());
        System.out.println(iterator.next());
        System.out.println(iterator.next());
    }

    @Test
    public void test02(){
        Collection coll = new ArrayList();
        coll.add("小李广");
        coll.add("扫地僧");
        coll.add("石破天");

        Iterator iterator = coll.iterator();//获取迭代器对象
        while(iterator.hasNext()) {//判断是否还有元素可迭代
            System.out.println(iterator.next());//取出下一个元素
        }
    }
}

提示:在进行集合元素取出时,如果集合中已经没有元素了,还继续使用迭代器的next方法,将会发生java.util.NoSuchElementException没有集合元素的错误。

2.2 迭代器的实现原理

我们在之前案例已经完成了Iterator遍历集合的整个过程。当遍历集合时,首先通过调用集合的iterator()方法获得迭代器对象,然后使用hashNext()方法判断集合中是否存在下一个元素,如果存在,则调用next()方法将元素取出,否则说明已到达了集合末尾,停止遍历元素。

Iterator迭代器对象在遍历集合时,内部采用指针的方式来跟踪集合中的元素,为了让初学者能更好地理解迭代器的工作原理,接下来通过一个图例来演示Iterator对象迭代元素的过程:

在这里插入图片描述

在调用Iterator的next方法之前,迭代器指向第一个元素,当第一次调用迭代器的next方法时,返回第一个元素,然后迭代器的索引会向后移动一位,指向第二个元素,当再次调用next方法时,返回第二个元素,然后迭代器的索引会再向后移动一位,指向第三个元素,依此类推,直到hasNext方法返回false,表示到达了集合的末尾,终止对元素的遍历。

2.3 Iterable接口

1、 Iterable接口依赖Iterator接口

java.lang.Iterable接口的抽象方法:

  • public Iterator iterator(): 获取对应的迭代器,用来遍历集合中的元素的。

凡是实现了 Iterable接口的集合,就必须重写 iterator()方法,即还必须为该集合提供一个Iterator接口的实现类,否则就无法完成该方法的重写。

2、forEach方法

java.lang.Iterable接口在Java8还提供了一个forEach默认方法:

  • public default void forEach(Consumer<? super T> action):传入Consumer接口的实现类对象,完成集合元素的迭代

java.util.function.Consumer接口的抽象方法:

  • void accept(T t):对元素t执行给定的操作
package com.atguigu.iter;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Consumer;

public class TestForEachMethod {
    @Test
    public void test1(){
        Collection coll = new ArrayList();
        coll.add("小李广");
        coll.add("扫地僧");
        coll.add("石破天");

        coll.forEach(new Consumer() {
            @Override
            public void accept(Object o) {
                System.out.println(o);
            }
        });
    }
}

3、新语法糖:foreach循环(增强for循环)

Java5(JDK1.5)中增加了java.lang.Iterable接口,实现这个接口允许对象成为 “foreach” 语句的目标。 Java 5时Collection接口继承了java.lang.Iterable接口,因此Collection系列的集合就可以直接使用foreach循环遍历。

foreach循环的语法格式:

for(元素类型 元素名 : 集合名等){
}
//这里元素名就是一个临时变量,自己命名就可以

对于集合类型来说,foreach循环其实就是使用Iterator迭代器来完成元素的遍历的。

package com.atguigu.iterator;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;

public class TestForeach {
    @Test
    public void test01(){
        Collection coll = new ArrayList();
        coll.add("小李广");
        coll.add("扫地僧");
        coll.add("石破天");

        for (Object o : coll) {
            System.out.println(o);
        }
    }
}

在这里插入图片描述

Java中的数组也支持这种语法糖。只不过编译器在处理foreach遍历数组时,是转换为普通for循环的。

代码示例:

package com.atguigu.api;

public class TestForeach {
    public static void main(String[] args) {
        int[] nums = {1,2,3,4,5};
        for (int num : nums) {
            System.out.println(num);
        }
        System.out.println("-----------------");
        String[] names = {"张三","李四","王五"};
        for (String name : names) {
            System.out.println(name);
        }
    }
}

2.4 使用Iterator迭代器删除元素

java.util.Iterator迭代器中有一个方法:

​ void remove() ;

那么,既然Collection已经有remove(xx)方法了,为什么Iterator迭代器还要提供删除方法呢?

因为在JDK1.8之前Collection接口没有removeIf方法,即无法根据条件删除。

例如:要删除以下集合元素中的偶数

package com.atguigu.iterator;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class TestIteratorRemove {
    @Test
    public void test01(){
        Collection coll = new ArrayList();
        coll.add(1);
        coll.add(2);
        coll.add(3);
        coll.add(4);

//		coll.remove(?)//没有removeIf方法无法实现删除“偶数”

        Iterator iterator = coll.iterator();
        while(iterator.hasNext()){
            Integer element = (Integer) iterator.next();
            if(element%2 == 0){
                iterator.remove();
            }
        }
        System.out.println(coll);
    }
}

2.5 Iterator迭代器的快速失败(fail-fast)机制

如果在Iterator、ListIterator迭代器创建后的任意时间从结构上修改了集合(通过迭代器自身的 remove 或 add 方法之外的任何其他方式),则迭代器将抛出 ConcurrentModificationException。因此,面对并发的修改,迭代器很快就完全失败,而不是冒着在将来不确定的时间任意发生不确定行为的风险。

这样设计是因为,迭代器代表集合中某个元素的位置,内部会存储某些能够代表该位置的信息。当集合发生改变时,该信息的含义可能会发生变化,这时操作迭代器就可能会造成不可预料的事情。因此,果断抛异常阻止,是最好的方法。这就是Iterator迭代器的快速失败(fail-fast)机制。

1、ConcurrentModificationException异常
package com.atguigu.iterator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class TestConcurrentModificationException {
    public static void main(String[] args) {
        Collection coll = new ArrayList();
        coll.add("hello");
        coll.add("world");
        coll.add("java");
        coll.add("haha");
        coll.add("mysql");

        Iterator iterator = coll.iterator();
        while(iterator.hasNext()){
            String str = (String)iterator.next();
            if(str.contains("a")){
                coll.remove(str);//foreach遍历集合过程中,调用集合的remove方法
            }
        }

        /*for (Object o : coll) {
            String str = (String) o;
            if(str.contains("a")){
                coll.remove(o);//foreach遍历集合过程中,调用集合的remove方法
            }
        }*/
    }
}
2、modCount变量

那么迭代器如何实现快速失败(fail-fast)机制的呢?

  • 在ArrayList等集合类中都有一个modCount变量。它用来记录集合的结构被修改的次数。
  • 当我们给集合添加和删除操作时,会导致modCount++。
  • 然后当我们用Iterator迭代器遍历集合时,创建集合迭代器的对象时,用一个变量记录当前集合的modCount。例如:int expectedModCount = modCount;,并且在迭代器每次next()迭代元素时,都要检查 expectedModCount != modCount,如果不相等了,那么说明你调用了Iterator迭代器以外的Collection的add,remove等方法,修改了集合的结构,使得modCount++,值变了,就会抛出ConcurrentModificationException。

下面以AbstractList和ArrayList.Itr迭代器为例进行源码分析:

AbstractList类中声明了modCount变量:

    /**
     * The number of times this list has been <i>structurally modified</i>.
     * Structural modifications are those that change the size of the
     * list, or otherwise perturb it in such a fashion that iterations in
     * progress may yield incorrect results.
     *
     * <p>This field is used by the iterator and list iterator implementation
     * returned by the {@code iterator} and {@code listIterator} methods.
     * If the value of this field changes unexpectedly, the iterator (or list
     * iterator) will throw a {@code ConcurrentModificationException} in
     * response to the {@code next}, {@code remove}, {@code previous},
     * {@code set} or {@code add} operations.  This provides
     * <i>fail-fast</i> behavior, rather than non-deterministic behavior in
     * the face of concurrent modification during iteration.
     *
     * <p><b>Use of this field by subclasses is optional.</b> If a subclass
     * wishes to provide fail-fast iterators (and list iterators), then it
     * merely has to increment this field in its {@code add(int, E)} and
     * {@code remove(int)} methods (and any other methods that it overrides
     * that result in structural modifications to the list).  A single call to
     * {@code add(int, E)} or {@code remove(int)} must add no more than
     * one to this field, or the iterators (and list iterators) will throw
     * bogus {@code ConcurrentModificationExceptions}.  If an implementation
     * does not wish to provide fail-fast iterators, this field may be
     * ignored.
     */
    protected transient int modCount = 0;

翻译解释:modCount是这个list被结构性修改的次数。子类使用这个字段是可选的,如果子类希望提供fail-fast迭代器,它仅仅需要在add(int, E),remove(int)方法(或者它重写的其他任何会结构性修改这个列表的方法)中添加这个字段。调用一次add(int,E)或者remove(int)方法时必须且仅仅给这个字段加1,否则迭代器会抛出伪装的ConcurrentModificationExceptions错误。如果一个实现类不希望提供fail-fast迭代器,则可以忽略这个字段。

Arraylist的Itr迭代器:

   private class Itr implements Iterator<E> {
        int cursor;      
        int lastRet = -1; 
        int expectedModCount = modCount;//在创建迭代器时,expectedModCount初始化为当前集合的modCount的值

        public boolean hasNext() {
            return cursor != size;
        }

        @SuppressWarnings("unchecked")
        public E next() {
            checkForComodification();//校验expectedModCount与modCount是否相等
            int i = cursor;
            if (i >= size)
                throw new NoSuchElementException();
            Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length)
                throw new ConcurrentModificationException();
            cursor = i + 1;
            return (E) elementData[lastRet = i];
        }
       	final void checkForComodification() {
            if (modCount != expectedModCount)//校验expectedModCount与modCount是否相等
                throw new ConcurrentModificationException();//不相等,抛异常
        }
}

ArrayList的remove方法:

    public boolean remove(Object o) {
        if (o == null) {
            for (int index = 0; index < size; index++)
                if (elementData[index] == null) {
                    fastRemove(index);
                    return true;
                }
        } else {
            for (int index = 0; index < size; index++)
                if (o.equals(elementData[index])) {
                    fastRemove(index);
                    return true;
                }
        }
        return false;
    }


    private void fastRemove(int index) {
        modCount++;
        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
        elementData[--size] = null; // clear to let GC do its work
    }

在这里插入图片描述

注意,迭代器的快速失败行为不能得到保证,一般来说,存在不同步的并发修改时,不可能作出任何坚决的保证。快速失败迭代器尽最大努力抛出 ConcurrentModificationException。因此,编写依赖于此异常的程序的方式是错误的,正确做法是:*迭代器的快速失败行为应该仅用于检测 bug。*例如:

package com.atguigu.iterator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class TestNoConcurrentModificationException {
    public static void main(String[] args) {
        Collection coll = new ArrayList();
        coll.add("hello");
        coll.add("world");
        coll.add("java");
        coll.add("haha");

        Iterator iterator = coll.iterator();
        while (iterator.hasNext()) {
            String str = (String) iterator.next();
            if (str.contains("a")) {
                coll.remove(str);
                //Iterator遍历集合过程中,调用集合的remove方法
            }
        }
    }
}

在这里插入图片描述

3 元素要求

3.1 有序、可重复

Collection 层次结构中的根接口。一些 collection 允许有重复的元素,而另一些则不允许。一些 collection 是有序的,而另一些则是无序的。JDK 不提供此接口的任何直接实现:它提供更具体的子接口(如 Set 和 List、Queue)实现。 我们掌握了Collection接口的使用后,再来看看Collection接口中的子接口,他们都具备那些特性呢?

1、List接口介绍

java.util.List接口继承自Collection接口,是单列集合的一个重要分支,习惯性地会将实现了List接口的对象称为List集合。

List的常用实现类有:ArrayList、Vector、LinkedList、Stack等。

List接口特点:

  • List集合所有的元素是以一种线性方式进行存储的
  • 它是一个元素存取有序的集合。即元素的存入顺序和取出顺序有保证。
  • 它是一个带有索引的集合,通过索引就可以精确的操作集合中的元素(与数组的索引是一个道理)。
  • 集合中可以有重复的元素

List集合类中元素有序、且可重复。这就像银行门口客服,给每一个来办理业务的客户分配序号:第一个来的是“张三”,客服给他分配的是0;第二个来的是“李四”,客服给他分配的1;以此类推,最后一个序号应该是“总人数-1”。

在这里插入图片描述

注意:

List集合关心元素是否有序,而不关心是否重复,请大家记住这个原则。例如“张三”可以领取两个号。

在JavaSE中List名称的类型有两个,一个是java.util.List集合接口,一个是java.awt.List图形界面的组件,别导错包了。

2、List接口中常用方法

List作为Collection集合的子接口,不但继承了Collection接口中的全部方法,而且还增加了一些根据元素索引来操作集合的特有方法,如下:

List除了从Collection集合继承的方法外,List 集合里添加了一些根据索引来操作集合元素的方法。

1、添加元素

  • void add(int index, E ele):把元素添加到指定位置
  • boolean addAll(int index, Collection<? extends E> eles):把一组元素添加到指定位置

2、删除元素

  • E remove(int index):删除指定位置的元素

3、修改元素

  • E set(int index, E ele):替换[index]位置的元素
  • default void replaceAll(UnaryOperator operator):按指定操作的要求替换元素

4、获取元素

  • E get(int index):返回[index]位置的元素

  • List subList(int fromIndex, int toIndex):返回[fromIndex, toIndex)范围的元素

  • int indexOf(Object obj):查询obj在列表中的位置,如果有重复,返回第1个

  • int lastIndexOf(Object obj):查询obj在列表中的位置,如果有重复,返回最后1个

示例代码:

package com.atguigu.list;

import org.junit.Test;

import java.util.*;
import java.util.function.UnaryOperator;

public class TestListMethod {

    @Test
    public void test08(){
        /*
        演示foreach和Iterator迭代器
         */
        List list = new ArrayList();
        list.add("hello");
        list.add("java");
        list.add("world");
        list.add("mysql");
        list.add("java");

        Iterator iterator = list.iterator();
        while(iterator.hasNext()){
            Object next = iterator.next();
            System.out.println(next);
        }
    }

    @Test
    public void test07(){
        /*
        演示foreach和Iterator迭代器
         */
        List list = new ArrayList();
        list.add("hello");
        list.add("java");
        list.add("world");
        list.add("mysql");
        list.add("java");

        for (Object o : list) {
            System.out.println(o);
        }
    }

    @Test
    public void test6() {
         /*
        ArrayList是List接口的实现类.
        演示查询,截取
         */
        List list = new ArrayList();
        list.add("hello");
        list.add("java");
        list.add("world");
        list.add("mysql");
        list.add("java");

        System.out.println(list.get(1));//java
        System.out.println(list.indexOf("java"));//1
        System.out.println(list.lastIndexOf("java"));//4

        List subList = list.subList(1, 3);//[1,3)下标范围的元素
        System.out.println("subList = " + subList);//[java, world]

    }

    @Test
    public void test5(){
        List list = new ArrayList();
        list.add("hello");
        list.add("java");
        list.add("world");
        list.add("mysql");
        list.add("java");

        list.replaceAll(new UnaryOperator() {
            @Override
            public Object apply(Object o) {
                return "java".equals(o) ? "atguigu" : o;
            }
        });

        System.out.println(list);
    }

    @Test
    public void test4(){
         /*
        ArrayList是List接口的实现类。
        演示修改替换
         */
        List list = new ArrayList();
        list.add("hello");
        list.add("java");

        list.set(0,"hi");
        System.out.println(list);//[hi, java]
    }
    @Test
    public void test03(){
       /*
        ArrayList是List接口的实现类。
        演示删除
         */
        List list =new ArrayList();
        list.add(10);
        list.add(20);
        list.add(30);
        list.add(40);

        list.remove(1);//删除[1]位置的元素
        System.out.println(list);//[10, 30, 40]

        //list.remove(30);//删除元素值为30的元素
        list.remove(Integer.valueOf(30));//手动装箱
        System.out.println(list);//[10, 40]

    }

    @Test
    public void test2(){
         /*
        ArrayList是List接口的实现类。
        演示删除
         */
        List<String> list = new ArrayList<>();
        list.add("hello");
        list.add("java");

        list.remove(0);
        System.out.println(list);
    }

    @Test
    public void test01(){
        /*
        ArrayList是List接口的实现类。
        演示添加
         */
        List list = new ArrayList();
        list.add("hello");
        list.add("java");

        list.add(0,"atguigu");
        list.add(2,"world");

        System.out.println(list);//[atguigu, hello, world, java]


        List list2 = Arrays.asList("chai", "lin", "yan");

        list.addAll(1,list2);
        System.out.println(list);//[atguigu, chai, lin, yan, hello, world, java]

    }
}

3、ListIterator迭代器

List 集合额外提供了一个 listIterator() 方法,该方法返回一个 ListIterator 列表迭代器对象, ListIterator 接口继承了 Iterator 接口,提供了专门操作 List 的方法:

  • void add():通过迭代器添加元素到对应集合
  • void set(Object obj):通过迭代器替换正迭代的元素
  • void remove():通过迭代器删除刚迭代的元素
  • boolean hasPrevious():如果以逆向遍历列表,往前是否还有元素。
  • Object previous():返回列表中的前一个元素。
  • int previousIndex():返回列表中的前一个元素的索引
  • boolean hasNext()
  • Object next()
  • int nextIndex()
package com.atguigu.list;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class TestListIterator {
    @Test
    public void test7() {
         /*
        ArrayList是List接口的实现类。
        演示ListIterator迭代器
         */
        List list = new ArrayList();
        list.add("hello");
        list.add("java");
        list.add("world");
        list.add("mysql");
        list.add("java");

        ListIterator stringListIterator = list.listIterator();
        while(stringListIterator.hasNext()){
            int index = stringListIterator.nextIndex();
            Object next = stringListIterator.next();

            if(next.equals("java")){
                stringListIterator.set("JavaEE");
            }
        }

        System.out.println(list);//[hello, JavaEE, world, mysql, JavaEE]
    }

    @Test
    public void test6() {
         /*
        ArrayList是List接口的实现类。
        演示ListIterator迭代器
         */
        List list = new ArrayList();
        list.add("hello");
        list.add("java");
        list.add("world");
        list.add("mysql");
        list.add("java");

        ListIterator stringListIterator = list.listIterator();
        while(stringListIterator.hasNext()){
            int index = stringListIterator.nextIndex();
            Object next = stringListIterator.next();
            System.out.println("index = " + index +",next = " + next);
        }

        System.out.println("---------------");
        while(stringListIterator.hasPrevious()){
            int index = stringListIterator.previousIndex();
            Object previous = stringListIterator.previous();
            System.out.println("index = " + index +",previous = " + previous);
        }

        System.out.println("---------------");
        stringListIterator = list.listIterator(2);
        while(stringListIterator.hasNext()){
            int index = stringListIterator.nextIndex();
            Object next = stringListIterator.next();
            System.out.println("index = " + index +",next = " + next);
        }
    }
}

3.2 无序、不可重复

1、Set接口介绍

Set接口是Collection的子接口,set接口没有提供额外的方法。但是比Collection接口更加严格了。

Set 集合不允许包含相同的元素,即元素不可重复。

Set集合支持的遍历方式和Collection集合一样:foreach和Iterator。

Set的常用实现类有:HashSet、TreeSet、LinkedHashSet等。

package com.atguigu.list;

import org.junit.Test;

import java.util.HashSet;

public class TestSet {
    @Test
    public void test02(){
        HashSet set = new HashSet();
        set.add("hello");
        set.add("java");
        set.add("hello");
        set.add("java");
        System.out.println(set);
    }

    @Test
    public void test01(){
        HashSet set = new HashSet();
        set.add(1);
        set.add(1);
        set.add(2);
        set.add(2);
        System.out.println(set);
    }
}
2、元素相等和重复问题:equals和hashCode方法

HashSet和LinkedHashSet元素不可重复是依赖于equals和hashCode方法

package com.atguigu.set;

public class Circle {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    @Override
    public String toString() {
        return "Circle{" +
                "radius=" + radius +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Circle circle = (Circle) o;

        return Double.compare(circle.radius, radius) == 0;
    }

    @Override
    public int hashCode() {
        long temp = Double.doubleToLongBits(radius);
        return (int) (temp ^ (temp >>> 32));
    }
}

 @Test
    public void test08(){
        HashSet<Circle> set = new HashSet<>();
        set.add(new Circle(1));
        set.add(new Circle(1));
        System.out.println(set);
    }
3、大小顺序和重复问题:Comparable和Comparator接口

TreeSet元素不可重复是依赖于元素大小关系的。

(1)要么元素类型实现Comparable接口,重写compareTo方法,

(2)要么创建TreeSet时,指定Comparator接口实现类对象,重写compare方法

package com.atguigu.set;

public class Rectangle implements Comparable<Rectangle> {
    private double length;
    private double width;

    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    public double getLength() {
        return length;
    }

    public void setLength(double length) {
        this.length = length;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double area(){
        return length * width;
    }

    @Override
    public String toString() {
        return "Rectangle{" +
                "length=" + length +
                ", width=" + width +
                ", area=" + area() +
                '}';
    }

    @Override
    public int compareTo(Rectangle o) {
        return Double.compare(this.area(), o.area());
//        int result = Double.compare(this.length, o.length);
//        return result != 0 ? result : Double.compare(this.width, o.width);
    }
}

   @Test
    public void test06(){
        TreeSet<Rectangle> set = new TreeSet<>();
        set.add(new Rectangle(5,3));
        set.add(new Rectangle(4,2));
        set.add(new Rectangle(6,1));
        set.add(new Rectangle(3,2));//进不去,因为Rectangle里面的compareTo方法按照面积比较大小
                                                //认为面积相同的就是相同的矩形对象

        System.out.println(set);
        /*'
        [Rectangle{length=6.0, width=1.0, area=6.0}, Rectangle{length=4.0, width=2.0, area=8.0}, Rectangle{length=5.0, width=3.0, area=15.0}]
         */
    }

    @Test
    public void test05(){
        TreeSet<Circle> set = new TreeSet<>(new Comparator<Circle>() {
            @Override
            public int compare(Circle o1, Circle o2) {
                return Double.compare(o1.getRadius(),o2.getRadius());
            }
        });
        set.add(new Circle(1.5));
        set.add(new Circle(1.0));
        set.add(new Circle(2.3));

        System.out.println(set);
        //[Circle{radius=1.0}, Circle{radius=1.5}, Circle{radius=2.3}]
    }
  • 18
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值