栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java | HashMap 语法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java | HashMap 语法

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

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

// 创建 HashMap对象 HashMap = new HashMap<>()
        HashMap 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 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 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> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = it.next();
            System.out.println("key = " + entry.getKey() + " and value = " + entry.getValue());
        }

17、求Map中Value(值)的最小值

    
    public static Object getMinValue(Map map) {
        if (map == null)
            return null;
        Collection c = map.values();
        Object[] obj = c.toArray();
        Arrays.sort(obj);
        return obj[0];
    }

18、求Map中Value(值)的最大值

   
    public static Object getMaxValue(Map map) {
        if (map == null)
            return null;
        int length =map.size();
        Collection c = map.values();
        Object[] obj = c.toArray();
        Arrays.sort(obj);
        return obj[length-1];
    }

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

    
    public static Object[] Sort_ByKey(HashMap Hashmap){
        Set set = Hashmap.keySet();
        Object[] arr = set.toArray();
        Arrays.sort(arr);
        return arr;
    }

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

    
    public static List Sort_ByValue(HashMap Hashmap){
        List > list = new ArrayList<>(Hashmap.entrySet());
        list.sort((o1, o2) -> o2.getValue().compareTo(o1.getValue()));

//        list.sort(new Comparator>() {
//            @Override
//            public int compare(Map.Entry o1, Map.Entry 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 = new HashMap<>()
        HashMap 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 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 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));
    }
    
    public static Object getMinValue(Map map) {
        if (map == null)
            return null;
        Collection c = map.values();
        Object[] obj = c.toArray();
        Arrays.sort(obj);
        return obj[0];
    }
    
    public static Object getMaxValue(Map map) {
        if (map == null)
            return null;
        int length =map.size();
        Collection 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 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> list = Sort_ByValue(Hashmap);
        for (Map.Entry mapping : list)
            System.out.println("key = "+mapping.getKey() + " value = " + mapping.getValue());
    }
    
    public static Object[] Sort_ByKey(HashMap Hashmap){
        Set set = Hashmap.keySet();
        Object[] arr = set.toArray();
        Arrays.sort(arr);
        return arr;
    }
    
    public static List Sort_ByValue(HashMap Hashmap){
        List > list = new ArrayList<>(Hashmap.entrySet());
        list.sort((o1, o2) -> o2.getValue().compareTo(o1.getValue()));

//        list.sort(new Comparator>() {
//            @Override
//            public int compare(Map.Entry o1, Map.Entry 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 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 entry : map.entrySet()) {
            System.out.println("key = " + entry.getKey() + " and value = " + entry.getValue());
        }

        // 通过 hashmap.entrySet 使用iterator遍历key和value
        System.out.println("-------------");
        Iterator> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = it.next();
            System.out.println("key = " + entry.getKey() + " and value = " + entry.getValue());
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1006057.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号