Java中遍历集合的常用方法
一、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中遍历集合的常用方法的更多相关文章
- java中遍历集合的三种方式
第一种遍历集合的方式:将集合变为数组 package com.lw.List; import java.util.ArrayList; import java.util.List; import ja ...
- Java中遍历Map的常用方法
以下方法适用于任何map实现(HashMap, TreeMap, LinkedHashMap, Hashtable, 等等): 方式一(推荐): // 推荐 // 在for-each循环中使用entr ...
- Java中List集合的常用方法
List接口是继承Collection接口,所以Collection集合中有的方法,List集合也继承过来. 这篇文章就不讲继承Collection接口的那些方法了 https://www.cnblo ...
- java中Map集合的常用方法 (转)
原文地址:https://www.cnblogs.com/xiaostudy/p/9510763.html Map集合和Collection集合的区别 Map集合是有Key和Value的,Collec ...
- java中Map集合的常用方法
Map集合和Collection集合的区别 Map集合是有Key和Value的,Collection集合是只有Value. Collection集合底层也是有Key和Value,只是隐藏起来. V p ...
- java中TreeMap集合的常用方法
实现Map集合的方法这里就不在讲了 https://www.cnblogs.com/xiaostudy/p/9510763.html public Map.Entry<K,V> ceili ...
- java中Hashtable集合的常用方法
实现Map集合的方法这里就不在讲了 https://www.cnblogs.com/xiaostudy/p/9510763.html public Object clone() 返回Hashtable ...
- java中HashMap集合的常用方法
public Object clone() 返回hashMap集合的副本 其余的方法都是实现Map集合的 https://www.cnblogs.com/xiaostudy/p/9510763.htm ...
- java中set集合的常用方法
因为Set集合也是继承Collection集合 所以这里就不讲继承Collection集合的方法 都是继承Collection集合的方法 https://www.cnblogs.com/xiaostu ...
随机推荐
- Set DSL in Ubuntu 18.04
Reference Solutions: Ctrl+Atl+t Type nmcli con edit type pppoe con-name ANY_NAME_OF_DSL_YOU_LIKE, wh ...
- js create Array ways All In One
js create Array ways All In One ES6 const arr = [...document.querySelectorAll(`[data-dom="^div& ...
- Android vs iOS vs Web
Android vs iOS vs Web UI view Android ViewGroup ImageView TextView iOS UIView ImageView TextView Web ...
- Apple 反人类的设计的产品组合
Apple 反人类的设计的产品组合 Apple shit design macbook pro 2018 + beats solo3 MBP 的耳机孔在电脑右边, betas 的耳机孔在左边, 组合起 ...
- js 获取是否为闰年,以及各月的天数 & isLeapYear
js 获取是否为闰年,以及各月的天数 calendar utils isLeapYear const isLeapYear = (year) => { return (year % 4 === ...
- 比特币等主流货币走势成谜,VAST深受关注
谁也不会想到,2021年的第一个月份,数字货币市场就会如此精彩.先是以比特币为首的主流货币迎来了一波上涨,让很多生态建设者看到了暴富的机会.再是一波大跌,让很多建设者失去了希望.再到后来触底反弹和冲高 ...
- spring boot用ModelAndView向Thymeleaf模板传参数
最近在调试一个Spring Boot向Thymeleaf模板传参数的例子,但踩了很多坑,这里就把详细过程记录下来,以供大家参考. 先说下,这里遇到哪些坑呢? 1 我用的是IDEA社区版,这不支持JSP ...
- AI数学基础之:奇异值和奇异值分解
目录 简介 相似矩阵 对角矩阵 可对角化矩阵 特征值 特征分解 特征值的几何意义 奇异值 Singular value 奇异值分解SVD 简介 奇异值是矩阵中的一个非常重要的概念,一般是通过奇异值分解 ...
- sql where 1=1 的详细解释
原文来自:https://blog.csdn.net/zc474235918/article/details/50544484 看一下这两个句子: select * from user select ...
- C语言:贪心算法之装箱问题
#include <stdio.h> #include <stdlib.h> #define N 6 #define V 100 typedef struct box // 使 ...