ArrayList、LinkedList、HashMap的遍历及遍历过程中增、删元素
ArrayList、LinkedList、HashMap是Java中常用到的几种集合类型,遍历它们是时常遇到的情况。当然还有一些变态的时候,那就是在遍历的过程中动态增加或者删除其中的元素。
下面的例子就是可以实现动态遍历ArrayList、LinkedList、HashMap。
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Map;
import java.util.Map.Entry; /**
* @Description:
* @author Scott
* @date 2014年2月21日 下午8:33:40
*/
public class TraversalTest { ArrayList<String> arrayList = new ArrayList<String>();
LinkedList<String> linkedList = new LinkedList<String>();
HashMap<String, String> map = new HashMap<String, String>(); public TraversalTest() {
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.add("4");
arrayList.add("5"); linkedList.add("1");
linkedList.add("2");
linkedList.add("3");
linkedList.add("4");
linkedList.add("5"); map.put("1", "1");
map.put("2", "2");
map.put("3", "3");
map.put("4", "4");
map.put("5", "5"); System.out.println("Data has init over ...");
} /**
* ForEach Traversal.
* The Effective-Java recommended this traversal type.
* */
public void arrayListTraversalNormal1() {
System.out.println("ArrayList ForEach Traversal.");
for(String str : arrayList){
System.out.println(str);
}
} /**
* Iterator Traversal.
* */
public void arrayListTraversalNormal2() {
System.out.println("ArrayList Iterator Traversal.");
Iterator<String> itor = arrayList.iterator();
while(itor.hasNext()){
System.out.println(itor.next().toString());
}
} /**
* ForEach Traversal.
* The Effective-Java recommended this traversal type.
* */
public void linkedListTraversalNormal1() {
System.out.println("LinkedList ForEach Traversal.");
for(String str : linkedList){
System.out.println(str);
}
} /**
* Iterator Traversal.
* */
public void linkedListTraversalNormal2() {
System.out.println("LinkedList Iterator Traversal.");
Iterator<String> itor = linkedList.iterator();
while(itor.hasNext()){
System.out.println(itor.next().toString());
}
} public void mapTraversalNormal() {
System.out.println("HashMap Iterator Traversal.");
Iterator<Entry<String, String>> itor = map.entrySet().iterator();
Entry<String, String> entry = null;
String key = null;
String value = null; while(itor.hasNext()){
entry = itor.next();
key = entry.getKey();
value = entry.getValue(); System.out.println("key is " + key + ", value is " + value);
}
} /**
* While traversing the arrayList, add '33' behind the '3' element.
* */
public void arrayListTraversalDynamic1() {
ListIterator<String> itor = arrayList.listIterator();
String str = null; while (itor.hasNext()) {
str = itor.next().toString();
if(str.equals("3")){
itor.add("33");
break;
}
}
} /**
* While traversing the arrayList, remove the '3' element.
* */
public void arrayListTraversalDynamic2() {
ListIterator<String> itor = arrayList.listIterator();
String str = null; while (itor.hasNext()) {
str = itor.next().toString();
if(str.equals("3")){
itor.remove();
break;
}
}
} /**
* While traversing the linkedList, add '33' behind the '3' element.
* */
public void linkedListTraversalDynamic1() {
ListIterator<String> itor = linkedList.listIterator();
String str = null; while (itor.hasNext()) {
str = itor.next().toString();
if(str.equals("3")){
itor.add("33");
break;
}
}
} /**
* While traversing the linkedList, remove the '3' element.
* */
public void linkedListTraversalDynamic2() {
ListIterator<String> itor = linkedList.listIterator();
String str = null; while (itor.hasNext()) {
str = itor.next().toString();
if(str.equals("3")){
itor.remove();
break;
}
}
} /**
* While traversing the arrayList, add '33' when we get the '3' element.
* */
@SuppressWarnings("rawtypes")
public void mapTraversalDynamic1() {
LinkedList<Map.Entry<String, String>> tempList = new LinkedList<Map.Entry<String, String>>();
tempList.addAll(map.entrySet());
ListIterator<Map.Entry<String, String>> itor = tempList.listIterator();
Map.Entry entry = null; while (itor.hasNext()) {
entry = (Map.Entry) itor.next();
Object key = entry.getKey(); if (key.toString().equals("3")) {
map.put("33", "33");
}
}
} /**
* While traversing the hashMap, remove the '3' element.
* */
@SuppressWarnings("rawtypes")
public void mapTraversalDynamic2() {
LinkedList<Map.Entry<String, String>> tempList = new LinkedList<Map.Entry<String, String>>();
tempList.addAll(map.entrySet());
ListIterator<Map.Entry<String, String>> itor = tempList.listIterator();
Map.Entry entry = null; while (itor.hasNext()) {
entry = (Map.Entry) itor.next();
Object key = entry.getKey(); if(key.toString().equals("3")){
map.remove("3");
break;
}
}
} public static void main(String[] args) {
TraversalTest test = new TraversalTest(); test.arrayListTraversalNormal1();
test.arrayListTraversalNormal2(); System.out.println("While traversing the arrayList, add '33' behind the '3' element. The result is:");
test.arrayListTraversalDynamic1();
test.arrayListTraversalNormal1(); System.out.println("While traversing the arrayList, remove the '3' element. The result is:");
test.arrayListTraversalDynamic2();
test.arrayListTraversalNormal1(); System.out.println("-----------------------------------"); test.linkedListTraversalNormal1();
test.linkedListTraversalNormal2(); System.out.println("While traversing the linkedList, add '33' behind the '3' element. The result is:");
test.linkedListTraversalDynamic1();
test.linkedListTraversalNormal1(); System.out.println("While traversing the linkedList, remove the '3' element. The result is:");
test.linkedListTraversalDynamic2();
test.linkedListTraversalNormal1(); System.out.println("-----------------------------------");
test.mapTraversalNormal(); System.out.println("While traversing the hashMap, add '33' when we get the '3' element. The result is:");
test.mapTraversalDynamic1();
test.mapTraversalNormal(); System.out.println("While traversing the hashMap, remove the '3' element. The result is:");
test.mapTraversalDynamic2();
test.mapTraversalNormal();
} }
代码是最好的说明,运行结果如下:
Data has init over ...
ArrayList ForEach Traversal.
1
2
3
4
5
ArrayList Iterator Traversal.
1
2
3
4
5
While traversing the arrayList, add '33' behind the '3' element. The result is:
ArrayList ForEach Traversal.
1
2
3
33
4
5
While traversing the arrayList, remove the '3' element. The result is:
ArrayList ForEach Traversal.
1
2
33
4
5
-----------------------------------
LinkedList ForEach Traversal.
1
2
3
4
5
LinkedList Iterator Traversal.
1
2
3
4
5
While traversing the linkedList, add '33' behind the '3' element. The result is:
LinkedList ForEach Traversal.
1
2
3
33
4
5
While traversing the linkedList, remove the '3' element. The result is:
LinkedList ForEach Traversal.
1
2
33
4
5
-----------------------------------
HashMap Iterator Traversal.
key is 3, value is 3
key is 2, value is 2
key is 1, value is 1
key is 5, value is 5
key is 4, value is 4
While traversing the hashMap, add '33' when we get the '3' element. The result is:
HashMap Iterator Traversal.
key is 3, value is 3
key is 2, value is 2
key is 1, value is 1
key is 5, value is 5
key is 4, value is 4
key is 33, value is 33
While traversing the hashMap, remove the '3' element. The result is:
HashMap Iterator Traversal.
key is 2, value is 2
key is 1, value is 1
key is 5, value is 5
key is 4, value is 4
key is 33, value is 33
-------------------------------------------------------------------------------
如果您看了本篇博客,觉得对您有所收获,请点击右下角的 [推荐]
如果您想转载本博客,请注明出处
如果您对本文有意见或者建议,欢迎留言
感谢您的阅读,请关注我的后续博客
ArrayList、LinkedList、HashMap的遍历及遍历过程中增、删元素的更多相关文章
- arrayList LinkedList HashMap HashTable的区别
ArrayList 采用的是数组形式来保存对象的,这种方式将对象放在连续的位置中,所以最大的缺点就是插入删除时非常麻烦 LinkedList 采用的将对象存放在独立的空间中,而且在每个空间中还保存下一 ...
- JDK1.7源码阅读tools包之------ArrayList,LinkedList,HashMap,TreeMap
1.HashMap 特点:基于哈希表的 Map 接口的实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.(除了非同步和允许使用 null 之外,HashMap 类与 Has ...
- Java Map在遍历过程中删除元素
Java中的Map如果在遍历过程中要删除元素,除非通过迭代器自己的remove()方法,否则就会导致抛出ConcurrentModificationException异常.JDK文档中是这么描述的: ...
- 设计模式系列之迭代器模式(Iterator Pattern)——遍历聚合对象中的元素
模式概述 模式定义 模式结构图 模式伪代码 模式改进 模式应用 模式在JDK中的应用 模式在开源项目中的应用 模式总结 说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修 ...
- 有关《查找两个List中的不同元素》的问题解答与编程实践
郑海波 2013-07-08 问题: 有List<String> list1和List<String> list2,两个集合各有上万个元素,怎样查找两个集合中不同的元素呢? ...
- Hashtable,HashMap,TreeMap有什么区别?Vector,ArrayList,LinkedList有什么区别?int和Integer有什么区别?
接着上篇继续更新. /*请尊重作者劳动成果,转载请标明原文链接:*/ /*https://www.cnblogs.com/jpcflyer/p/10759447.html* / 题目一:Hashtab ...
- Java泛型底层源码解析-ArrayList,LinkedList,HashSet和HashMap
声明:以下源代码使用的都是基于JDK1.8_112版本 1. ArrayList源码解析 <1. 集合中存放的依然是对象的引用而不是对象本身,且无法放置原生数据类型,我们需要使用原生数据类型的包 ...
- 【java基础】java中ArrayList,LinkedList
[一]ArrayList 一ArrayList的内部结构 (1)ArrayList内部维护的是一个Object数组 (2)ArrayList数组扩容后数组的长度的公式:旧的数组长度+(旧数组长度> ...
- LinkedList,ArrayList,Vector,HashMap,HashSet,HashTable之间的区别与联系
在编写java程序中,我们最常用的除了八种基本数据类型,String对象外还有一个集合类,在我们的的程序中到处充斥着集合类的身影!java中集合大家族的成员实在是太丰富了,有常用的ArrayList. ...
随机推荐
- 利用 __FUNCTION__ 宏打印函数调用信息
__FUNCTION__ 宏表示当前所在函数名: __FILE__ 宏表示当前所在文件路径: __LING__ 宏表示当前所在行: 利用对象离开函数时调用析构函数销毁的特点,打印出函数执行结束的信息 ...
- C++:构造函数的重载
构造函数的重载的判断条件是:参数的个数或者类型不同,其他要保持一样 举例说明: 方式一:在类中声明重载构造函数,在类外定义 //第一种方法: #include<iostream> usin ...
- 你听说过PHP 的面向方面编程吗?
面向方面编程(AOP)对于PHP来说是一个新的概念.现在PHP对于 AOP 并没有官方支持,但有很多扩展和库实现了这个特性.本课中,我们将使用 Go! PHP library 来学习 PHP 如何进行 ...
- maven小项目注册服务(二)--captcha模块
验证码生成模块,配置信息基本和前面的模块一样.account-captcha需要提供的服务是生成随机的验证码主键,然后用户可以使用这个主键要求服务生成一个验证码图片,这个图片对应的值应该是随机的,最后 ...
- Lua for windows中SciTe开启支持python的方法
打开Options-Open Global Options #import python的#去掉即可
- struts2更新版本操作有关事项备注
struts2更新版本操作有关事项备注, 更新主要jar包:struts2-convention-plugin-version,struts2-core-version, struts2-spring ...
- js中的this怎么理解
本博客供自己学习备忘, js中的this感觉很混乱,目前还有不少地方搞得不是很清楚,看到一篇不错的文章,先摘下来 this是Javascript语言的一个关键字它代表函数运行时,自动生成的一个内部对象 ...
- Web内容管理系统 Magnolia 启程-挖掘优良的架构(3)
Author and Public instances 第一个关键观念:instance-实例.每一个项目都必须至少有一个Author实例和至少一个Public实例.下面将告诉你为什么: 基本概念:J ...
- hdu 3433 A Task Process(dp+二分)
题目链接 题意:n个人, 要完成a个x任务, b个y任务. 求,最短的时间 思路:由于时间较大,用 二分来找时间. dp[i][j]表示 i个人完成j个x任务, 最多能完成的y任务个数 这个题 不是很 ...
- topcoder srm 610 div2 250
第一次做tc 的比赛,一点也不懂,虽然题目做出来了, 但是,也没有在比赛的时候提交成功.. 还有,感谢一宁对tc使用的讲解.. 贴一下代码..... #include <cstring> ...