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. sql随机

    想从MySQL数据库中随机取一条或者N条记录时,最好把RAND()生成随机数放在JOIN子查询中以提高效率. SELECT id FROM table ORDER BY RAND() LIMIT n; ...

  2. linux yum安装mongodb

    1.yum -y install mongodb-server  mongodb 2.service mongod start                     #启动mongodb 服务 3. ...

  3. wordpress建站过程4——index.php

    <?php get_header(); ?> <div id="primary" class="content-area col-md-9"& ...

  4. 《C++ Primer》之重载操作符与转换(中)

    赋值操作符 类赋值操作符接受类类型形参,通常,该形参是对类类型的 const 引用,但也可以是类类型或对类类型的非 const 引用.如果没有定义这个操作符,则编译器将合成它.类赋值操作符必须是类的成 ...

  5. Chapter 2 Open Book——2

    It was worse because I was tired; 更糟糕的是因为我疲惫了. I still couldn't sleep with the wind echoing around t ...

  6. find排除目录

    在linux find 进行查找的时候,有时候需要忽略某些目录不查找,可以使用 -prune 参数来进行过滤,但必须要注意要忽略的路径参数必须紧跟着搜索的路径之后,否则该参数无法起作用. 命令语法: ...

  7. Linux中seq命令的用法

    用于产生从某个数到另外一个数之间的所有整数 例一: # seq 1 10 结果是1 2 3 4 5 6 7 8 9 10 例二: #!/bin/bash for i in `seq 1 10`; do ...

  8. 运行出第一个程序Hello World、第二个程序网页浏览器

    很长时间没有发博客记录我的奋斗历程了,原因不外乎遇到了对我而言“巨大的”困难. 经历了长期的找不到合适的教材.找不到Xcode资源.运行不出例程的痛苦以后,近日终于走上正轨. 现在补发一个过去2个月来 ...

  9. strictmode

    最新的Android平台中(Android 2.3起),新增加了一个新的类,叫StrictMode(android.os.StrictMode).这个类可以用来帮助开发者改进他们编写的应用,并且提供了 ...

  10. HttpCookie类

    转自:http://www.cnblogs.com/kissdodog/archive/2013/01/08/2851937.html HttpCookie类专门由C#用于读取和写入Cookie的类. ...