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 ...
随机推荐
- git branch & git remote branch
git branch & git remote branch $ git branch -h usage: git branch [<options>] [-r | -a] [-- ...
- 2018 free pdf ebooks
2018 free pdf ebooks https://gist.github.com/xgqfrms-GitHub/f606efb0d4bce884c873518647e79f2f https:/ ...
- flutter package & pub publish
flutter package & pub publish dart-library-package https://pub.dev/packages/dart_library_package ...
- how to input special keyboard symbol in macOS(⌘⇧⌃⌥)
how to input special keyboard symbol in macOS(⌘⇧⌃⌥) emoji ctrl + command + space / ⌘⇧⌃ ⌘⇧⌃ Character ...
- 微信小程序 API
微信小程序 API https://developers.weixin.qq.com/miniprogram/dev/component/cover-view.html demo https://de ...
- 漫画 | C语言哭了,过年回家,只有我还没对象
C语言回家过年,遇到不少小伙伴. 大家都在外地打拼,一年难得见面,聚到一起吃饭,都非常高兴. 听Java提到TIOBE, 正在喝酒的C语言激动起来. 自己常年在那里排名第二,人类用自己写的程序可真不少 ...
- Java基础语法:运算符
Java 运算符(operator)根据功能分类: 算术运算符:+,-,*,/,%,++,-- 赋值运算符:= 关系运算符:>,<,>=,<=,==,!=,instanceof ...
- PowerDesigner 设计数据库中常用脚本
PowerDesigner 设计数据库中常用脚本 数据库设计 物理模型设置 Name转Comment脚本 '********************************************** ...
- nginx反向代理、负载均衡以及分布式下的session保持
[前言]部署服务器用到了nginx,相比较于apache并发能力更强,优点也比其多得多.虽然我的项目可能用不到这么多性能,还是部署一个流行的服务器吧! 此篇博文主要学习nginx(ingine x)的 ...
- wxWidgets源码分析(4) - 消息处理过程
目录 消息处理过程 消息如何到达wxWidgets Win32消息与wxWidgets消息的转换 菜单消息处理 消息处理链(基于wxEvtHandler) 消息处理链(基于wxWindow) 总结 消 ...