java集合框架map

Map<K,V>

K key

V value

 

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

1,添加.

2,删除.

3,判断.

4,获取.

 

Map

|--Hashtable:底层是哈希表数据结构,不可以存入nullnull.该集合是线程同步的.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("张三", 10);
        map.put("赵四", 15);
        map.put("王武", 25);
        
        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("01", "zhangsan1"));
        print(map.put("01", "wangwu"));// 当存在相同键的时候,新的值会替代旧的值,而且会返回原来的值
        map.put("02", "zhangsan2");
        map.put("03", "zhangsan3");
        System.out.println("containsKey:" + map.containsKey("02")); // 是否包含此键.
        System.out.println("remove::" + map.remove("02")); // 根据键删除元素
        print("get:" + map.get("01")); // 获取
        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", 14), "东风大街16号");
        map.put(new Student("zhouqitong", 15), "东风大街13号");
        map.put(new Student("qiuyingjian", 11), "东风大街13号");
        map.put(new Student("liuyong", 10), "东风大街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 == 0) 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 * 34; } 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", 23), "nanjing");
        tm.put(new Student("lisi1", 21), "shanghai");
        tm.put(new Student("lisi5", 22), "hangzhou");
        tm.put(new Student("lisi2", 26), "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 == 0)
            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 == 0)
            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 * 34;
    }

    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; // 这里只要判断是否相等
    }
}

 

posted @ 2014-12-01 16:03  wikiki  阅读(170)  评论(0编辑  收藏  举报