Java | HashMap 语法

	HashMap 是一个散列表,它存储的内容是键值对(key-value)映射。
    HashMap 实现了 Map 接口,根据键的 HashCode 值存储数据,具有很快的访问速度,最多允许一条记录的键为 null,不支持线程同步。
    HashMap 是无序的,即不会记录插入的顺序。
    HashMap 继承于AbstractMap,实现了 Map、Cloneable、java.io.Serializable 接口。

1、创建 HashMap对象 HashMap<key,value> = new HashMap<>()

// 创建 HashMap对象 HashMap<key,value> = new HashMap<>()
        HashMap<Integer,String> sites = new HashMap<>();


2、添加键值对 hashmap.put(key,value)

// 添加键值对 hashmap.put(key,value)
        sites.put(1, "Google");
        sites.put(2, "Runoob");
        sites.put(3, "Taobao");
        sites.put(4, "Zhihu");

3、访问 所有键值对 hashmapName

// 访问 所有键值对 hashmapName
        System.out.println(sites);

4、访问 指定键值对 hashmap.get(key)

// 访问 指定键值对 hashmap.get(key)
        System.out.println(sites.get(3));

5、删除 指定键值对 hashmap.remove(key)

// 删除 指定键值对 hashmap.remove(key)
        sites.remove(4);
        System.out.println(sites);

6、计算大小 hashmap.size()

 // 计算大小 hashmap.size()
        System.out.println(sites.size());

7、访问 hashMap 中所有 value 组成的集合视图 hashmap.values()

// 访问 hashMap 中所有 value 组成的集合视图 hashmap.values()
        System.out.println(sites.values());

8、访问 hashMap 中所有 key 组成的集合视图 hashmap.keySet()

// 访问 hashMap 中所有 key 组成的集合视图 hashmap.keySet()
        System.out.println(sites.keySet());

9、访问 hashMap 中所有映射项的集合集合视图 hashmap.entrySet()

// 访问 hashMap 中所有映射项的集合集合视图 hashmap.entrySet()
        System.out.println(sites.entrySet());

10、通过 hashmap.entrySet 遍历key和value

// 通过 hashmap.entrySet 遍历key和value
        for (Map.Entry<Integer, String> entry : sites.entrySet()) {
            System.out.println("key = " + entry.getKey() + " and value = " + entry.getValue());
        }

11、判断是否为空 hashmap.isEmpty()

// 判断是否为空 hashmap.isEmpty()
        System.out.println(sites.isEmpty());

12、删除所有键值对(key-value) hashmap.clear()

// 删除所有键值对(key-value) hashmap.clear()
        sites.clear();
        System.out.println(sites.isEmpty());

13、通过 hashmap.keySet 遍历key和value

// 通过 hashmap.keySet 遍历key和value
        System.out.println("-------------");
        for (String key : map.keySet()) {
            System.out.println("key = "+ key + " and value = " + map.get(key));
        }

14、通过 hashmap.values() 遍历所有的value,但不能遍历key

// 通过 hashmap.values() 遍历所有的value,但不能遍历key
        System.out.println("-------------");
        for (String value : map.values()) {
            System.out.println("value = " + value);
        }

15、通过 hashmap.entrySet 遍历key和value

// 通过 hashmap.entrySet 遍历key和value
        System.out.println("-------------");
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println("key = " + entry.getKey() + " and value = " + entry.getValue());
        }

16、通过 hashmap.entrySet 使用iterator遍历key和value

// 通过 hashmap.entrySet 使用iterator遍历key和value
        System.out.println("-------------");
        Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, String> entry = it.next();
            System.out.println("key = " + entry.getKey() + " and value = " + entry.getValue());
        }

17、求Map<Key,Value>中Value(值)的最小值

    /**
     * @param map
     * @Description 求Map<Key,Value>中Value(值)的最小值
     */
    public static Object getMinValue(Map<Integer, Integer> map) {
        if (map == null)
            return null;
        Collection<Integer> c = map.values();
        Object[] obj = c.toArray();
        Arrays.sort(obj);
        return obj[0];
    }

18、求Map<Key,Value>中Value(值)的最大值

   /**
     * @param map
     * @Description 求Map<Key,Value>中Value(值)的最大值
     */
    public static Object getMaxValue(Map<Integer, Integer> map) {
        if (map == null)
            return null;
        int length =map.size();
        Collection<Integer> c = map.values();
        Object[] obj = c.toArray();
        Arrays.sort(obj);
        return obj[length-1];
    }

19、按照key值对Hashmap进行升序排序

    /**
     * @param Hashmap HashMap<String,Integer>类型
     * @return Object[]类型
     * @Description 按照key值对Hashmap进行升序排序
     * */
    public static Object[] Sort_ByKey(HashMap<String,Integer> Hashmap){
        Set<String> set = Hashmap.keySet();
        Object[] arr = set.toArray();
        Arrays.sort(arr);
        return arr;
    }

20、通过 list.sort 方法 按照value值 对Hashmap进行降序排序

    /**
     * @param Hashmap HashMap<String,Integer>类型
     * @return List<Map.Entry<String, Integer>>类型
     * @Description 通过list.sort方法 按照value值 对Hashmap进行降序排序
     * */
    public static List Sort_ByValue(HashMap<String,Integer> Hashmap){
        List <Map.Entry<String, Integer>> list = new ArrayList<>(Hashmap.entrySet());
        list.sort((o1, o2) -> o2.getValue().compareTo(o1.getValue()));

//        list.sort(new Comparator<Map.Entry<String, Integer>>() {
//            @Override
//            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
//                return o2.get().compareTo(o1.get());
//            }
//        });
        return list;
    }

