Home > Back-end >  How can I use an If-statement insede the Collector?
How can I use an If-statement insede the Collector?

Time:04-14

The output I require is like this :

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

The structure of this output is: 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.

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")]

Where "0.1","0.2","0.3" is the key of the Pair and "true","false","true" is the value of the Pair.

How do I write an if-else statement for this Pair? Where for a particular condition I want the value of the Pair to be returned as true or false?

This is the code I have written for **Map<String, List<String>>** but now my requirement is this Map<String, List<Pair<String, String>>> so how do I write a code for this?

  public Map<String, List<String>> getCAVersions(Set<String> corporateActionIds) {
        List<CorporateActionEvent> corporateActionEvents = corporateActionEventRepository.findAllByCorporateActionIdIn(corporateActionIds);
        return corporateActionEvents.stream()
                .sorted(Comparator.comparing(CorporateActionEvent::getMajorVersion).thenComparing(CorporateActionEvent::getMinorVersion))
                .collect(Collectors.groupingBy(CorporateActionEvent::getCorporateActionId, Collectors.mapping(cae ->
                        cae.getMajorVersion()   "."   cae.getMinorVersion(), Collectors.toList())));
    }

CodePudding user response:

Create a method/Function which accepts a CorporateActionEvent and returns a 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, 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:

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

CodePudding user response:

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:

new AbstractMap.SimpleEntry<>(key, value)

The attribute key is defined as final and value is mutable. If you are not satisfied with that (for instance, if you need an immutable object), you can define your own 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? Where for a particular condition I want the value of the Pair to be returned as true or false?

For your conditional logic (if statements that you've mentioned) you can either use a ternary operator condition ? true-case : false-case in order to place it inside the collector or extract it into a separate method, which will be a better option if there are several conditions.

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(cae -> Map.entry(cae -> Map.entry(cae.getMajorVersion()   "."   cae.getMinorVersion(),
                                    your_condition ? "true" : "false"), 
            Collectors.toList())
  • Related