Map<K,V>

K key

V value

Map集合:该集合存储键值对.一对一对往里存,而且要保证键的唯一性.

1,添加.

2,删除.

3,判断.

4,获取.

Map

|--Hashtable:底层是哈希表数据结构,不可以存入null键null值.该集合是线程同步的.jdk1.0

|--HashMap: 底层是哈希表数据结构.允许使用null值和null键,该集合是不同步的.jdk1.2

        如果比较的是对象,这个要重写HashCode和equals

|--TreeMap: 底层是二叉树数据结构,不同步,可以用于给map集合中的键进行排序.

       如果比较的是对象,要继承comparable

和Set很像.

其实,set底层就是使用了Map集合.

可以通过get方法的返回值来判断一个键是否存在.

Map集合的两种取出方式。

1,Set<k>keySet:将map中所有的键存入到Set集合,因为set具备迭代器.

所以剖可以迭代方式取出所有键,然后根据get方法,获取每一个键对应的值.

2.entrySet

Map的两种输出方式

package pack;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set; public class Demo
{
public static void main(String args[])
{
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("张三", );
map.put("赵四", );
map.put("王武", ); Set<Entry<String, Integer>> entrySet = map.entrySet();
Iterator<Entry<String, Integer>> iterator2 = entrySet.iterator();
while(iterator2.hasNext()){
Entry<String, Integer> next = iterator2.next();
System.out.println("key"+next.getKey()+"value"+next.getValue());
} Set<String> keySet = map.keySet();
Iterator<String> iterator = keySet.iterator();
while(iterator.hasNext()){
String key = iterator.next();
Integer integer = map.get(key);
System.out.println(integer);
} }
}

//Map.Entry 其实Entey也是一个接口,它是Map接口中的一个内部接口.

package pack;

interface Map {
public static interface Entry // 接口中可以定义内部接口
{
public abstract Object getKey();
public abstract Object getValue();
}
} class HashMap1 implements Map.Entry {
public Object getKey() {
}; public Object getValue() {
};
} class HashMap2 implements Map {
class Hahs implements Map.Entry {
public Object getKey() {
}; public Object getValue() {
};
}
}

map集合应用

package pack;

import java.util.*;

public class Demo {
public static void main(String args[]) {
Map<String, String> map = new HashMap<String, String>();
// 添加元素
print(map.put("", "zhangsan1"));
print(map.put("", "wangwu"));// 当存在相同键的时候,新的值会替代旧的值,而且会返回原来的值
map.put("", "zhangsan2");
map.put("", "zhangsan3");
System.out.println("containsKey:" + map.containsKey("")); // 是否包含此键.
System.out.println("remove::" + map.remove("")); // 根据键删除元素
print("get:" + map.get("")); // 获取
map.put(null, "haha"); // 设置元素
print("get:" + map.get(null)); // 获取
// 可以通过get方法的返回值来判断一个键是否存在.通过返回null来判断
print(map); // values 返回值的 Collection 视图
Collection<String> coll = map.values();
print("over");
print(coll);
} public static void print(Object p) {
System.out.println(p);
}
}

map的应用

//由于是hashmap比较所以重写了hashcode和equals,这样可以检测重复
//如果是treemap比较,要重写comparable,
//否则如果你传一个没有实现comparable的对象放进treemap里面,会报异常
package pack;

import java.util.*;

public class Demo {
public static void main(String args[]) {
HashMap<Student, String> map = new HashMap<Student, String>();
map.put(new Student("litiepeng", ), "东风大街16号");
map.put(new Student("zhouqitong", ), "东风大街13号");
map.put(new Student("qiuyingjian", ), "东风大街13号");
map.put(new Student("liuyong", ), "东风大街11号"); Set<Map.Entry<Student, String>> entrySet = map.entrySet();
Iterator<Map.Entry<Student, String>> it = entrySet.iterator();
while (it.hasNext()) {
Map.Entry<Student, String> me = it.next();
Student s = me.getKey();
String add = me.getValue();
System.out.println(s.getName() + s.getAge() + add);
}
} }

class Student implements Comparable<Student> // 当同时要创建多个对象,最好有个自然排序
{
String name;
Integer age; public int compareTo(Student s) {
int num = new Integer(this.age).compareTo(new Integer(s.age)); if (num == )
return this.name.compareTo(s.name);
return num;
} Student(String name, Integer age) {
this.name = name;
this.age = age;
} public String getName() {
return name;
} public Integer getAge() {
return age;
} public int hashCode() {
return name.hashCode() + age * ;
} public boolean equals(Object obj) {
if (!(obj instanceof Student))
throw new ClassCastException("类型不匹配");
Student s = (Student) obj;
return this.name.equals(s.name) && this.age == s.age; // 这里只要判断是否相等
}
} class Address {
String add; Address(String add) {
this.add = add;
} public String getAddress() {
return add;
}
}

给treemap创建比较器,这个比较器作用在键上

package pack;

