Collection
    -----List
               -----LinkedList    非同步
                ----ArrayList      非同步,实现了可变大小的元素数组
                ----Vector          同步
                         ------Stack
    -----Set   不允许有相同的元素
Map
    -----HashTable        同步,实现一个key--value映射的哈希表
    -----HashMap          非同步,
    -----WeakHashMap   改进的HashMap,实现了“弱引用”,如果一个key不被引用,则被GC回收
 
 

Collection


在我们编程的时候,有时候需要集中存放多个数据,可以用数组来保存多个数据,但是数组的长度是不可变的,一旦数组的长度确定了之后就无法再改变,如果要保存可变长度的数据的话,数组肯定是不行的了。而且数组也无法保存具有一定关联的数据,比如:数学–80,英语–50。为了可以保存上面的这些信息,java提供了集合类,主要是负责保存、盛装数据的。因此集合相当于一个容器类。
    集合类有两个派生类,CollectionMap,本篇文章主要讲解Collection接口


package java.util; public interface Collection<E> extends Iterable<E> {
// Query Operations
//查询的一些方法 int size();
boolean isEmpty();
boolean contains(Object o);
Iterator<E> iterator();
Object[] toArray();
<T> T[] toArray(T[] a); // Modification Operations
//修改的一些方法
boolean add(E e);
boolean remove(Object o); // Bulk Operations
//一些批量操作的方法
boolean containsAll(Collection<?> c);
boolean addAll(Collection<? extends E> c);
boolean removeAll(Collection<?> c);
boolean retainAll(Collection<?> c);
void clear(); // Comparison and hashing
//和hash对比
boolean equals(Object o);
int hashCode();

List

List的长度可变。
    List集合像一个数组,是有序的。

0 1 2 3 4 5 6
ele1 ele2 ele3 ele4 ele5 ele6 ele7

List集合的一些基本操作

List<String>    list    =    new    ArrayList<String>();
list.add("testone");
list.add(“testtwo”); //List遍历
//第一种:这种方式在循环执行过程中会进行数据锁定,性能稍差,同时,如果你想在寻欢过程中去掉某个元素,只能调用it.remove方法,不能使list.remove方法,否则一定出现并发访问的错误.
for(Iterator<String> it = ist.iterator(); it.hasNext();) {
....
} //第二种:内部调用第一种,换汤不换药,因此比Iterator慢,这种循环方式还有其他限制,不建议使用它。
for(String data:list) {
.....
} //第三种:内部不锁定,效率最高,但是当写多线程时要考虑并发操作的问题。
for(int i=0;i<list.size();i++) {
A a=list.get(i);
...
}

Set

Set集合是无序的,元元素不允许重复。

HashSet h=new HashSet();
h.add("1st");
h.add("2nd");
h.add(new Integer(3));
h.add(new Double(4.0));
h.add("2nd"); //重复元素,未被添加
h.add(new Integer(3)); //重复元素,未被添加
h.add(new Date());
System.out.println("开始:size="+h.size());
Iterator it=h.iterator();
while(it.hasNext())
{
Object o=it.next();
System.out.println(o);
} h.remove("2nd");
System.out.println("移除元素后:size="+h.size());
System.out.println(h);

Map


Map是java集合的另一个根接口,下图中是Map体系中一些常用的实现类。

map也像一个罐子,不过在map中存储的时候都是以键值对的方式存储的(key-value方式)。存储的时候,key值是不能重复的,相当于索引,而value值是可以重复的。查询value值时通过key进行查询。

package com.jackey.topic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set; //循环遍历map的方法
public class CircleMap
{ public static void main(String[] args) { Map<String, Integer> tempMap = new HashMap<String, Integer>();
tempMap.put("a", 1);
tempMap.put("b", 2);
tempMap.put("c", 3);
// JDK1.4中 // 遍历方法一 hashmap entrySet() 遍历
System.out.println("方法一");
Iterator it = tempMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println("key=" + key + " value=" + value);
}
System.out.println(""); // JDK1.5中,应用新特性For-Each循环
// 遍历方法二
System.out.println("方法二");
for (Map.Entry<String, Integer> entry : tempMap.entrySet()) {
String key = entry.getKey().toString();
String value = entry.getValue().toString();
System.out.println("key=" + key + " value=" + value);
}
System.out.println(""); // 遍历方法三 hashmap keySet() 遍历
System.out.println("方法三");
for (Iterator i = tempMap.keySet().iterator(); i.hasNext();) {
Object obj = i.next();
System.out.println(obj);// 循环输出key
System.out.println("key=" + obj + " value=" + tempMap.get(obj));
}
for (Iterator i = tempMap.values().iterator(); i.hasNext();) {
Object obj = i.next();
System.out.println(obj);// 循环输出value
}
System.out.println(""); // 遍历方法四 treemap keySet()遍历
System.out.println("方法四");
for (Object o : tempMap.keySet()) {
System.out.println("key=" + o + " value=" + tempMap.get(o));
}
System.out.println("11111"); // java如何遍历Map <String, ArrayList> map = new HashMap <String,
// ArrayList>();
System.out.println("java 遍历Map <String, ArrayList> map = new HashMap<String, ArrayList>();");
Map<String, ArrayList> map = new HashMap<String, ArrayList>();
Set<String> keys = map.keySet();
Iterator<String> iterator = keys.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
ArrayList arrayList = map.get(key);
for (Object o : arrayList) {
System.out.println(o + "遍历过程");
}
}
System.out.println("2222");
Map<String, List> mapList = new HashMap<String, List>();
for (Map.Entry entry : mapList.entrySet()) {
String key = entry.getKey().toString();
List<String> values = (List) entry.getValue();
for (String value : values) {
System.out.println(key + " --> " + value);
}
}
}
}

