HashMultimap的数据处理

Data handling of HashMultimap

我有一个 HashMultimap

Multimap<String, String> map = HashMultimap.create();

我放入地图的数据是

map.put("cpu", "i9");
map.put("hang", "MSI");
map.put("hang", "DELL");
map.put("hang", "DELL");
map.put("cpu", "i5");
map.put("hang", "HP");
map.put("cpu", "i7");

我有一个流

String joinString = map.entries().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining(" OR "));

我需要输出为

(hang=HP OR hang=MSI OR hang=DELL) AND (cpu=i9 OR cpu=i5 OR cpu=i7)

我需要在按键之间插入一个 AND。我该怎么做?

使用地图视图:

String joined = map.asMap()
        .entrySet()
        .stream()
        .map(e -> e.getValue()
                .stream()
                .map(v -> e.getKey() + "=" + v)
                .collect(Collectors.joining(" OR ", "(", ")")))
        .collect(Collectors.joining(" AND "));

当然schmosel打败了我,但这里略有不同api/usage:

String joined = map.keySet() // keySet() instead of asMap()
  .stream().map(k
    -> String.format( // string.format instead of concatenation ;)
        "(%s)",
        map.get(k).stream() // map.get(k) instead of e.getValue()
            .map(v
                -> String.format("%s=%s", k, v))
            .collect(Collectors.joining(" OR "))
       )
  ).collect(Collectors.joining(" AND "));