集合的遍历迭代**

    科技2024-03-23  85

    集合遍历/迭代专题**

    注意:以下遍历/迭代方式,是所有Collection通用的一种方式。在Map集合中不能使用

    Collection c = new HashSet(); //后面的集合无所谓,主要看前面的Collection接口,怎么遍历/迭代。 c.add("abc"); c.add("def"); c.add(100); c.add(new Object()); //对集合Collection进行遍历/迭代 //第一步:获取集合对象的迭代器对象Iterator Iterator it = c.iterator(); //第二步:通过以上获取到的迭代器对象来遍历/迭代集合。

    迭代器中常用的方法:(要先调用iterator方法才能使用迭代器)

    boolean hasNext() 如果还有元素可以迭代/遍历,这个方法就返回true, 否则返回false

    Object next() 返回迭代中的下一个元素。

    while (it.hasNext()){ System.out.println(it.next()); } /* abc def 100 java.lang.Object@1b6d3586 */

    Collection c2 = new HashSet(); //无序:存进去和取出的顺序不一定相同 //不可重复:存储100,不能再存储100 c2.add(100); c2.add(90); c2.add(80); c2.add(100); c2.add(300); c2.add(400); c2.add(100); Iterator it2 = c2.iterator(); while (it2.hasNext()){ System.out.println(it2.next()); } /* 80 400 100 90 300 */ //HashSet无序不可重复的特点

    注意点:

    //创建集合 Collection c = new ArrayList(); //注意:此时获取的迭代器,指向的是那个集合中没有元素状态下的迭代器。 //集合结构只要改变,迭代器必须重新获取。 Iterator it = c.iterator(); //添加元素 c.add(1); c.add(2); c.add(3); //获取迭代器 //Iterator it = c.iterator(); while (it.hasNext()) { System.out.println(it.next()); }

    当集合结构发生改变,迭代器必须重新获取。

    这种情况下使用next方法会出现 java.util.ConcurrentModificationException异常

    在迭代集合元素的过程中,不能调用集合对象的remove方法删除元素 c.remove

    迭代器具有一次性,一次使用过后无法继续使用

    while (it.hasNext()) { Object o = it.next(); //删除元素 //删除元素之后,集合的结构发生了变化,应该重新区获取迭代器 //但是,循环下一次的时候并没有重新获取迭代器,所以会出现异常java.util.ConcurrentModificationException c.remove(o); System.out.println(o); } default void remove() 从底层集合中删除此迭代器返回的最后一个元素(可选操作)。

    在创建迭代器的时候相当于对集合的状态拍一张快照

    集合的remove方法是对原集合内的内容进行删除,当迭代器的进行迭代的时候会比较快照与集合的内容是否相同

    不同就会出现异常:java.util.ConcurrentModificationException

    而使用迭代器的remove方法,会直接将快照以及原集合的内容一起删除

    集合remove方法

    remove方法的源码分析

    //创建集合 Collection cc = new ArrayList(); //创建字符串对象 String hello = new String("hello"); //添加进集合 cc.add(hello); //创建了一个新的字符串对象 String hello2 = new String("hello"); //删除s2 cc.remove(hello2); //集合中元素的个数 System.out.println(cc.size());//0

    调用remove方法的时候,会自动调用equals方法

    迭代器remove方法

    深入Collection集合的contain方法

    Collection c = new ArrayList(); String s1 = new String("abc"); c.add(s1); String s2 = new String("def"); c.add(s2); System.out.println(c.size()); String x = new String("abc"); System.out.println(c.contains(x));//该方法底层调用的是equals方法

    重写equals方法

    放在集合中的元素要重写equals方法重写了比较的是对象的内容,不重写比较的是对象的地址

    public class CollectionTest05 { public static void main(String[] args) { Collection c = new ArrayList(); User u1 = new User("Jack"); User u2 = new User("Jack"); c.add(u1); //没有重写equals之前:这个结果是false //System.out.println(c.contains(u2));//false //重写equals方法之后,比较的时候会比较name System.out.println(c.contains(u2));//true } } class User{ private String name; public User() { } public User(String name) { this.name = name; } //重写equals方法 //将来调用equals方法的时候,一定是调用这个equals方法。 //这个equals方法的比较原理是:只要姓名一样就表示同一个用户 @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; //如果名字一样表示同一个人 return name.equals(user.name); } }

    Processed: 0.023, SQL: 8