HashMap常用方法

简介:

当需要对元素进行计数时,HashMap非常有用,如下例子,统计一个字符串中每个字符出现的次数:

复制代码
package simplejava;

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

public class Q12 {

    public static void main(String[] args) {
        HashMap<Integer, Integer> countMap = new HashMap<Integer, Integer>();
        // .... a lot of a’s like the following
        
        String chars = "abcabcabcghgk";
        
        for(int i = 0; i < chars.length(); i++){
            int a = chars.charAt(i);
            if (countMap.keySet().contains(a)) {
                countMap.put(a, countMap.get(a) + 1);
            } else {
                countMap.put(a, 1);
            }
        }
        
        
        for(Entry<Integer, Integer> e : countMap.entrySet()){
            System.out.println((char)(int)e.getKey() + " " + e.getValue());
        }

    }

}
复制代码

输出结果:

g 2
b 3
c 3
a 3
k 1
h 1

HashMap遍历

复制代码
        Map<Integer, Integer> mp = new HashMap<Integer, Integer>();
        Iterator it = mp.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry) it.next();
            System.out.println(pairs.getKey() + " = " + pairs.getValue());
        }
        
        
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
复制代码

打印HashMap的元素

复制代码
    public static void printMap(Map mp) {
        Iterator it = mp.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry) it.next();
            System.out.println(pairs.getKey() + " = " + pairs.getValue());
            it.remove(); // avoids a ConcurrentModificationException
        }
    }
复制代码

根据键值对的value排序

以下代码往TreeMap的构造函数传入一个比较器,来对map进行排序:

复制代码
package simplejava;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

class ValueComparator implements Comparator<String> {
    Map<String, Integer> base;

    public ValueComparator(Map<String, Integer> base) {
        this.base = base;
    }

    public int compare(String a, String b) {
        if (base.get(a) >= base.get(b)) {
            return -1;
        } else {
            return 1;
        } // returning 0 would merge keys
    }
}

public class Q12 {
    public static void printMap(Map mp) {
        Iterator it = mp.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry) it.next();
            System.out.println(pairs.getKey() + " = " + pairs.getValue());
            it.remove(); // avoids a ConcurrentModificationException
        }
    }

    public static void main(String[] args) {
        HashMap<String, Integer> countMap = new HashMap<String, Integer>();
        // add a lot of entries
        countMap.put("a", 10);
        countMap.put("b", 20);
        ValueComparator vc = new ValueComparator(countMap);
        TreeMap<String, Integer> sortedMap = new TreeMap<String, Integer>(vc);
        sortedMap.putAll(countMap);
        printMap(sortedMap);

    }

}
复制代码

虽然有很多种方法来对HashMap进行排序,但以上这种方法在stackoverflow中是最被推崇的;

注:使用了一个比较器Comparator对TreeMap排序,该比较器比较key的方式是取出key对应的value进行大小比较;

本文转自风一样的码农博客园博客,原文链接:http://www.cnblogs.com/chenpi/p/5493678.html,如需转载请自行联系原作者

相关文章
|
5月前
|
索引
HashMap中hash()方法的位运算
HashMap中hash()方法的位运算
HashMap中hash()方法的位运算
|
5月前
|
存储 安全 Java
HashMap的使用,以及内置方法
HashMap的使用,以及内置方法
|
存储 Java C++
HashMap 之继承结构和基本方法
HashMap 是 Java 中常用的数据结构之一,hash 是散列的意思,Map 有映射表的意思,于是 HashMap 就是散列表的意思,它存储的内容是键值对。Java8 之前,HashMap 在存储大量数据时,查询效率并不是非常高,但在 Java8,HashMap 的底层实现发生了一些改变,引进了一些新的技术,如当索引值大于或等于 8 时,数据结构将会由链表转换为红黑树,目的是为了提高数据的查询效率等。
261 1
|
5月前
|
存储 Java 索引
【JAVA】HashMap的put()方法执行流程
【JAVA】HashMap的put()方法执行流程
|
5月前
|
机器学习/深度学习
HashMap中tableSizeFor()方法详解
HashMap中tableSizeFor()方法详解
HashMap中tableSizeFor()方法详解
HashMap中put()方法源码详解
HashMap中put()方法源码详解
|
5月前
|
存储 算法 安全
HashMap 的常用方法
HashMap 的常用方法
34 0
|
5月前
|
Go C语言 C#
HashMap中putMapEntries()方法源码详解
HashMap中putMapEntries()方法源码详解
|
存储 安全 索引
HashMap的put方法的具体流程
HashMap的put()方法用于向HashMap中添加键值对。
174 0
|
5月前
|
索引
HashMap的put方法的具体流程
HashMap的put方法的具体流程