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 ...
随机推荐
- Hadoop 3.0 EC技术
Hadoop 3.0 EC技术 EC的设计目标 Hadoop默认的3副本方案需要额外的200%的存储空间.和网络IO开销 而一些较低I/O的warn和cold数据,副本数据的访问是比较少的(hot数据 ...
- Node.js 实战 & 最佳 Express 项目架构
Node.js 实战 & 最佳 Express 项目架构 Express Koa refs https://github.com/xgqfrms/learn-node.js-by-practi ...
- p5.js
p5.js p5.js是一个用于创意编码的JavaScript库,其重点是使艺术家,设计师,教育者,初学者以及其他任何人都可以访问并包含所有编码! https://p5js.org/ https: ...
- Linux & terminal color & command line color
Linux & terminal color & command line color how to change Linux terminal color https://askub ...
- c++ 动态解析PE导出表
测试环境是x86 main #include <iostream> #include <Windows.h> #include <TlHelp32.h> #incl ...
- react 异步的setState
import React from "react"; class App extends React.Component { state = { a: 0 }; component ...
- 「NGK每日快讯」2021.2.8日NGK公链第97期官方快讯!
- 两百万SPC空投来袭,带动市场热情!
NGK投放项目的时间节点总是以牛市为主,像是上一次的BGV项目投放就在2020年末的数字加密货币牛市,其结果想必各位生态建设者们都已经见到了,在登陆交易所首日便高收于近889美金,创下惊人的近一千七百 ...
- NGK项目八大板块是什么?
公链项目生态各板块中,应用生态繁荣与实体经济联系作为密切,也被看做公链平台追求的终极目标,NGK公链以实体企业粘合客户,致力于重塑金融体系,构建全球区块链生态. NGK让经济权益上链发行,目前已有八大 ...
- SpringCloud Stream
1.介绍 官网:https://www.springcloud.cc/spring-cloud-dalston.html#_spring_cloud_stream 1.1定义 是一个构建消息驱动微服务 ...