Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从地图交换密钥<;键,列表<;价值观>&燃气轮机;_Java_Dictionary_Key - Fatal编程技术网

Java 从地图交换密钥<;键,列表<;价值观>&燃气轮机;

Java 从地图交换密钥<;键,列表<;价值观>&燃气轮机;,java,dictionary,key,Java,Dictionary,Key,我正在为这个问题寻找解决方案(这是为了考试): 我有一个Map{ if(结果集装箱(城市)) 结果。获取(城市)。添加(键); 否则{ SortedSet集合=新树集合(); set.add(键); 结果。放置(城市、集合); }); }); 要获得示例输出,请使用带有流的简单单行程序: List<String> result = operators.entrySet().stream().filter(entry -> entry.getValue().contains(de

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

我有一个Map操作符,由一个函数填充

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);


    }
public void addOperator(String operatorName,String…destinationNames)抛出ProposalException{
if(operators.containsKey((operatorName))){
抛出新的ProposalException(“运算符”+operatorName+“已进入系统!”);
}
否则{
SortedSet destinationstemp=新树集();
for(字符串s:destinationNames){
如果(s!=null){
目的临时添加;
}
}
operators.put(operatorName,destinationstemp);
}
现在,我想创建一个新的Mapdestinations,它以destinationName作为键,以与运算符名称相关的值作为值

我怎么才能明白呢

注:上面这一部分是方法的用法,不在代码中的部分是需要的输出。很抱歉代码的格式不好。ph是façade pattern类的实例

    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"));
public SortedSet getDestOperators(String destinationName){…}//方法,该方法返回与destinationName有关的**目的地**值}
运营商博士(“op3”、“伦敦”、“罗马”);
运营商博士(“op2”、“伦敦”、“柏林”);
博士(op5、柏林、罗马、马德里);
运营商博士(“op1”、“伦敦”、“马德里”、“柏林”);
运营商博士(“凤凰社第10章”,“罗马”);
运营商博士(“op4”、“马德里”、“柏林”);
系统输出打印LN(ph.getDestOperators(“马德里”));

输出:[op1、op4、op5]

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

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;
}
public SortedSet getDestOperators(字符串destinationName){
Set result=new HashSet();
for(Map.Entry:operators.getValues()){
if(entry.getValue().contains(destinationName)){
results.add(entry.getKey());
}
}
返回结果;
}

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

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);
                });
        });
origin.forEach((键,列表)->{list.forEach(城市->{
if(结果集装箱(城市))
结果。获取(城市)。添加(键);
否则{
SortedSet集合=新树集合();
set.add(键);
结果。放置(城市、集合);
});
});

要获得示例输出,请使用带有流的简单单行程序:

List<String> result = operators.entrySet().stream().filter(entry -> entry.getValue().contains(destinationName)).map(Entry::getKey).sorted().collect(Collectors.toList());
List result=operators.entrySet().stream().filter(entry->entry.getValue().contains(destinationName)).map(entry::getKey).sorted().collect(collector.toList());
或者,为了更好地在多行中扩展可读性:

List<String> result = operators
      .entrySet()
      .stream()
      .filter(entry -> entry.getValue().contains(destinationName))
      .map(Entry::getKey)
      .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())));
列表结果=运算符
.entrySet()
.stream()
.filter(entry->entry.getValue().contains(destinationName))
.map(条目::getKey)
.已排序()
.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 result=operators.entrySet().stream().flatMap(entry->entry.getValue().stream().collect(Collectors.toMap(Function.identity(),o->Arrays.asList(entry.getKey()).entrySet().stream()).collector(Collectors.toMap(entry::getKey,entry::getValue,(a,b)->stream.of(a,b).flatMap(List::stream).sorted().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>> 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())));
Map result2=运算符
.entrySet()
.stream()
.flatMap(条目->条目
.getValue()
.stream()
.collect(Collectors.toMap(Function.identity()),
o->Arrays.asList(entry.getKey()))
.entrySet()
.stream())
.collect(Collectors.toMap)(条目::getKey,
条目::getValue,
(a,b)->(a,b)流
.flatMap(列表::流)
.已排序()
.collect(Collectors.toList());

如果我想用destinationName作为键,相关操作符列表作为值,构建地图目的地,并返回与参数相关的SortedSet作为getDestOperators的结果,该怎么办?很遗憾,我不知道该怎么做;(您需要考虑每种方法的优缺点,您描述的方法在创建过程中速度较慢,在检索过程中速度较快,但需要更多ram,我的方法在检索过程中速度较慢,每次检索过程中速度较慢,并且使用我的cpu功率。