一、List

1、普通for循环

for (int i = 0; i < list.size(); i++)){
String temp = (String)list.get(i);
System.out.println(temp);
}

  

2、增强for循环(使用泛型)

for (String temp: list) {
System.out.println(temp);
}

  

3、使用Iterator迭代器

for (Iterator it = list.iterator(); it.hasNext();) {
String temp = (String)it.next();
System.out.println(temp);
}

  

4、使用Iterator迭代器

Iterator it = list.iterator();
while(it.hasNext()) {
Object obj = it.next();
it.remove(); // 如果遍历时要删除集合中的元素
System.out.println(obj);
}

  

二、Set

1、增强for循环

for (String temp: set) {
System.out.println(temp);
}

  

2、使用Iterator迭代器

for (Iterator<String> it = set.iterator(); it.hasNext();) {
String temp = (String)it.next();
System.out.println(temp);
}

  

三、Map

1、根据key获取value

Set<Integer> s2 = map1.keySet();
for (Iterator<Integer> it = s2.iterator(); it.hasNext();) {
Integer temp = it.next();
System.out.println(temp + " " + map1.get(temp));
}

  

2、使用entrySet

Set<Entry<Integer, String>> s1 = map1.entrySet();
for (Iterator<Entry<Integer, String>> it = s1.iterator(); it.hasNext();) {
Entry<Integer, String> temp = it.next();
System.out.println(temp.getKey() + " " + temp.getValue()); }

  

Java中遍历集合的常用方法的更多相关文章

  1. java中遍历集合的三种方式

    第一种遍历集合的方式:将集合变为数组 package com.lw.List; import java.util.ArrayList; import java.util.List; import ja ...

  2. Java中遍历Map的常用方法

    以下方法适用于任何map实现(HashMap, TreeMap, LinkedHashMap, Hashtable, 等等): 方式一(推荐): // 推荐 // 在for-each循环中使用entr ...

  3. Java中List集合的常用方法

    List接口是继承Collection接口,所以Collection集合中有的方法,List集合也继承过来. 这篇文章就不讲继承Collection接口的那些方法了 https://www.cnblo ...

  4. java中Map集合的常用方法 (转)

    原文地址:https://www.cnblogs.com/xiaostudy/p/9510763.html Map集合和Collection集合的区别 Map集合是有Key和Value的,Collec ...

  5. java中Map集合的常用方法

    Map集合和Collection集合的区别 Map集合是有Key和Value的,Collection集合是只有Value. Collection集合底层也是有Key和Value,只是隐藏起来. V p ...

  6. java中TreeMap集合的常用方法

    实现Map集合的方法这里就不在讲了 https://www.cnblogs.com/xiaostudy/p/9510763.html public Map.Entry<K,V> ceili ...

  7. java中Hashtable集合的常用方法

    实现Map集合的方法这里就不在讲了 https://www.cnblogs.com/xiaostudy/p/9510763.html public Object clone() 返回Hashtable ...

  8. java中HashMap集合的常用方法

    public Object clone() 返回hashMap集合的副本 其余的方法都是实现Map集合的 https://www.cnblogs.com/xiaostudy/p/9510763.htm ...

  9. java中set集合的常用方法

    因为Set集合也是继承Collection集合 所以这里就不讲继承Collection集合的方法 都是继承Collection集合的方法 https://www.cnblogs.com/xiaostu ...

随机推荐

  1. 域渗透——获得域控服务器的NTDS.dit文件

    0x00 前言 在之前的文章<导出当前域内所有用户hash的技术整理>曾介绍过通过Volume Shadow Copy实现对ntds.dit文件的复制, 可用来导出域内所有用户hash.本 ...

  2. 力扣561. 数组拆分 I-C语言实现-简单题

    题目 传送门 给定长度为 2n 的整数数组 nums ,你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从 1 到 n 的 min(a ...

  3. MOOC学习成果认证及对高等教育变革路径的影响

    MOOC是网络开放教育创新发展的产物,也是备受人们欢迎的网络学习途径.当前制约MOOC能否可持续深入发展的问题聚焦于MOOC学习成果能否得到合理的认证.MOOC学习成果认证分为非学分认证和学分认证.M ...

  4. learning all in one

    learning learning all in one https://github.com/xgqfrms/learning/tree/gh-pages/GraphQL https://githu ...

  5. PHP & LAMP & WAMP

    PHP & LAMP & WAMP https://github.com/xgqfrms/DataStructure/issues/7#issuecomment-430538438 h ...

  6. taro 渲染 html

    taro 渲染 html https://taro-docs.jd.com/taro/next/docs/next/html.html // Taro 更推荐使用框架自带的渲染 HTML 方式 // ...

  7. nvm install node error

    nvm install node error ➜ mini-program-all git:(master) nvm install 10.15.3 Downloading and installin ...

  8. vue & dynamic components

    vue & dynamic components https://vuejs.org/v2/guide/components-dynamic-async.html keep-alive htt ...

  9. 快速读懂 HTTP/3 协议

    在 深入浅出:HTTP/2 一文中详细介绍了 HTTP/2 新的特性,比如头部压缩.二进制分帧.虚拟的"流"与多路复用,性能方面比 HTTP/1 有了很大的提升.与所有性能优化过程 ...

  10. SQL EXPLAIN解析

    本文转载自MySQL性能优化最佳实践 - 08 SQL EXPLAIN解析 什么是归并排序? 如果需要排序的数据超过了sort_buffer_size的大小,说明无法在内存中完成排序,就需要写到临时文 ...