简体   繁体   English

如何在 Collector 中使用 If 语句?

[英]How can I use an If-statement inside the Collector?

The output I require is like this:我要求的output是这样的:

"Versions" : [("0.1","true"),("0.2","false"),("0.3","true")]

The structure of this output is: Map<String, List<Pair<String, String>>>这个output的结构是: Map<String, List<Pair<String, String>>>

Where "Versions" is the key of the Map and [("0.1","true"),("0.2","false"),("0.3","true")] this is the value of the Map .其中"Versions"是 Map 的键, [("0.1","true"),("0.2","false"),("0.3","true")]这是Map的值。

Now, inside the Map we have a List like: List<Pair<String, String>> eg is this: [("0.1","true"),("0.2","false"),("0.3","true")]现在,在Map中,我们有一个List ,如: List<Pair<String, String>>例如: [("0.1","true"),("0.2","false"),("0.3","true")]

Where "0.1","0.2","0.3" is the key of the Pair and "true","false","true" is the value of the Pair .其中"0.1","0.2","0.3"Pair的键, "true","false","true"Pair的值。

How do I write an if-else statement for this Pair ?如何为这个Pair写一个if-else语句? Where for a particular condition I want the value of the Pair to be returned as true or false ?对于特定条件,我希望Pair的值返回为truefalse的位置在哪里?

You can utilize the existing implementation of map entry by invoking the static method Map.entry() that expects a key and a value ( only for Java 9 and above ) or by invoking the constructor of map entry directly:您可以通过调用 static 方法Map.entry()来利用 map 条目的现有实现,该方法需要一个键和一个值(仅适用于 Java 9 及以上)或直接调用 map 条目的构造函数:

new AbstractMap.SimpleEntry<>(key, value)

The attribute key is defined as final and value is mutable.属性被定义为final的,是可变的。 If you are not satisfied with that ( for instance, if you need an immutable object ), you can define your own class Pair .如果您对此不满意(例如,如果您需要一个不可变的 object ),您可以定义自己的 class Pair

public class Pair<K, V> {
    private final K key;
    private final V value;
    
    public Pair(K key, V value) {
        this.key = key;
        this.value = value;
    }
    
    // getters
}

How do I write an if-else statement for this Pair ?如何为这个Pair写一个if-else语句? Where for a particular condition I want the value of the Pair to be returned as true or false ?对于特定条件,我希望Pair的值返回为truefalse的位置在哪里?

For your conditional logic ( if statements that you've mentioned ) you can either it in-line inside the collector ( as it shown below ) or extract it into a separate method, which will be a better option if there are several conditions.对于您的条件逻辑(您提到if语句),您可以将其内嵌在收集器中(如下所示)或将其提取到单独的方法中,如果有多个条件,这将是更好的选择。

The only change needs to be done to your code is to replace the string concatenation inside the Collectors.mapping() with the expression that creates a map entry ( or an instance of your custom class ) which take the concatenated string representing a version as a key and the result of the condition as a value.需要对您的代码进行的唯一更改是将Collectors.mapping()中的字符串连接替换为创建 map 条目(或自定义 class 的实例)的表达式,该表达式将表示版本的连接字符串作为键和条件的结果作为值。

Collectors.mapping(cae -> Map.entry(cae.getMajorVersion() + "." + cae.getMinorVersion(),
                                    String.valueOf(your_condition)), 
            Collectors.toList())

Create a method/Function which accepts a CorporateActionEvent and returns a Pair<String, String> :创建一个接受CorporateActionEvent并返回Pair<String, String>的方法/函数:

public static Pair<String, String> getPair(CorporateActionEvent cae){
   boolean myCondition; //= check whatever
   String key = cae.getMajorVersion() + "." + cae.getMinorVersion();
   return new Pair(key, String.valueOf(myCondition));
}

and use it by adjusting the corresponding mapping并通过调整对应的映射来使用

....Collectors.mapping(cae -> getPair(cae), Collectors.toList())));

or using method reference, assuming you placed the getPair method in your CorporateActionEvent class:或使用方法参考,假设您将getPair方法放在CorporateActionEvent class 中:

....Collectors.mapping(CorporateActionEvent::getPair, Collectors.toList())));

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

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