一、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. CSS 解决Float后塌陷问题

    当父级元素没有设定高度时候,而子集元素设定float类型时候,此时父级元素不能靠子集元素撑起来,所以就形成了塌陷: 示例分析 **1.Float之前的效果** <!DOCTYPE html> ...

  2. [USACO15JAN]Moovie Mooving G

    [USACO15JAN]Moovie Mooving G 状压难题.不过也好理解. 首先我们根据题意: she does not want to ever visit the same movie t ...

  3. 蓝桥杯——试题 算法训练 Sereja and Squares

    Java 代码 ```` import java.util.Scanner; public class Main { private static long num = 0; private stat ...

  4. php 安装 yii framework notice-error 的解决方案!

    1 问题描述: 2 解决方案: error_reporting(0); //解决error_notice 的最简单最有效的方法在每一个php文件的头部都加上error_reporting(0); 3. ...

  5. H5 CSS 悬浮滚动条

    H5 CSS 悬浮滚动条 refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!

  6. 十大排序算法时间复杂度 All In One

    十大排序算法时间复杂度 All In One 排序算法时间复杂度 排序算法对比 Big O O(n) O(n*log(n)) O(n^2) 冒泡排序 选择排序 插入排序 快速排序 归并排序 基数排序 ...

  7. Interview Questions All In One

    Interview Questions All In One web fullstack System Design Operating System Object-Oriented Design O ...

  8. Vue Learning Paths

    Vue Learning Paths Vue Expert refs https://vueschool.io/articles/vuejs-tutorials/exciting-new-featur ...

  9. 手机 wifi 已连接,不可上网 bug

    手机 wifi 已连接,不可上网 bug 同一个 Wi-Fi,电脑却可以? 注意事项 Mac 共享热点支持有线连接.蓝牙连接的网络进行共享. 如果你的 Mac 本身是通过 wifi 来连接上网的,那就 ...

  10. SVG & gradient & color

    SVG & gradient & color https://developer.mozilla.org/zh-CN/docs/Web/SVG/Tutorial/Gradients & ...