Java集合三

Map   

Map接口的实现类 Hashtable      底层数据结构  哈希表

虽然 Hashtable 是线程安全的    运行速度慢

但HashMap  是线程不安全的  运行速度快  

 

Hashtalbe 与 Vector一样  从JDK1.2开始 已经被后来者取代

HashMap 可以存null key  null value

但 Hashtalbe  不可存null key  null value

 

 

Hashtalbe 的子类 Properties 依然活跃在开发舞台  Properties类 表示了一个持久的属性集  在IO流中 有应用体现

 

 集合中的元素 是 键值对  key-value   key 不可重复   

keySet()方法  可以将集合中所有元素的key  保持在Set集合中  (hashSet)   

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



        Map<String , Integer> map = new HashMap<String , Integer>();
        map.put("a", 97);
        map.put("b", 98);
        map.put("c", 99);
        map.put("d", 100);
        map.put("e", 101);
        
        Set<String> set = map.keySet();

 

为集合增添元素

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

        Map<String , Integer> map = new HashMap<String , Integer>();
        
        map.put("a", 97);        
        System.out.println(map.put("b", 10));//null  向集合中添加元素  键值不重复 则返回null
        System.out.println(map.put("b", 66));//10 存储重复元素  返回被覆盖前的旧值

获取元素

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

        Integer i = map.get("b");
        System.out.println(i);//66    键值存在    则依据key 返回key对应的value值
        System.out.println(map.get("c"));//null 键值不存在  则返回null

 

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

     map.put("a", 97);
        map.put("b", 66);
        map.put(null, null);// 集合元素 可以是null~
        System.out.println(map); //{null=null, a=97, b=66}

 

 

 

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

Map<String , Integer> map = new HashMap<String , Integer>();
        map.put("a", 97);
        map.put("b", 98);
        map.put("c", 99);
        map.put("d", 100);
        map.put("e", 101);
        
        Set<String> set = map.keySet();
System.out.println(set.getClass()); //class java.util.HashMap$KeySet  $-HashMap的内部类
//Map集合 自身没有迭代器 借助Set集合的迭代器 利用Map的get()方法 实现了元素的遍历 Iterator<String> it = set.iterator(); while(it.hasNext()){ String key = it.next(); System.out.println(key+"="+map.get(key)+" "); }

 

Map 是一个集合接口

Entry  是一个 嵌套接口   该接口嵌套在 Map 中   Entry 是对键值对对应关系的封装  Entry 的getKey( ) 方法  getValue( )方法  可以获取出 对应关系中的 key  和 value

Entry  是一个接口  自身不能new出对象   它在Map中 被static修饰   因此作为内部类 可以使用  Map. Entry

所有的映射关系对象可装载在一个Set集合中,该集合可以通过 Map类的entrySet( )方法得到

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


Map<Integer , String> map = new HashMap<Integer , String>();
        map.put(97, "a");
        map.put(98, "b");
        map.put(99, "c");
        map.put(100, "d");
        
        // 内部类对象     外部类.内部类 Map.Entry<Integer , String>
        Set<Map.Entry<Integer , String>> set = map.entrySet();
        //借助Set集合进行迭代遍历   该集合中的元素 是 映射关系对象
        Iterator<Map.Entry<Integer , String>> it = set.iterator();
        while(it.hasNext()){
            
            Map.Entry<Integer , String> entry = it.next();
            System.out.println(entry.getKey() + "—"+ entry.getValue());
            
        }

 

Map  集合  与 Interable 无关

Colletion 集合  与 Interable 有关

在遍历Map 中的元素时  我们是借助了Set集合的 interator 迭代器

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() {
        super();
    }
    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (obj instanceof Person){
            Person p = (Person)obj;
            return name.equals(p.name) && age == p.age;
        }
        return false;
    }
    
    public String toString() {
        return "Person" + name + "-" + age;
    }
    

}
View Code

 

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



        HashMap<String, Person> map = new HashMap<String, Person> ();
        map.put("A1001", new Person("小李", 20));
        map.put("A1002", new Person("小李", 22));
        map.put("A1003", new Person("小张", 21));
        map.put("A1004", new Person("小刘", 24));
        
        
        Set<Map.Entry<String, Person>> set = map.entrySet();
        for(Map.Entry<String, Person> entry : set){
            String s = entry.getKey();
            Person p = entry.getValue();
            System.out.println(s+ " .. "+ p);
        }
        

 

posted @ 2020-05-18 19:23  CherryYang  阅读(155)  评论(0编辑  收藏  举报