List

List<String> list = new ArrayList<>();
list.add("张三");
list.add("李四");
list.add("王五");
//遍历List
//方式一:for循环
System.out.print("for循环遍历List:");
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i));
}
System.out.println();
//方式二:增强型for循环
System.out.print("增强型for循环遍历List:");
for (String str :
list) {
System.out.print(str);
}
System.out.println();
//方式三:Iterator迭代器方式遍历
//for循环方式
System.out.print("Iterator迭代器遍历List(for循环):");
for (Iterator<String> iterator = list.iterator();iterator.hasNext();){
System.out.print(iterator.next());
}
System.out.println();
//while循环方式
System.out.print("Iterator迭代器遍历List(while循环):");
Iterator<String> iterator = list.iterator();
while(iterator.hasNext()){
System.out.print(iterator.next());
iterator.remove(); //如果想一边遍历一边删除,建议使用此种方式
}


Set

Set<String> set = new HashSet<>();
set.add("one");
set.add("two");
set.add("three");
set.add("four");
//遍历Set
//方式一:增强型for循环(底层用HashMap实现不能用简单for循环)
System.out.print("增强型for循环遍历Set:");
for (String str :
set) {
System.out.print(str);
}
System.out.println();
//方式二:迭代器Iterator
System.out.print("Iterator迭代器遍历Set:");
for (Iterator<String> iteraotr = set.iterator();iteraotr.hasNext();){
System.out.print(iteraotr.next());
} //while循环一样


Map

 //遍历Map
Map<Integer,String> map = new HashMap<>();
map.put(1,"张三");
map.put(2,"李四");
map.put(3,"王五");
map.put(4,"赵六");
System.out.println();
//通过keySet方式
System.out.print("keySet方法遍历Map:");
Set<Integer> tmp = map.keySet();
for (Iterator<Integer> iterator1 = tmp.iterator();iterator1.hasNext();){
Integer key = iterator1.next();
System.out.print(key+"-->"+map.get(key));
}
System.out.println();
//通过entrySet方式
System.out.print("entrySet方法遍历Map:");
Set<Map.Entry<Integer,String>> t = map.entrySet();
for (Iterator<Map.Entry<Integer,String>> iterator1 = t.iterator();iterator1.hasNext();){
Map.Entry<Integer,String> entry = iterator1.next();
System.out.print(entry.getKey()+"-->"+entry.getValue());
}

Java中遍历容器List、Map、Set的方法总结的更多相关文章

  1. java 中遍历hashmap 和hashset 的方法

    一.java中遍历hashmap:    for (Map.Entry<String, Integer> entry : tempMap.entrySet()) {     String ...

  2. Java中遍历Map集合的四种方法

    在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...

  3. java中遍历map对象的多种方法

    在Java中如何遍历Map对象   How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有ma ...

  4. java中遍历MAP,嵌套map的几种方法

    java中遍历MAP的几种方法 Map<String,String> map=new HashMap<String,String>();    map.put("us ...

  5. Java中遍历map的四种方法 - 转载

    在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...

  6. JAVA中遍历Map和Set方法,取出map中所有的key

    Java遍历Set集合 1.迭代器遍历: Set<String> set = new HashSet<String>(); Iterator<String> it ...

  7. JAVA 中的 Collection 和 Map 以及相关派生类的概念

    JAVA中Collection接口和Map接口的主要实现类   Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的 ...

  8. (转)Java中的容器详细总结

    Java中的容器详细总结(编辑中) 原文链接:http://anxpp.com/index.php/archives/656/ 注:本文基于 Jdk1.8 编写 通常程序总是根据运行时才知道的某些条件 ...

  9. java中的容器问题

    小小的总结一下java中的容器问题. 一.三个知识点 1.迭代器 1).java.util.Interator + hasnext(); next(); remove(); 2).java.lang. ...

随机推荐

  1. 局部处理的边缘连接(python+opencv)

    rt import cv2 import numpy as np path = "_lo.png" img = cv2.imread(path) gray = cv2.cvtCol ...

  2. PowerDesigner 生成SQL Server 2005 注释脚本

    --生成数据表的注释EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=[%R%?[N]]%.q:COMMENT% , @l ...

  3. Acwing-278-数字组合(背包)

    链接: https://www.acwing.com/problem/content/280/ 题意: 给定N个正整数A1,A2,-,AN,从中选出若干个数,使它们的和为M,求有多少种选择方案. 思路 ...

  4. nginx实现商品详情页的缓存

  5. CSS的文本属性

    CSS 文本属性可定义文本的外观. 通过文本属性,可以改变文本的颜色.字符间距,对齐文本,装饰文本,对文本进行缩进等. ㈠缩进文本   text-indent  通过使用 text-indent 属性 ...

  6. python3基础:基本语句

    http://www.cnblogs.com/qq21270/p/4591318.html  字符串.文本文件 http://www.cnblogs.com/qq21270/p/7872824.htm ...

  7. AcWing:110. 防晒(贪心)

    有C头奶牛进行日光浴,第i头奶牛需要minSPF[i]到maxSPF[i]单位强度之间的阳光. 每头奶牛在日光浴前必须涂防晒霜,防晒霜有L种,涂上第i种之后,身体接收到的阳光强度就会稳定为SPF[i] ...

  8. R_Studio(决策树算法)鸢尾花卉数据集Iris是一类多重变量分析的数据集【精】

    鸢尾花卉数据集Iris是一类多重变量分析的数据集 通过花萼长度,花萼宽度,花瓣长度,花瓣宽度4个属性预测鸢尾花卉属于(Setosa,Versicolour,Virginica)三个种类中的哪一类 针对 ...

  9. dataX调优

    dataX调优 标签(空格分隔): ETL 一,Datax调优方向 DataX调优要分成几个部分(注:此处任务机指运行Datax任务所在的机器). 1,网络本身的带宽等硬件因素造成的影响: 2,Dat ...

  10. 项目配置 xml文件时 报错提示(The reference to entity "useSSL" must end with the ';' delimiter.)

    这次在配置xml文件时,出现错误提示( The reference to entity “useSSL” must end with the ‘;’ delimiter.) 报错行为 <prop ...