java集合框架map
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的更多相关文章
- java集合框架——Map
		
一.概述 1.Map是一种接口,在JAVA集合框架中是以一种非常重要的集合.2.Map一次添加一对元素,所以又称为“双列集合”(Collection一次添加一个元素,所以又称为“单列集合”)3.Map ...
 - Java集合框架——Map接口
		
第三阶段 JAVA常见对象的学习 集合框架--Map集合 在实际需求中,我们常常会遇到这样的问题,在诸多的数据中,通过其编号来寻找某一些信息,从而进行查看或者修改,例如通过学号查询学生信息.今天我们所 ...
 - Java集合框架Map接口
		
集合框架Map接口 Map接口: 键值对存储一组对象 key不能重复(唯一),value可以重复 常用具体实现类:HashMap.LinkedHashMap.TreeMap.Hashtable Has ...
 - JAVA集合框架 - Map接口
		
Map 接口大致说明(jdk11): 整体介绍: 一个将键映射到值的(key-value)对象, 键值(key)不能重复, 每个键值只能影射一个对象(一一对应). 这个接口取代了Dictionary类 ...
 - Java—集合框架Map
		
Map接口 Map提供了一种映射关系,其中的元素是以键值对(key-value)的形式存储的,key和value可以是任意类型的对象,能够实现根据key快速查找value. Map中的键值对以Entr ...
 - Java集合框架—Map
		
Map集合:该集合存储键值对.一对一对往里存.而且要保证键的唯一性. 1,添加. put(K key, V value) putAll(Map<? extends K,? extends V& ...
 - Java集合框架List,Map,Set等全面介绍
		
Java集合框架的基本接口/类层次结构: java.util.Collection [I]+--java.util.List [I] +--java.util.ArrayList [C] +- ...
 - Java集合框架之map
		
Java集合框架之map. Map的主要实现类有HashMap,LinkedHashMap,TreeMap,等等.具体可参阅API文档. 其中HashMap是无序排序. LinkedHashMap是自 ...
 - 【JAVA集合框架之Map】
		
一.概述.1.Map是一种接口,在JAVA集合框架中是以一种非常重要的集合.2.Map一次添加一对元素,所以又称为“双列集合”(Collection一次添加一个元素,所以又称为“单列集合”)3.Map ...
 
随机推荐
- python初识1
			
作者:武沛齐 出处:http://www.cnblogs.com/wupeiqi/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接. 安装Pyt ...
 - 用yum安装JDK(CentOS)
			
1.查看yum库中都有哪些jdk版本 [root@localhost ~]# yum search java|grep jdk 2.选择版本,进行安装 [root@localhost ~]# yum ...
 - Sql sever 常用语句(续)
			
distintct: 查询结果排除了重复项(合并算一项)--如查姓名 select distinct ReaName from UserInfo 分页语句:(查询区间时候应该查询出行号,作为分页的 ...
 - android  常用
			
1:常用之动画(View Animation,Drawable Animation,Property Animation) http://blog.csdn.net/huxueyan521/artic ...
 - C#连接ACCESS的一个问题
			
C# 连接ACCESS数据库有时候报 "Microsoft.Jet.Oledb.4.0"没有注册,其实,并不是真的没注册,可能是下面的原因 在菜单 “项目”的最下面 工程属性 菜单 ...
 - hdu 1020
			
//自信满满地交上去~~but...超时了 #include <iostream> #include <string.h> #include <stdio.h> u ...
 - spring security 3中的10个典型用法小结
			
spring security 3比较庞大,但功能很强,下面小结下spring security 3中值得 注意的10个典型用法 1)多个authentication-provide可以同时使用 &l ...
 - Mongoose如何实现统计查询、关联查询
			
[问题]Mongoose如何实现统计查询.关联查询 发布于 4 年前 作者 a272121742 13025 次浏览 最近业务上提出一个需求,要求能做统计,我们设计的文档集,统计可能跨越的文档会 ...
 - shell命令一行代码搞定【转】
			
查看文件内容-while: cat 1.txt|while read line;do echo $line;done while read line; do echo $line; done < ...
 - JS-将input输入框写入的小写字母全部转换成为大写字母的JS代码
			
<input name="htmer" type="text" onkeyup="this.value=this.value.toUpperCa ...