Reputation: 133
I have a map of type Map<Object,ArrayList> MapA. map can have values like
MapA
{key=0003bb2d-f3cf-4b5c-9f34 , value = ["12345","Complete","5431"],
key=0003bb2d-f3cf-4b5c-9f71 , value = ["2311","InComplete","5321"],
key=0003bb2d-f3cf-4b5c-1d71 , value = ["4567","InComplete","4321"],
key=0003bb2d-f3cf-4b5c-1r51 , value = ["2345","Complete","5432"]}
output should be
MapA
{key=0003bb2d-f3cf-4b5c-9f34 , value = ["12345","Complete","5431"],
key=0003bb2d-f3cf-4b5c-1r51 , value = ["2345","Complete","5432"]}
MapB
{key=0003bb2d-f3cf-4b5c-9f71 , value = ["2311","InComplete","5321"],
key=0003bb2d-f3cf-4b5c-1d71 , value = ["4567","InComplete","4321"]}
I want to create a new MapB from MapA having all key value pairs, where value has "InComplete" in it. Either i can have two extra map having "Complete" and "InComplete" KeyValue pairs or if i can remove Incomplete from MapA.
So i want to Segregate MapA on the basis of Complete or InComplete into two seperate maps.
I need to write it in groovy but if i can get some hint in java 8 as well, i can try to convert it.
Code snippet i am working on but this will create new Map but won't remove these values from MapA, how can i do that?
MapA.entrySet().stream().filter(entry->entry.getValue().get(1).equals("InComplete")).collect(Collectors.toMap())
Appreciate your help
UPDATE
def mapB = [:].withDefault {key -> return []}
Iterator<Map.Entry<Object, ArrayList>> iterator = mapA.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Object, ArrayList> entry = iterator.next();
if(entry.getValue().get(1).equals("InComplete")) {
mapB.put(entry.getKey(), entry.getValue())
iterator.remove()
}
}
Upvotes: 0
Views: 61
Reputation: 171104
And for fun, this is the same but in Java 17
var stringListMap = Map.of(
"0003bb2d-f3cf-4b5c-9f34", List.of("12345", "Complete", "5431"),
"0003bb2d-f3cf-4b5c-9f71", List.of("2311", "InComplete", "5321"),
"0003bb2d-f3cf-4b5c-1d71", List.of("4567", "InComplete", "4321"),
"0003bb2d-f3cf-4b5c-1r51", List.of("2345", "Complete", "5432")
);
var result = stringListMap.entrySet().stream().collect(
Collectors.groupingBy(
e -> e.getValue().get(1),
Collectors.mapping(e -> e, Collectors.toList())
)
);
result.forEach((k, v) -> System.out.println(k + " => " + v));
Which prints:
Complete => [0003bb2d-f3cf-4b5c-9f34=[12345, Complete, 5431], 0003bb2d-f3cf-4b5c-1r51=[2345, Complete, 5432]]
InComplete => [0003bb2d-f3cf-4b5c-9f71=[2311, InComplete, 5321], 0003bb2d-f3cf-4b5c-1d71=[4567, InComplete, 4321]]
Upvotes: 2
Reputation: 20699
There are thousands way of doing this.
Down below I put 3 options: 1 with filling 2 maps independently in 2 loops, 1 (slightly better performing) using the single loop, and 1 using groupBy
:
def map = [ '0003bb2d-f3cf-4b5c-9f34':["12345","Complete","5431"],
'0003bb2d-f3cf-4b5c-9f71':["2311","InComplete","5321"],
'0003bb2d-f3cf-4b5c-1d71':["4567","InComplete","4321"],
'0003bb2d-f3cf-4b5c-1r51':["2345","Complete","5432"]]
// 1. option
Map complete = map.findAll{ k, v -> 'Complete' == v[ 1 ] }
Map inComplete = map.findAll{ k, v -> 'InComplete' in v }
assert complete.toString() == '[0003bb2d-f3cf-4b5c-9f34:[12345, Complete, 5431], 0003bb2d-f3cf-4b5c-1r51:[2345, Complete, 5432]]'
assert inComplete.toString() == '[0003bb2d-f3cf-4b5c-9f71:[2311, InComplete, 5321], 0003bb2d-f3cf-4b5c-1d71:[4567, InComplete, 4321]]'
// 2. option
def twoMaps = map.inject( [:].withDefault{ [:] } ){ res, curr ->
res[ curr.value[ 1 ] ] << curr
res
}
assert twoMaps.toString() == '[Complete:[0003bb2d-f3cf-4b5c-9f34:[12345, Complete, 5431], 0003bb2d-f3cf-4b5c-1r51:[2345, Complete, 5432]], InComplete:[0003bb2d-f3cf-4b5c-9f71:[2311, InComplete, 5321], 0003bb2d-f3cf-4b5c-1d71:[4567, InComplete, 4321]]]'
// 3. option -> credits to @tim_yates!
def groupMaps = map.groupBy{ it.value[ 1 ] }
assert groupMaps.toString() == '[Complete:[0003bb2d-f3cf-4b5c-9f34:[12345, Complete, 5431], 0003bb2d-f3cf-4b5c-1r51:[2345, Complete, 5432]], InComplete:[0003bb2d-f3cf-4b5c-9f71:[2311, InComplete, 5321], 0003bb2d-f3cf-4b5c-1d71:[4567, InComplete, 4321]]]'
Upvotes: 2