【问题标题】:Swapping key from a Map<Key, List<Values>>从 Map<Key, List<Values>> 交换键
【发布时间】:2017-06-30 12:41:13
【问题描述】:

我正在寻找这个问题的解决方案(这是为了考试):

我有一个由函数填充的 Map > operators

public void addOperator(String operatorName, String... destinationNames) throws ProposalException {
    if(operators.containsKey((operatorName))){
        throw new ProposalException("Operator " + operatorName + "already into system!");
    }
    else{
        SortedSet<String> destinationstemp=new TreeSet<>();
        for(String s: destinationNames){
            if(s!=null){
                destinationstemp.add(s);
            }
        }
        operators.put(operatorName, destinationstemp);


    }

现在,我想创建一个新的 Map > destinations,它的键是 destinationName,值是与 operatorNames 相关的。

我怎样才能做到这一点?

P.S:上面是方法的使用,非代码部分是想要的输出。抱歉,代码格式错误。 ph 是外观模式类的实例

    public SortedSet<String> getDestOperators(String destinationName) {...}//method that returns the **destinations** values related to destinationName}
ph.addOperator("op3","london","rome");
 ph.addOperator("op2","london","berlin");
 ph.addOperator("op5","berlin","rome","madrid");
ph.addOperator("op1","london","madrid","berlin");
 ph.addOperator("op10","rome");
 ph.addOperator("op4","madrid","berlin"); 
 System.out.println(ph.getDestOperators("madrid"));

输出:[op1, op4, op5]

【问题讨论】:

    标签: java dictionary key


    【解决方案1】:

    您需要检查地图中的每个条目并检查内部集合是否包含您要检查的值,

    public SortedSet<String> getDestOperators(String destinationName) {
       Set<String> result = new HashSet<String>();
       for(Map.Entry<String,Set<String>> entry : operators.getValues()){
    
          if(entry.getValue().contains(destinationName)){
              results.add(entry.getKey());
          }
       }
    
      return result;
    }
    

    【讨论】:

    • 如果我想构建 Map > destinations 以destinationName为键,相关运算符列表为值并返回SortedSet相关到参数作为 getDestOperators 的结果?
    • 不幸的是我不知道该怎么做;(
    • 您需要考虑每种方法的优缺点,您所描述的在创建过程中很慢,在检索过程中速度更快,但需要更多的内存,我的方法在检索 val 和每次检索和使用我的 cpu 电源。
    【解决方案2】:

    要让您的示例输出带有流的简单单行:

    List<String> result = operators.entrySet().stream().filter(entry -> entry.getValue().contains(destinationName)).map(Entry::getKey).sorted().collect(Collectors.toList());
    

    或在这里为了更好的可读性分布在多行:

    List<String> result = operators
          .entrySet()
          .stream()
          .filter(entry -> entry.getValue().contains(destinationName))
          .map(Entry::getKey)
          .sorted()
          .collect(Collectors.toList());
    

    如果您想“反转”文本中描述的映射,则可以使用更复杂的单线:

    Map<String, List<String>> result = operators.entrySet().stream().flatMap(entry -> entry.getValue().stream().collect(Collectors.toMap(Function.identity(), o -> Arrays.asList(entry.getKey()))).entrySet().stream()).collect(Collectors.toMap(Entry::getKey, Entry::getValue, (a, b) -> Stream.of(a, b).flatMap(List::stream).sorted().collect(Collectors.toList())));
    

    或在这里为了更好的可读性分布在多行:

    Map<String, List<String>> result2 = operators
          .entrySet()
          .stream()
          .flatMap(entry -> entry
                .getValue()
                .stream()
                .collect(Collectors.toMap(Function.identity(),
                                          o -> Arrays.asList(entry.getKey())))
                .entrySet()
                .stream())
          .collect(Collectors.toMap(Entry::getKey,
                                    Entry::getValue,
                                    (a, b) -> Stream.of(a, b)
                                          .flatMap(List::stream)
                                          .sorted()
                                          .collect(Collectors.toList())));
    

    【讨论】:

      【解决方案3】:

      您需要做的是遍历每个运算符,然后遍历列表中的所有条目,如果列表中的值尚未出现在输出映射中,则添加它,否则修改其运算符集合. 下面是一些代码:

      origin.forEach((key, list) -> {list.forEach(city -> {
                  if(result.containsKey(city))
                      result.get(city).add(key);
                  else{
                      SortedSet<String> set = new TreeSet<>();
                      set.add(key);
                      result.put(city, set);
                      });
              });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-08
        • 2020-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-22
        • 2010-10-30
        • 1970-01-01
        相关资源
        最近更新 更多