zoukankan      html  css  js  c++  java
  • 题型总结之Hash Table

    Map常用场景

    # map来track每个元素的index
    Map <item, index> map = new HashMap<>();
    
    # map来track每个元素出现的频率
    Map <item, freq> map = new HashMap<>();
    
    # char字符在ASCII中都有对应,故可简化
    int[]map = new int[256];
    
    # map来group一串同类
    Map<String, List<String>> map = new HashMap<>();
    

    Map常用操作

    map.containsKey(k)
    map.put(k,v)
    map.get(k)
    map.remove(k)
    map.size()
    map.isEmpty()
    map.clear() 

    遍历map

    // map.entrySet()
    for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
         System.out.print( entry.getKey() + "," + entry.getValue());
     }
    
    // map.keySet() 
    for (String name : map.keySet()) {
    	System.out.println("key: " + name); 
    }
    
    
    // map.values() 
    for (String url : map.values()) {
    	System.out.println("value: " + url);
    }
    

      

    将给定String[]inputStrings中的每个元素加到Map<String, List<String>> 里

    简洁版

    for(String s : inputStrings) {
                List<String> list = map.getOrDefault(s, new ArrayList<>());
                list.add(s);
                map.put(s, list);
          }

    将map.values()里的每一组List<String>list 转到返回的List<List<String>>中

      List<List<String>> result = new ArrayList<>();
            for (List<String> list : map.values()) {
                result.add(list);
            }
       return result;

    简洁版

    return new ArrayList<>(map.values());
    

      

  • 相关阅读:
    Kafka 1.0.0集群安装
    java实现以docx格式导出
    基于java处理.docx格式的word合并
    基于java 合并.doc和docx格式的Word文件
    Docker版本Jenkins的使用
    Codeforces 1243 D 0-1 MST
    Public model for matrix
    Codeforces 1220 E Tourism
    Codeforces 1221 G Graph And Numbers
    Codeforces 1221 F Choose a Square
  • 原文地址:https://www.cnblogs.com/liuliu5151/p/14224717.html
Copyright © 2011-2022 走看看