I've just started looking at Java 8 and trying out lambdas, below is the problem i wanted to solve
Below is the code so far I have
Map<String, List<List<DataType>>> a = Arrays.stream(OperatorType.values())
.collect(
groupingBy(i -> i.getKey(),
mapping(i -> i.getSupportedtypes(),
Collectors.toList())));
The above snippet works, but thats not what i want. I want to it to be returning Map<String, List<DataType>>
I am guessing some way of flat mapping will resolve the question
OperatorType enum for reference
public enum OperatorType {
IS_ONE_OF("IS_ONE_OF", 1,
Collections.singletonList(DataType.ALL)),
IS_NOT_ONE_OF("IS_NOT_ONE_OF", 2,
Collections.singletonList(DataType.ALL)),
ENDS_WITH("ENDS_WITH", 3,
Collections.singletonList(DataType.STRING)),
DOES_NOT_ENDS_WITH("DOES_NOT_ENDS_WITH", 4,
Collections.singletonList(DataType.STRING)),
STARTS_WITH("STARTS_WITH", 5,
Collections.singletonList(DataType.STRING)),
DOES_NOT_START_WITH("DOES_NOT_START_WITH", 6,
Collections.singletonList(DataType.STRING)),
MATCHES("MATCHES", 7,
Collections.singletonList(DataType.STRING)),
DOES_NOT_MATCH("DOES_NOT_MATCH", 8,
Collections.singletonList(DataType.STRING)),
CONTAINS("CONTAINS", 9,
Collections.singletonList(DataType.STRING)),
DOES_NOT_CONTAIN("DOES_NOT_CONTAIN", 10,
Collections.singletonList(DataType.STRING)),
GREATER_THAN("GREATER_THAN", 11, Arrays.asList(DataType.INTEGER,DataType.DOUBLE)),
GREATER_THAN_OR_EQUAL_TO("GREATER_THAN_OR_EQUAL_TO", 12, Arrays.asList(DataType.INTEGER,DataType.DOUBLE)),
LESS_THAN("LESS_THAN", 13, Arrays.asList(DataType.INTEGER,DataType.DOUBLE)),
LESS_THAN_OR_EQUAL_TO("LESS_THAN_OR_EQUAL_TO", 15, Arrays.asList(DataType.INTEGER,DataType.DOUBLE)),
AFTER("AFTER", 15,
Collections.singletonList(DataType.DATE)),
BEFORE("BEFORE", 16,
Collections.singletonList(DataType.DATE));
private final int value;
private final String key;
private final List<DataType> supportedtypes;
OperatorType(String key, int value, List<DataType> supportedtypes) {
this.value = value;
this.key = key;
this.supportedtypes = supportedtypes;
}
public int getValue() {
return this.value;
}
public String getKey() {
return this.key;
}
public List<DataType> getSupportedtypes() {
return this.supportedtypes;
}
@Override
public String toString() {
return String.valueOf(this.value);
}
@JsonCreator
public static OperatorType create(String key) {
if (key == null) {
throw new IllegalArgumentException();
}
for (OperatorType v : values()) {
if (v.getKey().equalsIgnoreCase(key)) {
return v;
}
}
throw new IllegalArgumentException();
}
public static OperatorType fromValue(Integer value) {
for (OperatorType type : OperatorType.values()) {
if (value == type.getValue()) {
return type;
}
}
throw new IllegalArgumentException("Invalid enum type supplied");
}
public static OperatorType fromValue(String key) {
for (OperatorType type : OperatorType.values()) {
if (type.getKey().equalsIgnoreCase(key)) {
return type;
}
}
throw new IllegalArgumentException("Invalid enum type supplied");
}
}
Found one way to do this:
public static Map<DataType, List<OperatorType>> buildmap() {
Map<OperatorType, List<DataType>> map = new HashMap<>();
Arrays.stream(OperatorType.values()).collect(groupingBy(i -> OperatorType.fromValue(i.getKey()),
mapping(i -> i.getSupportedtypes(), Collectors.toList()))).entrySet().forEach(entry ->
{
map.put(entry.getKey(),
entry.getValue().stream().flatMap(List::stream).collect(Collectors.toList()));
});
return map.entrySet().stream().flatMap(e -> e.getValue().stream().map(v -> new SimpleEntry<>(v, e.getKey())))
.collect(
Collectors.groupingBy(Entry::getKey, Collectors.mapping(Entry::getValue, Collectors.toList())));
}
groupingBy? The "keys" in your enum are all uniquetoMapinstead ofgroupingBy