【附】源码

HashMap_BasicUsage

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

public class HashMap_BasicUsage {
    public static void main(String[] args){
        // 创建 HashMap对象 HashMap<key,value> = new HashMap<>()
        HashMap<Integer,String> sites = new HashMap<>();

        // 添加键值对 hashmap.put(key,value)
        sites.put(1, "Google");
        sites.put(2, "Runoob");
        sites.put(3, "Taobao");
        sites.put(4, "Zhihu");

        // 访问 所有键值对 hashmapName
        System.out.println(sites);

        // 访问 指定键值对 hashmap.get(key)
        System.out.println(sites.get(3));

        // 删除 指定键值对 hashmap.remove(key)
        sites.remove(4);
        System.out.println(sites);

        // 计算大小 hashmap.size()
        System.out.println(sites.size());


        // 访问 hashMap 中所有 value 组成的集合视图 hashmap.values()
        System.out.println(sites.values());

        // 访问 hashMap 中所有 key 组成的集合视图 hashmap.keySet()
        System.out.println(sites.keySet());

        // 访问 hashMap 中所有映射项的集合集合视图 hashmap.entrySet()
        System.out.println(sites.entrySet());

        // 通过 hashmap.entrySet 遍历key和value
        for (Map.Entry<Integer, String> entry : sites.entrySet()) {
            System.out.println("key = " + entry.getKey() + " and value = " + entry.getValue());
        }


        // 判断是否为空 hashmap.isEmpty()
        System.out.println(sites.isEmpty());

        // 删除所有键值对(key-value) hashmap.clear()
        sites.clear();
        System.out.println(sites.isEmpty());
    }
}

HashMap_MaximumValueGetter

import java.util.*;

public class HashMap_MaximumValueGetter {
    public static void main(String[] args) throws InterruptedException {
        Map<Integer, Integer> map = new HashMap<>();
        map.put(1,1);
        map.put(2,2);
        map.put(3,3);
        map.put(4,5);
        System.out.println(getMaxValue(map));
        System.out.println(getMinValue(map));
    }
    /**
     * @param map
     * @Description 求Map<Key,Value>中Value(值)的最小值
     */
    public static Object getMinValue(Map<Integer, Integer> map) {
        if (map == null)
            return null;
        Collection<Integer> c = map.values();
        Object[] obj = c.toArray();
        Arrays.sort(obj);
        return obj[0];
    }
    /**
     * @param map
     * @Description 求Map<Key,Value>中Value(值)的最大值
     */
    public static Object getMaxValue(Map<Integer, Integer> map) {
        if (map == null)
            return null;
        int length =map.size();
        Collection<Integer> c = map.values();
        Object[] obj = c.toArray();
        Arrays.sort(obj);
        return obj[length-1];
    }
}

 HashMap_Sort

import java.util.*;

public class HashMap_Sort {
    public static void main(String[] args) {
        HashMap<String, Integer> Hashmap = new HashMap<>();
        Hashmap.put("1", 123);
        Hashmap.put("8", 222);
        Hashmap.put("3", 345);
        Hashmap.put("7", 777);
        Hashmap.put("0", 687);

        // Sort_ByKey(map)
        for (Object key : Sort_ByKey(Hashmap))
            System.out.println("key = "+key + " value = " + Hashmap.get(key));

        System.out.println();

        // Sort_ByValue(Hashmap)
        List<Map.Entry<String, Integer>> list = Sort_ByValue(Hashmap);
        for (Map.Entry<String, Integer> mapping : list)
            System.out.println("key = "+mapping.getKey() + " value = " + mapping.getValue());
    }
    /**
     * @param Hashmap HashMap<String,Integer>类型
     * @return Object[]类型
     * @Description 按照key值对Hashmap进行升序排序
     * */
    public static Object[] Sort_ByKey(HashMap<String,Integer> Hashmap){
        Set<String> set = Hashmap.keySet();
        Object[] arr = set.toArray();
        Arrays.sort(arr);
        return arr;
    }
    /**
     * @param Hashmap HashMap<String,Integer>类型
     * @return List<Map.Entry<String, Integer>>类型
     * @Description 通过list.sort方法 按照value值 对Hashmap进行降序排序
     * */
    public static List Sort_ByValue(HashMap<String,Integer> Hashmap){
        List <Map.Entry<String, Integer>> list = new ArrayList<>(Hashmap.entrySet());
        list.sort((o1, o2) -> o2.getValue().compareTo(o1.getValue()));

//        list.sort(new Comparator<Map.Entry<String, Integer>>() {
//            @Override
//            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
//                return o2.get().compareTo(o1.get());
//            }
//        });
        return list;
    }
}

HashMap_Traverse

import java.util.*;

public class HashMap_Traverse {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();

        map.put("1", "value1");
        map.put("2", "value2");
        map.put("3", "value3");

        // 通过 hashmap.keySet 遍历key和value
        System.out.println("-------------");
        for (String key : map.keySet()) {
            System.out.println("key = "+ key + " and value = " + map.get(key));
        }

        // 通过 hashmap.values() 遍历所有的value,但不能遍历key
        System.out.println("-------------");
        for (String value : map.values()) {
            System.out.println("value = " + value);
        }

        // 通过 hashmap.entrySet 遍历key和value
        System.out.println("-------------");
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println("key = " + entry.getKey() + " and value = " + entry.getValue());
        }

        // 通过 hashmap.entrySet 使用iterator遍历key和value
        System.out.println("-------------");
        Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, String> entry = it.next();
            System.out.println("key = " + entry.getKey() + " and value = " + entry.getValue());
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值