星空网 > 软件开发 > Java

【Simple Java】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.programcreek.com/2013/04/frequently-used-methods-of-java-hashmap/




原标题:【Simple Java】HashMap常用方法

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

小件跨境物流哪个最便宜:https://www.goluckyvip.com/tag/93343.html
跨境电商物流主要有哪些:https://www.goluckyvip.com/tag/93344.html
常见的跨境电商物流有哪些:https://www.goluckyvip.com/tag/93345.html
跨境物流有哪几种方式:https://www.goluckyvip.com/tag/93346.html
跨境电商物流渠道有哪些:https://www.goluckyvip.com/tag/93348.html
跨境物流的禁运产品有哪些:https://www.goluckyvip.com/tag/93349.html
仿品独立站从建站、推广、收款到底怎么玩?:https://www.kjdsnews.com/a/1836312.html
仿品独立站从建站、推广、收款到底怎么玩?:https://www.goluckyvip.com/news/186215.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流