• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

lzsykal

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

20220806 第六组 张嘉源 学习笔记二

集合(全)

遍历

迭代器

Collection接口遍历元素使用Iterator(迭代器),所有实现Collection接口的集合类都有Iterator()方法,只能用于遍历集合

增强for循环遍历

底层也是采用了Iterator的方式实现

List常用遍历方式

1.普通for循环
2.增强for循环
3.迭代器

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
 * List集合的遍历
 */
public class Ch10 {
    public static void main(String[] args) {
       List<String> list = new ArrayList<>();
       list.add("a");
       list.add("b");
       list.add("c");
       list.add("d");
       // 1.for循环
//        for (int i = 0; i < list.size(); i++) {
//            System.out.println(list.get(i));
//        }
        // 2.foreach语句
//        for (String s : list) {
//            System.out.println(s);
//        }
        // 3.迭代器
        Iterator<String> iterator = list.iterator();
        while(iterator.hasNext()) {
            String s = iterator.next();
            iterator.remove();
//            System.out.println(s);
        }
        System.out.println(list);
    }
}

Set常用遍历方式

1.增强for循环(不能使用普通for循环,因为没有提供get方法)
2.迭代器

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Ch11 {
    public static void main(String[] args) {
        Set<Integer> set = new HashSet<>();
        set.add(4);
        set.add(15);
        set.add(38);
        // 2.迭代器
        Iterator<Integer> iterator = set.iterator();
        while(iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        // 1.增强for
//        for (Integer integer : set) {
//            System.out.println(integer);
//        }
//        for (int i = 0; i < set.size(); i++) {
//        }
    }
}

Map常用遍历方式

import java.util.*;
public class Ch01 {
        public static void main(String[] args) {
            Map<String, String> map = new HashMap<>();
            map.put("1001","张岳");
            map.put("1002","赵红兵");
            map.put("1003","小北京");
            map.put("1004","李四");
            map.put("1005","张浩然");

            // 4.迭代器(先取出所有的key,再通过key获取对应的values)
            Set<String> strings = map.keySet();
            Iterator<String> iterator = strings.iterator();
            while(iterator.hasNext()) {
                String key = iterator.next();
                System.out.println(key + "->" + map.get(key));
            }

            // 3.增强for循环
            // Entry是hashmap的一个内部类
            // 每一组键值对就是一个Entry对象
//        Set<Map.Entry<String, String>> entries = map.entrySet();
//        for (Map.Entry<String, String> entry : entries) {
//            System.out.print(entry.getKey() + "->");
//            System.out.println(entry.getValue());
//        }

            // 2.增强for循环
//        Set<String> strings = map.keySet();
//        Collection<String> values = map.values();

            // 1.for循环
//        Set<String> strings = map.keySet();
//        for (String s : strings) {
//            System.out.println(s + "->" + map.get(s));
//        }
        }
    }

迭代中删除元素

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
/**
 * 迭代中删除元素
 */
public class Ch01 {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>();
        names.add("tom");
        names.add("lucy");
        names.add("lucy");
        names.add("lucy");
        names.add("jerry");

//        for (int i = 0; i < names.size(); i++) {
//            if(Objects.equals(names.get(i),"lucy")){
//                names.remove(i);
//                // 1.回调指针
//                i--;
//            }
////            if("lucy".equals(names.get(i))){
////
////            }
//        }
        // 2.逆序遍历
//        for (int i = names.size() - 1; i >= 0; i--) {
//            if(Objects.equals(names.get(i),"lucy")){
//                names.remove(i);
//            }
//        }
        // 3.使用迭代器(推荐)万无一失
        Iterator<String> iterator = names.iterator();
        while(iterator.hasNext()) {
            String s = iterator.next();
            if(Objects.equals(s,"lucy")){
                iterator.remove();
            }
        }
        // 4.增强for循环
//        for (String s : names) {
//            if(Objects.equals(s,"lucy")){
//                names.remove(names.indexOf(s));
//            }
//        }
        System.out.println(names);
    }
}
面试题

线程安全问题:
迭代器是依赖于集合而存在,在判断成功以后,集合中新增了元素,迭代器不知道,所以就报错。
解决:
1.迭代器遍历元素,迭代器删除元素
2.普通for循环遍历,集合删除

public class Ch15 {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>();
        names.add("tom");
        names.add("lucy");
        names.add("lucy");
        names.add("lucy");
        names.add("jerry");

        for (String s : names) {
            if(Objects.equals(s,"lucy")){
//                names.remove(s);
                names.add(s);
            }
        }
    }
}

posted on 2022-08-06 21:06  林钟朔一  阅读(23)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3