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. php 分页类(3)

    <?php class Page { private $total; //总记录 private $pagesize; //每页显示多少条 private $limit; //limit pri ...

  2. android Bind机制(二)

    1.binder通信概述 binder通信是一种client-server的通信结构,    1.从表面上来看,是client通过获得一个server的代理接口,对server进行直接调用:    2 ...

  3. tmux commands

    最近在学Linux,用到tmux这个命令,看到很多快捷键的介绍,个人觉得不太好用,因此把几个常用的命令记录下来,以便以后学习和使用. 常用tmux commands: tmux ls         ...

  4. BaLaBaLa

    #include<cstdio>#include<cstring>#include<cmath>#include<queue>#include<a ...

  5. 能加载文件或程序集“XXX”或它的某一个依赖项,系统找不到指定的文件

    能加载文件或程序集“XXX”或它的某一个依赖项,系统找不到指定的文件 http://blog.csdn.net/pplcheer/article/details/7796211 做项目总是遇到各种的问 ...

  6. OpenCV成长之路:图像滤波

    http://ronny.blog.51cto.com/8801997/1394138 OpenCV成长之路:图像滤波 2014-04-11 14:28:44 标签:opencv 边缘检测 sobel ...

  7. hdu_5293_Tree chain problem(DFS序+树形DP+LCA)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5293 被这题打蹦了,看着题解写的,很是爆炸,确实想不到,我用的DFS序+LCA+树形DP,当然也可以写 ...

  8. LeetCode OJ 98. Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  9. 我的 Github 个人博客是怎样炼成的

    Joey's Blog 长大后才发现政府建造 GFW 真是太 TM 机智了,由于本人自制力较差,且不说 91porn, youporn 等两性知识网站的超强战斗力,单单一个Youtube就可以让我瞬间 ...

  10. [code]判断周期串

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include<stdio.h> #include<string.h> ...