【问题标题】:How to get value from Hashtable<Integer,Hashtable<String,Integer>> h如何从 Hashtable<Integer,Hashtable<String,Integer>> h 中获取值
【发布时间】:2020-02-20 09:42:51
【问题描述】:

对于给定的外部hashtable key,如何获取内部HashTable 的整数值

HashMap<Integer, String[]> map;

Hashtable<Integer,Hashtable<String,Integer>> h = 
                                    new Hashtable<Integer,Hashtable<String,Integer>>();

for(int z = 0;z<Integer.MAX_VALUE;z++) {
    String temp4 = map.get(k)[j];
    h.put(z, new Hashtable(){{put(temp4,j);}});
}

【问题讨论】:

  • 你能写出这个答案解决了你的问题还是说出了什么问题?回复有助于详细说明答案
  • 对不起,我尝试了其他方法来解决这个问题。
  • 因此您可以将其作为另一个解决方案发布。
  • 抱歉耽搁了,我的解决方案是针对我的要求的

标签: java dictionary collections hashmap


【解决方案1】:

不是一个优雅的解决方案,只针对我的要求:

        for(Integer key : h.keySet()) {
        //System.out.println(key + " "+h.get(key));
        Hashtable temp1 = h.get(key);
        String key1 = temp1.toString();
        key1 = key1.substring(1);
        String separator ="=";
        int sepPos = key1.lastIndexOf(separator);
        key1 = key1.substring(0,sepPos);
        Enumeration enu = temp1.elements();
        int col = (int)enu.nextElement();
        for(int p=0;p<colums;p++)//j moves over columns
        {   
            if(p == col) {
                for(int r = 0;r<rows;r++) {
                    String temp = fr.get(r)[p];
                    //System.out.println(" "+temp+ " ");
                    if(temp.contentEquals(key1)) {
                        String temp2 = Integer.toString(p);
                        temp2 = temp2+ "*";
                        fr.get(r)[p] = temp2; 
                    }
                }
            } 
        }

【讨论】:

    【解决方案2】:

    第一个解决方案是最简单的,但它需要默认的中间值。

    Hashtable<Integer, Hashtable<String, Integer>> h = new Hashtable<>();
    h.put(1, new Hashtable<String, Integer>() {{put("firstKey", 3);}});
    
    Integer intValue = h.getOrDefault(1, new Hashtable<>()).getOrDefault("firstKey", null);
    System.out.println(intValue);
    

    基于流的解决方案:

    Optional<Integer> firstValue = h.entrySet().stream()
          .filter(e -> e.getKey().equals(1))
          .flatMap(e -> e.getValue().entrySet().stream())
          .filter(e -> e.getKey().equals("firstKey"))
          .map(e -> e.getValue())
          .findFirst();
    
    firstValue.ifPresent(System.out::println);
    

    可以用Optional引入辅助方法。

    private static <K,T> Optional<T> from(Map<K, T>  from, K key) {
      return (from.containsKey(key)) ? Optional.of(from.get(key)) : Optional.empty();
    }
    

    然后它看起来更具可读性:

    from(h, 1)
          .flatMap(x -> from(x, "firstKey"))
          .ifPresent(System.out::println);
    

    或者在之前的基础上再添加一个方法:

    private static <K, T> Function<Map<K, T>, Optional<T>> forKey(K key) {
      return (Map<K, T>  from) -> from(from, key);
    }
    

    然后可以写成:

    Optional.of(h)
          .flatMap(forKey(1))
          .flatMap(forKey("firstKey"))
          .ifPresent(System.out::println);
    

    【讨论】:

    • 嘿,如果你投了反对票,你能删除吗,我不能就堆栈溢出提出问题。这是我唯一的知识来源和好的建议,我不能失去它。我真的很忙,所以无法回复您的评论。谢谢
    • 我没有这样做
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    • 2021-07-12
    • 1970-01-01
    • 2019-05-05
    • 2016-09-26
    • 2013-12-01
    相关资源
    最近更新 更多