Java学习笔记32

Map接口与Collection不同:

Collection中的集合成分是孤立的,可领略为单身,是多少个多个存进去的,称为单列集结

Map中的集结成分是成对存在的,可理解为夫妇,是一对一对存进来的,称为双列群集

Map中存入的是:键值对,键不得以重复,值能够重复

 

Map接口中的常用集结:

1.HashMap:哈希表的积累结构,不过力不胜任担保存取顺序

2.LinkedHashMap:存储数据接纳的是哈希表和链表,能够有各样

 

Map接口的常用方法:

示例:

package demo;

import java.util.HashMap;
import java.util.Map;

public class MapDemo {
    public static void main(String[] args) {
        function1();
        function2();
        function3();
    }

    public static void function1() {
        // 将键值对存储到集合中
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("a", 1);
        map.put("b", 2);
        map.put("c", 3);
        map.put("c", 4);
        System.out.println(map);
        // {b=2, c=4, a=1}
        // =连接键值对,存入重复键,则会覆盖
    }

    public static void function2() {
        //通过键获取值
        Map<Integer, String> map = new HashMap<Integer, String>();
        map.put(1, "a");
        map.put(2, "b");
        map.put(3, "c");
        String value = map.get(1);
        System.out.println(value);//a
        //有则返回值,不存在返回null
    }
    public static void function3(){
        //移除集合中的键值对
        Map<Integer, String> map = new HashMap<Integer, String>();
        map.put(1, "a");
        map.put(2, "b");
        map.put(3, "c");
        map.remove(3);
        System.out.println(map);
        //{1=a, 2=b}
    }
}

 

Map接口的遍历:

第一种艺术:

package demo;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class MapDemo {
    public static void main(String[] args) {
        function1();
        function2();
    }

    public static void function1() {
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("a", 1);
        map.put("b", 2);
        map.put("c", 3);
        map.put("d", 4);
        Set<String> set = map.keySet();
        Iterator<String> it = set.iterator();
        while (it.hasNext()) {
            String key = it.next();
            Integer value = map.get(key);
            System.out.println(key + "<==>" + value);
        }
    }

    public static void function2() {
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("a", 1);
        map.put("b", 2);
        map.put("c", 3);
        map.put("d", 4);
        for (String key : map.keySet()) {
            Integer value = map.get(key);
            System.out.println(key + "<==>" + value);
        }
    }
}
// 遍历输出的无序

 

第三种办法(依据映射关系):

package demo;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class MapDemo {
    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<Integer, String>();
        map.put(1, "a");
        map.put(2, "b");
        map.put(3, "c");
        Set<Entry<Integer, String>> set = map.entrySet();
        Iterator<Entry<Integer, String>> it = set.iterator();
        while (it.hasNext()) {
            Entry<Integer, String> entry = it.next();
            Integer key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + "<==>" + value);
        }
    }
}

 

存款和储蓄自定义对象:

 

package demo;

public class Person {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

    public Person() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Person other = (Person) obj;
        if (age != other.age)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

}

package demo;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

//存储自定义类型
public class HashMapDemo {
    public static void main(String[] args) {
        function1();
        function2();
    }

    public static void function1() {
        // 自定义类型作为值出现
        HashMap<String, Person> map = new HashMap<String, Person>();
        map.put("a", new Person("1", 20));
        map.put("b", new Person("2", 20));
        map.put("c", new Person("3", 20));
        // 利用两种遍历
        for (String key : map.keySet()) {
            Person value = map.get(key);
            System.out.println(key + "<==>" + value);
        }
        for (Entry<String, Person> entry : map.entrySet()) {
            String key = entry.getKey();
            Person value = entry.getValue();
            System.out.println(key + "<==>" + value);
        }
    }

    public static void function2() {
        // 自定义类型作为键出现
        // 保证键的唯一性,需要重写hashcode和equals方法
        HashMap<Person, String> map = new HashMap<Person, String>();
        map.put(new Person("a", 20), "a");
        map.put(new Person("b", 20), "a");
        map.put(new Person("c", 20), "a");
        map.put(new Person("c", 20), "a");
        // 两种遍历
        for (Person key : map.keySet()) {
            String value = map.get(key);
            System.out.println(key + "<==>" + value);
        }
        for (Entry<Person, String> entry : map.entrySet()) {
            System.out.println(entry.getKey() + "<==>" + entry.getValue());
        }
    }

}

 

LinkedHashMap集合:

package demo;

import java.util.LinkedHashMap;

public class LinkedHashMapDemo {
    public static void main(String[] args) {
        LinkedHashMap<String, String> link = new LinkedHashMap<String, String>();
        link.put("1", "a");
        link.put("2", "a");
        link.put("3", "a");
        link.put("4", "a");
        System.out.println(link);
        //{1=a, 2=a, 3=a, 4=a}
        //存取顺序一致
    }
}

 

set接口下还可能有一个hashtable集结,可是过时了,今后由hashmap代替

只是,要当心叁个主题材料:

HashMap允许存款和储蓄null值,HashTable不允许存款和储蓄null值,二种都不容许存款和储蓄null键