Map和Collection详解的更多相关文章

  1. (7)Java数据结构--集合map,set,list详解

    MAP,SET,LIST,等JAVA中集合解析(了解) - clam_clam的专栏 - CSDN博---有颜色, http://blog.csdn.net/clam_clam/article/det ...

  2. js数组中foEach和map的用法详解 jq中的$.each和$.map

    数组中foEach和map的用法详解 相同点: 1.都是循环遍历数组(仅仅是数组)中的每一项. 2.forEach() 和 map() 里面每一次执行匿名函数都支持3个参数:数组中的当前项value, ...

  3. STL map 常见用法详解

    <算法笔记>学习笔记 map 常见用法详解 map翻译为映射,也是常用的STL容器 map可以将任何基本类型(包括STL容器)映射到任何基本类型(包括STL容器) 1. map 的定义 / ...

  4. hadoop学习WordCount+Block+Split+Shuffle+Map+Reduce技术详解

    转自:http://blog.csdn.net/yczws1/article/details/21899007 纯干货:通过WourdCount程序示例:详细讲解MapReduce之Block+Spl ...

  5. [转]Java中Map的用法详解

    转载地址:http://www.zhixing123.cn/jsp/30113.html Map简介 将键映射到值的对象.一个映射不能包含重复的键:每个键最多只能映射到一个值.此接口取代 Dictio ...

  6. Java中Map的用法详解

    Map简介 将键映射到值的对象.一个映射不能包含重复的键:每个键最多只能映射到一个值.此接口取代 Dictionary 类,后者完全是一个抽象类,而不是一个接口. Map 接口提供三种collecti ...

  7. jQuery数组($.grep,$.each,$.inArray,$.map)处理函数详解

    1.jQuery.grep( array, function(elementOfArray, indexInArray) [, invert ] ) 描述: 查找满足过滤函数的数组元素.原始数组不受影 ...

  8. Android中Google地图路径导航,使用mapfragment地图上画出线路(google map api v2)详解

    在这篇里我们只聊怎么在android中google map api v2地图上画出路径导航,用mapfragment而不是mapview,至于怎么去申请key,manifest.xml中加入的权限,系 ...

  9. map函数用法详解

    map函数是Python内置的高阶函数,它是一个典型的函数式编程例子.它的参数为: 一个函数function.一个或多个sequence.通过把函数function依次作用在sequence的每个元素 ...

随机推荐

  1. 记一次mysql性能优化过程

    摘要: 所谓mysql的优化,三分是配置的优化,七分是sql语句的优化,通过一些案例分析,希望给大家在工作中带来一些思路 由于配置是运行过那么长时间,很稳定,基本上不考虑,所以本次主要是sql的优化, ...

  2. SpringBoot学习笔记(7)-----CORS支持解决跨域问题

    在实际应用开发中,跨域是一个比较常见的问题,解决方法可以用jsonp,frame,cors等, 这里示例的是SpringBoot对CORS的支持的三种实现方式 第一种:配置一种全局的支持,这种方式需要 ...

  3. React-Router-API中文介绍

    React-Router API 以下内容翻译自react-router/doc/API.md,方便使用时查看,之前的学习都是能够工作即可,但一些内在发生的行为并不知晓,借此理解一番: ##Compo ...

  4. Hadoop_MapReduce中Mapper类和Reduce类

    在权威指南中,有个关于处理温度的MapReduce类,具体如下: 第一部分:Map public class MaxTemperatureMapper extends MapReduceBase im ...

  5. document.body

    比如document.body,最好是写成document.getElementsByTagName("body")[0];

  6. TP框架传值

    /*TP框架传值*/ location.href = "../add/add/department/"+department+"/username/"+user ...

  7. POJ 2228 Naptime(DP+环形处理)

    题解 这题一眼望去DP. 发现自己太智障了. 这题想的是O(n^3m)的. 环形处理只会断环成链....然后DP也想的不好. 我们先考虑如果除去环这题该怎么做? dp[i][j][0/1]代表到第i小 ...

  8. mac pro配置php开发环境

    mac pro自带php和apache,所以我们只要配置下就好了 // 启动Apache服务 sudo apachectl start // 重启Apache服务 sudo apachectl res ...

  9. 紫书 习题 11-10 UVa 12264 (二分答案+最大流)

    书上写的是UVa 12011, 实际上是 12264 参考了https://blog.csdn.net/xl2015190026/article/details/51902823 这道题就是求出一种最 ...

  10. Zookeeper入门-Linux环境下异常ConnectionLossException解决

    实际项目开发中,用的是Linux环境.  中午突然断电,死活连不上Zookeeper,最终发现是需要关闭防火墙.    看日志,报错如下:  Exception in thread "mai ...