import java.util.*;
/**
* 给treemap创建比较器,这个比较器作用在键上
*/
public class Demo {
public static void main(String args[]) {
TreeMap<Student, String> tm = new TreeMap<Student, String>(
new MyComparator());
tm.put(new Student("lisi3", ), "nanjing");
tm.put(new Student("lisi1", ), "shanghai");
tm.put(new Student("lisi5", ), "hangzhou");
tm.put(new Student("lisi2", ), "yichun"); Set<Map.Entry<Student, String>> entrySet = tm.entrySet();
Iterator<Map.Entry<Student, String>> it = entrySet.iterator();
while (it.hasNext()) {
Map.Entry<Student, String> me = it.next();
Student s = me.getKey();
String add = me.getValue();
System.out.println(s.getAge() + ":::" + s.getName() + ":::" + add);
}
}
} class MyComparator implements Comparator<Student> // 这里按照姓名排序
{
public int compare(Student s1, Student s2) {
int num = s1.getName().compareTo(s2.getName());
if (num == )
return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
return num;
}
} class Student implements Comparable<Student> // 当同时要创建多个对象,最好有个自然排序
{
String name;
Integer age; public int compareTo(Student s) // 这里按照年龄排序
{
int num = new Integer(this.age).compareTo(new Integer(s.age)); if (num == )
return this.name.compareTo(s.name);
return num;
} Student(String name, Integer age) {
this.name = name;
this.age = age;
} public String getName() {
return name;
} public Integer getAge() {
return age;
} public int hashCode() {
return name.hashCode() + age * ;
} public boolean equals(Object obj) {
if (!(obj instanceof Student))
throw new ClassCastException("类型不匹配");
Student s = (Student) obj;
return this.name.equals(s.name) && this.age == s.age; // 这里只要判断是否相等
}
}

java集合框架map的更多相关文章

  1. java集合框架——Map

    一.概述 1.Map是一种接口,在JAVA集合框架中是以一种非常重要的集合.2.Map一次添加一对元素,所以又称为“双列集合”(Collection一次添加一个元素,所以又称为“单列集合”)3.Map ...

  2. Java集合框架——Map接口

    第三阶段 JAVA常见对象的学习 集合框架--Map集合 在实际需求中,我们常常会遇到这样的问题,在诸多的数据中,通过其编号来寻找某一些信息,从而进行查看或者修改,例如通过学号查询学生信息.今天我们所 ...

  3. Java集合框架Map接口

    集合框架Map接口 Map接口: 键值对存储一组对象 key不能重复(唯一),value可以重复 常用具体实现类:HashMap.LinkedHashMap.TreeMap.Hashtable Has ...

  4. JAVA集合框架 - Map接口

    Map 接口大致说明(jdk11): 整体介绍: 一个将键映射到值的(key-value)对象, 键值(key)不能重复, 每个键值只能影射一个对象(一一对应). 这个接口取代了Dictionary类 ...

  5. Java—集合框架Map

    Map接口 Map提供了一种映射关系,其中的元素是以键值对(key-value)的形式存储的,key和value可以是任意类型的对象,能够实现根据key快速查找value. Map中的键值对以Entr ...

  6. Java集合框架—Map

    Map集合:该集合存储键值对.一对一对往里存.而且要保证键的唯一性. 1,添加. put(K key, V value)  putAll(Map<? extends K,? extends V& ...

  7. Java集合框架List,Map,Set等全面介绍

    Java集合框架的基本接口/类层次结构: java.util.Collection [I]+--java.util.List [I]   +--java.util.ArrayList [C]   +- ...

  8. Java集合框架之map

    Java集合框架之map. Map的主要实现类有HashMap,LinkedHashMap,TreeMap,等等.具体可参阅API文档. 其中HashMap是无序排序. LinkedHashMap是自 ...

  9. 【JAVA集合框架之Map】

    一.概述.1.Map是一种接口,在JAVA集合框架中是以一种非常重要的集合.2.Map一次添加一对元素,所以又称为“双列集合”(Collection一次添加一个元素,所以又称为“单列集合”)3.Map ...

随机推荐

  1. mac 文件路径问题

    Finder栏显示访问路径 操作步骤: 打开“终端”(应用程序->实用工具),输入以下两条命令: defaults write com.apple.finder _FXShowPosixPath ...

  2. android 联网

    <?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="htt ...

  3. 关于PS的一些总结

    1.设计给的图,单独用里边的个别图层 打开图 — 新建一个图层(ctrl+n) — (点开上面的窗口排列-垂直排列,左下边下边自动选择改成图层)—选择移动工具,选中要移动的图层,拉到新建文件夹中.  ...

  4. EXCEL应用:高级筛选里的条件或和与的条件怎么写 例:不包含,包含等

    ============================================================= a列包含b列,在c列中显示b列信息, =INDEX(B:B,MIN(IF(I ...

  5. Eclipse的WorkingSet使用(转载)

    Eclipse作为一款流行的JavaIDE开发工具,其有很多好用的功能为我们的开发提供帮助.但我们的工作空间中有很多项目时,管理起来就很头疼了. 但是我们又不想更换工作区间,所以我们需要一个更加有效的 ...

  6. 关于prototype属性的理解

    众所周知,prototype是一个属性对象,只要创建一个新函数,就会根据特定的规则为该函数创建一个prototype属性,这个属性指向函数的原型对象.在默认情况下,所有原型对象都会自动获得一个cons ...

  7. 利用JFreeChart绘制股票K线图完整解决方案

    http://blog.sina.com.cn/s/blog_4ad042e50100q7d9.html 利用JFreeChart绘制股票K线图完整解决方案 (2011-04-30 13:27:17) ...

  8. shell脚本 整数比较

    1.整数比较  -eq  等于,如:if [ "$a" -eq "$b" ] -ne  不等于,如:if [ "$a" -ne " ...

  9. 通过配置的方式Autofac(1)

    一.基本配置 1.通过配置的方式使用Autofac <?xml version="1.0"?> <configuration> <configSect ...

  10. 还在纠结 Flux 或 Relay,或许 Redux 更适合你

    重磅消息,Redux 1.0 发布,终于可以放心用于生产环境了! 在这个端应用技术膨胀的时代,每天都有一大堆框架冒出,号称解决了 XYZ 等一系列牛 X 的问题,然后过一段时间就不被提起了.但开发的应 ...