List&Map&Set的操作和遍历
- Set:代表无序、不可重复的集合,常用的有HashSet(哈希表实现)、TreeSet(红黑树实现);
- List:代表有序、可以重复的集合,比较常用的有ArrayList(数组实现)、LinkedList(链表实现);
- Map:代表具有映射关系的集合,常用的有HashMap(哈希表实现)、TreeMap(红黑树实现);
Java5以后又增加了Queue体系集合,代表一种队列集合实现,这里先不介绍。
List的实现类原理比较简单,Map比较复杂,而Set其实是基于Map的一种实现。
下面从各个集合的基本操作介绍一下,分别选取HashSet、ArrayList、HashMap三个典型的实现类:
1. HashSet
/**
* HashSet的增删遍历
* @author wangjun
* @email scuwangjun@hotmail.com
* @time 2018年4月6日 下午2:40:33
*/
public class HashSetOperation {
public static void main(String[] args) {
//初始化
HashSet<String> set = new HashSet<>();
//增
set.add("key1");
set.add("key2");
set.add("key3");
//删
set.remove("key1");
//遍历1
//使用set.descendingIterator()方法可以反向遍历
System.out.println("HashSet遍历1,使用Iterator:");
Iterator<String> it = set.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
//遍历2
System.out.println("HashSet遍历2,使用for:");
for(String str: set) {
System.out.println(str);
}
}
运行结果:
HashSet遍历1,使用Iterator:
key2
key3
HashSet遍历2,使用for:
key2
key3
2.ArrayList
/**
* ArrayList的增删查改,遍历
* @author wangjun
* @email scuwangjun@hotmail.com
* @time 2018年4月6日 下午2:25:43
*/
public class ArrayListOperation {
public static void main(String[] args) {
//初始化
List<String> list = new ArrayList<>();
//增
list.add("str1");
list.add("str2");
list.add("str3");
//删
list.remove(1);
//查
System.out.println("list的第二个元素是:" + list.get(1));
//改
list.set(0, "str11");
System.out.println("最终的list:" + list.toString());
//遍历1,使用for
System.out.println("LinkedList遍历1,使用for:");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
//遍历2,使用增强for
System.out.println("LinkedList遍历1,使用增强for:");
for(String str: list) {
System.out.println(str);
}
//遍历3,使用Iterator,集合类的通用遍历方式
System.out.println("LinkedList遍历3,使用Iterator:");
Iterator<String> it = list.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
运行结果:
list的第二个元素是:str3
最终的list:[str11, str3]
LinkedList遍历1,使用for:
str11
str3
LinkedList遍历1,使用增强for:
str11
str3
LinkedList遍历3,使用Iterator:
str11
str3
3.HashMap
/**
* hashMap的增删查改
* 无序
* key相当于set,不可重复
* value相当于list,可重复
* @author wangjun
* @email scuwangjun@hotmail.com
* @time 2018年4月6日 下午2:30:31
*/
public class HashMapOperation {
public static void main(String[] args) {
//初始化
HashMap<String,String> map = new HashMap<>();
//增
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
//删
map.remove("key2");
//查
System.out.println("key1对应的valve为:" + map.get("key1"));
//改
map.replace("key3", "value33");
System.out.println("最终的map是:" + map.toString());
//遍历1,取出map中所有的key组成一个set
System.out.println("HashMap遍历1,取出map中所有的key组成一个set:");
for(String key: map.keySet()) {
System.out.println("key:" + key + ",value:" + map.get(key));
}
//遍历2,取出key组成set后,通过Iterator遍历key
System.out.println("HashMap遍历2,取出key组成set后,通过Iterator遍历key:");
Iterator<String> it = map.keySet().iterator();
while(it.hasNext()) {
String key = it.next();
String value = map.get(key);
System.out.println("key:" + key + ",value:" + value);
}
//遍历3,取出map中实际存储的数据结构--Map.Entry,在HashMap中使用的是Node静态内部类
//推荐这种,尤其是数据很大时
System.out.println("HashMap遍历3,通过Map.Entry:");
Set<Map.Entry<String, String>> entry = map.entrySet();
for(Map.Entry<String, String> entryItem: entry) {
String key = entryItem.getKey();
String value = entryItem.getValue();
System.out.println("key:" + key + ",value:" + value);
}
//遍历4,只能遍历value,不能遍历key,相当于取出map中左右的value组成一个list
System.out.println("HashMap遍历4,只遍历value:");
for(String value: map.values()) {
System.out.println("value:" + value);
}
}
}
运行结果:
key1对应的valve为:value1
最终的map是:{key1=value1, key3=value33}
HashMap遍历1,取出map中所有的key组成一个set:
key:key1,value:value1
key:key3,value:value33
HashMap遍历2,取出key组成set后,通过Iterator遍历key:
key:key1,value:value1
key:key3,value:value33
HashMap遍历3,通过Map.Entry:
key:key1,value:value1
key:key3,value:value33
HashMap遍历4,只遍历value:
value:value1
value:value33
可以看到:
遍历Set一般常用2种方式;
遍历List一般常用3种方式;
遍历Map一般常用4种方式;
根据使用场景,选择合适的遍历方式。
List&Map&Set的操作和遍历的更多相关文章
- 003-Tuple、Array、Map与文件操作入门实战
003-Tuple.Array.Map与文件操作入门实战 Tuple 各个元素可以类型不同 注意索引的方式 下标从1开始 灵活 Array 注意for循环的until用法 数组的索引方式 上面的for ...
- Guava中这些Map的骚操作,让我的代码量减少了50%
原创:微信公众号 码农参上,欢迎分享,转载请保留出处. Guava是google公司开发的一款Java类库扩展工具包,内含了丰富的API,涵盖了集合.缓存.并发.I/O等多个方面.使用这些API一方面 ...
- vector 与map的下标操作
1.vector的下标操作不会添加元素,只能针对已经存在的元素操作. 2.map的下标操作具有副作用,key不存在,会在map中添加一个具有该key的新元素,新元素的value使用默认构造方法. 3. ...
- java中集合的增删改操作及遍历总结
集合的增删改操作及遍历总结
- Java map 详解 - 用法、遍历、排序、常用API等
尊重原创: http://www.cnblogs.com/lzq198754/p/5780165.html 概要: java.util 中的集合类包含 Java 中某些最常用的类.最常用的集合类是 L ...
- Map的常用操作
public static void main(String[] args) { Map<String, String> map = new HashMap<>(); map. ...
- Map集合的几种遍历方式
Map<String ,String> map=new HashMap<String,String>(); map.put("1","value1 ...
- golang在多个go routine中进行map或者slice操作应该注意的对象。
因为golang的map和列表切片都是引用类型,且非线程安全的,所以在多个go routine中进行读写操作的时候,会产生“map read and map write“的panic错误. 某一些类型 ...
- pandas-05 map和replace操作
# pandas-05 map和replace操作 map可以做一个映射,对于操作大型的dataframe来说就非常方便了,而且也不容易出错.replace的作用是替换,这个很好理解. import ...
随机推荐
- Lintcode: Implement Queue by Stacks 解题报告
Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...
- hdu1198(模拟搜索)
这个题目,比较恶心,思路很是简单,就是模拟的时候有些麻烦......水题 #include<iostream> #include<cstdio> #include<cst ...
- mysql-connector-java-5.1.22下载
java连接mysql时,需要安装驱动.如果未安装,会出现找不到“com.mysql.jdbc.Driver”的错误. 最新版驱动是:mysql-connector-java-5.1.22 下载地址: ...
- python-urllib&urllib2模块
GET #!/usr/bin/env python # encoding: utf-8 import urllib import urllib2 url = "http://127.0.0. ...
- java基础篇---内存分析
Java的并发采用的是共享内存模型(而非消息传递模型),线程之间共享程序的公共状态,线程之间通过写-读内存中的公共状态来隐式进行通信.多个线程之间是不能直接传递数据交互的,它们之间的交互只能通过共享变 ...
- sphinx增量索引和主索引来实现索引的实时更新
项目中文章的信息内容因为持续有新增,而文章总量的基数又比较大,所以做搜索的时候,用了主索引+增量索引这种方式来实现索引的实时更新. 实现原理: 1. 新建一张表,记录一下上一次已经创建好索引的最后一条 ...
- 将本地的项目推送到github上
好像还是不能用git在本地直接建一个repository,然后推送到github,这是把本地项目推送到github上已经建好的裤 …or create a new repository on the ...
- Java NIO 系列教程 <转>
Java NIO提供了与标准IO不同的IO工作方式: Channels and Buffers(通道和缓冲区):标准的IO基于字节流和字符流进行操作的,而NIO是基于通道(Channel)和缓冲区(B ...
- E-trunk和Eth-trunk 区别
之前一直把E-trunk和Eth-trunk当作一回事,其实不然,简单说明一下. 同一设备链路聚合Eth-Trunk 一台交换机将这多个接口捆绑,形成一个Eth-Trunk接口,从而实现了增加带宽和提 ...
- JProfiler 9版本注册码(亲测可用!!!)
JProfiler 9版本注册码(亲测可用!!!) 按默认选择“Single or evaluation license” ,Name 和 Company 随意填!!! JProfiler 9.2 ...