简体   繁体   English

Hashmap - 无法推断参数

[英]Hashmap - cannot infer arguments

Hey :) This is my first post and my first approach at streams!嘿 :) 这是我的第一篇文章,也是我在流中的第一个方法! I am trying to learn it by myself, but since its a new style of programming Im having a tough time :D我正在尝试自己学习它,但由于它是一种新的编程风格,我很难过:D

So here's my problem:所以这是我的问题:

In the following Method I have a Map of <Long, Ingredient> as parameter.在下面的方法中,我有一个<Long, Ingredient>的地图作为参数。 (Ingredient has the String Attribute "name".) (成分具有字符串属性“名称”。)

I want to return an inverted Map of <String, Long> .我想返回<String, Long>的倒置 Map。 (The string being the Attribute "name" of the class Ingredient.) (字符串是类成分的属性“名称”。)

Here is my solution using a for-loop (which worked)这是我使用 for 循环的解决方案(有效)

public static Map<String, Long> focusOnNameAndInvert(Map<Long, Ingredient> articles) {
    ArrayList<String> nameList = new ArrayList<>(articles.values().stream().map(Ingredient::getName).collect(Collectors.toList()));
    ArrayList<Long> keyList = new ArrayList<>(articles.keySet());

    Map<String, Long> nameAndInvertedMap = new HashMap<>();
    for (int i = 0; i<nameList.size(); i++){
        nameAndInvertedMap.put(nameList.get(i), keyList.get(i));
    }

    return nameAndInvertedMap;
}

Here is my approach on using streams (which I need help with)这是我使用流的方法(我需要帮助)

The "<>" on the right side of initializing the HashMap is red underlined and says "Cannot infer arguments" (Using IntelliJ)初始化 HashMap 右侧的“<>”带有红色下划线,并表示“无法推断参数” (使用 IntelliJ)

public static Map<String, Long> focusOnNameAndInvert(Map<Long, Ingredient> articles) {
    Map<String, Long> nameAndInvertedMap =  new HashMap<>(
            articles.values().stream().map(Ingredient::getName),
            articles.keySet().stream().map(articles::get));


    return nameAndInvertedMap;
}

Thank you for all input, tips, critique and especially for your time!感谢您的所有输入、提示、批评,尤其是您的时间! Wishing you all a nice day :-)祝大家有个美好的一天:-)

Your method is not actually doing what you hoped.您的方法实际上并没有达到您的预期。 As you can see from the compiler error, there is no HashMap constructor which accepts a List of keys and a List of values.从编译器错误中可以看出,没有接受键List和值ListHashMap构造函数。 Besides, even if it did, you're streaming the given Map (twice), then trying to swap the keys and the values in different streams and finally not even collecting the values of your streams.此外,即使这样做了,您也在流式传输给定的Map (两次),然后尝试交换不同流中的键和值,最后甚至不收集流的值。 Streams are pipelines of operations lazily evaluated, if you do not add a terminal operation they are not even executed.流是延迟评估的操作管道,如果您不添加终端操作,它们甚至不会执行。

Here there is an official Oracle's tutorial which sums up quite good the concept of streams and how they work:这里有一个 Oracle 的官方教程,它很好地总结了流的概念及其工作原理:

https://docs.oracle.com/javase/tutorial/collections/streams/parallelism.html#laziness https://docs.oracle.com/javase/tutorial/collections/streams/parallelism.html#laziness

You can write what your method by streaming the entries of the given map and then collect them with the collect(Collectors.toMap()) terminal operation where each Ingredient 's name is mapped as the key while the long keys as their value.您可以通过流式传输给定映射的条目来编写您的方法,然后使用collect(Collectors.toMap())终端操作收集它们,其中每个Ingredient的名称被映射为键,而长键作为它们的值。

public static Map<String, Long> focusOnNameAndInvert(Map<Long, Ingredient> articles) {
    return articles.entrySet().stream()
            .collect(Collectors.toMap(entry -> entry.getValue().getName(), entry -> entry.getKey()));
}

Note that collecting the entries with the 2 parameters version of the method toMap() does not work if there are multiple keys, ie duplicate ingredient names in your case.请注意,如果有多个键,即在您的情况下重复成分名称,则使用toMap()方法的 2 个参数版本收集条目不起作用。 The 3 parameters version of the method should be preferred as it allows handling the case of colliding keys (multiple same keys mapping different values).应该首选该方法的 3 参数版本,因为它允许处理键冲突的情况(多个相同的键映射不同的值)。 Possible implementations could be: discarding a value over the other, summing the values and so on.可能的实现可能是:丢弃另一个值,对值求和等等。

Here is a possible implementation where just one of the two values is maintained.这是一个可能的实现,其中仅维护两个值之一。

public static Map<String, Long> focusOnNameAndInvert(Map<Long, Ingredient> articles) {
        return articles.entrySet().stream()
                .collect(Collectors.toMap(entry -> entry.getValue().getName(), entry -> entry.getKey(), (longVal1, longVal2) -> longVal1));
    }

Your stream is incorrect.您的直播不正确。 Should be something like this:应该是这样的:

public static Map<String, Long> focusOnNameAndInvert(Map<Long, Ingredient> articles) {
  return articles.entrySet().stream()
      .collect(Collectors.toMap(entry -> entry.getValue().getName(), Map.Entry::getKey));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM