基于Groovy或Java中的值将一个Map分离到两个Map中

9avjhtql  于 2022-09-21  发布在  Java
关注(0)|答案(2)|浏览(216)

我有一个Map<Object,ArrayList>Mapa类型的Map。MAP可以具有如下值

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

我想从MAPA创建一个新的MAPB,它具有所有的键值对,其中的值具有“不完整”。我可以有两个额外的Map,具有“Complete”和“Complete”KeyValue对,或者如果我可以从MAPA中删除不完整。

因此,我想根据完整或不完整的Map将马帕划分为两个独立的Map。

我需要用groovy编写它,但如果我也能在Java 8中得到一些提示,我可以尝试转换它。

代码片段我正在工作,但这将创建新的Map,但不会从MAPA删除这些值,我该怎么做呢?

MapA.entrySet().stream().filter(entry->entry.getValue().get(1).equals("InComplete")).collect(Collectors.toMap())

感谢你的帮助

更新

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()
        }
    }
rur96b6h

rur96b6h1#

有成千上万种方法可以做到这一点。

下面我给出了3个选项:1在2个循环中独立填充2个贴图,1个使用单循环(性能稍好),1个使用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]]]'
zbdgwd5y

zbdgwd5y2#

有趣的是,在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));

打印内容如下:

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

相关问题