首页 > 编程 > Java > 正文

Java复习之Map接口

2019-11-06 08:19:16
字体:
来源:转载
供稿:网友
1.Map接口

public interface Map<K,V>

将键映射到值的对象,一个映射不能包含重复的键:每个键最多只能映射到一个值void clear()清空Map集合中的内容boolen containsKey(Object key)判断集合中是否存在指定的keyboolen containsValue(Object value)判断集合中是否存在指定的valueSet<Map.Entry<K,V>> entrySet()将Map接口变为set集合V get(Object key)根据key找到其对应的valueboolen isEmpty()判断是否为空Set<K> keySet()将全部的key变为Set集合Collection<V> values()将全部的value变为Collection集合V put(K key,V value)向集合中增加内容void putAll(Map<?extends K,extends V> m)增加一组集合V remove(Object key)增加key删除内容

2.HashMap

public class HashMap<K,V>

extends Map<K,V>,Clonable,Serializable基于哈希表的Map接口实现。此实现提供所有的可选的映射操作,并允许使用null值和null键。(除了非同步和允许使用null之外,HashMap类与Hashtable大致相同。)此类不保证映射的顺序,特别是它不保证顺序恒久不变

HashMap注意事项

1.基于哈希表+链表实现2.默认容量为16,加载因子为0.753.当哈希表需要重新撒列,会影响性能4.每次重新散列的方式:原数组长度*2

    public static void hashMap()    {        //创建一个HashMap对象,使用泛型的时候使用的是对象,进行装箱操作        Map<Integer,String >map =new HashMap<Integer, String>();        //添加数据        map.put(1,"白老师");        map.put(2,"苍老师");        map.put(3,"武老师");        //根据键取值        System.out.PRintln(map.get(1));        System.out.println(map.size());        System.out.println(map.containsKey(1));        System.out.println(map.containsValue("武老师"));        //获取map中的所有key对象的set        Set<Integer> keyset=map.keySet();        Iterator iterator =keyset.iterator();        while(iterator.hasNext())        {            Integer key=(Integer) iterator.next();            String value=map.get(key);            System.out.println("key="+key+",value="+value);        }    }

3.Hashtable

public class Hashtable<K,V>

extends Map<K,V>,Clonenable,Serializable此类实现一个哈希表,该哈希表将键值映射到相应的值,任何非null对象都可以用作键或者值。

为了成功地在哈希表中存储和获取对象,用作键的对象必须实现hashCode方法和equals方法

HashTable注意事项

1.键值不允许为空

2.默认的初始容量为11,加载因子为0.75

3.线程安全的,同步

 public static void hashTable()    {        Hashtable<String,String> hashtable=new Hashtable();        hashtable.put("1","包老师");        hashtable.put("2","曹老师");    }

4.Map集合的输出

在Collection接口中,可以使用iterator()方法为Iterator接口实例化,并进行输出操作,但是在Map接口中并没有此方法的定义,所以Map接口本身是不能直接使用Iterator进行输出的。

 1.使用map.keyset()方法把所有的key对象以转换成set集合, 然后迭代set集合取出每个key,再通过key从map中取值

 2.使用map.values()方法把所有的value对象转换成collection集合,然后遍历

 3.使用map.entrySet()方法把所有的Entry对象转换成set集合,然后进行迭代

Set<Map.Entry<K,V>> entrySet()返回此映射所包含的映射关系的Set视图Set<K> keyset()返回此映射中所包含的键的Set视图Collection<V> values()

返回此映射中所包含的值的collection视图

public static void print()    {        Map<Integer,String> map=new HashMap<>();        Integer i1=new Integer(1);        Integer i2=new Integer(2);        Integer i3=new Integer(3);        map.put(i1,"白老师");        map.put(i2,"苍老师");        map.put(i3,"武老师");        //方式2        Collection<String> c=map.values();        Iterator<String> iterator=c.iterator();        while(iterator.hasNext())        {            String value=iterator.next();            System.out.println(value);        }        //方式3        Set<Map.Entry<Integer,String>> entrys=map.entrySet();        Iterator<Map.Entry<Integer,String>> iterator1=entrys.iterator();        while (iterator1.hasNext())        {            Map.Entry<Integer,String> entry=iterator1.next();            System.out.println(entry.getKey()+"---"+entry.getValue());        }    }


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表