使用另一个哈希映射Java中给定的筛选条件筛选哈希映射列表

使用另一个哈希映射Java中给定的筛选条件筛选哈希映射列表,java,collections,functional-programming,hashmap,java-stream,Java,Collections,Functional Programming,Hashmap,Java Stream,假设我有一个对象p的列表 Class P { int id; Map<String,String> value; } 如上所述,我有一个p的列表。假设这个列表是listP map1 = { "Category" : "Category 1", "family" : "Family 1", "color" : "Color 1" } map2 = { "Category" : "Category 2", "family" : "

假设我有一个对象p的列表

Class P {
    int id;
    Map<String,String> value;
}
如上所述,我有一个p的列表。假设这个列表是listP

map1 = {
    "Category" : "Category 1",
    "family" : "Family 1",
    "color" : "Color 1"
}
map2 = {
    "Category" : "Category 2",
    "family" : "Family 2",
    "color" : "Color 1"
}
map3 = {
    "Category" : "Category 1",
    "family" : "Family 1",
    "color" : "Color 2"
}
map4 = {
    "Category" : "Category 2",
    "family" : "Family 1",
    "color" : "Color 1"
}
List<P> listP = [
    P(1, map1),
    P(2, map2),
    P(3, map3),
    P(4, map4),
]
过滤后,很明显,输出将

List<P> listP = [
    P(1, map1),
    P(4, map4),
]
正如评论中所建议的,我已经为上面提到的p类产品的value属性添加了代码和示例值

Map<String, String> filter = new HashMap<String, String>();
        filter.put("Category","7018");
        filter.put("Carbon","0.075");

        List<Product> productList= productRepository.findAll();


        List<Product> prodList = productList.stream()
                .filter(prod -> filter.entrySet().stream().allMatch(e -> prod.getValue().get(e.getKey()) == e.getValue()))
                .collect(Collectors.toList());
我能够使用普通循环和基本lambda完成任务,但我无法使用过滤器和收集。如有任何帮助,我们将不胜感激。我已经学习了一些教程,但对于函数式编程,我对Java还是新手。提前谢谢


编辑-将p中的id从字符串转换为int,以避免混淆。与怀疑无关,只是为了清楚起见

以下内容将实现您想要实现的目标

List<Product> filteredProducts = products.stream()
            .filter(product -> filter.entrySet()
                    .stream()
                    .allMatch(e -> product.getValue().get(e.getKey()) != null && product.getValue().get(e.getKey()).equals(e.getValue())))
            .collect(Collectors.toList());
基本上迭代列表中的所有元素,并过滤过滤器映射中的所有键/值。或者更好的方法是

List<Product> filteredProducts = products.stream()
            .filter(product ->  product.getValue().entrySet().containsAll(filter.entrySet()))
            .collect(Collectors.toList());
过滤器是什么样子的

Map<String, String> filter = new HashMap<>();
    filter.put("color", "Color 1");
    filter.put("family", "Family 1");

以下内容将实现您想要实现的目标

List<Product> filteredProducts = products.stream()
            .filter(product -> filter.entrySet()
                    .stream()
                    .allMatch(e -> product.getValue().get(e.getKey()) != null && product.getValue().get(e.getKey()).equals(e.getValue())))
            .collect(Collectors.toList());
基本上迭代列表中的所有元素,并过滤过滤器映射中的所有键/值。或者更好的方法是

List<Product> filteredProducts = products.stream()
            .filter(product ->  product.getValue().entrySet().containsAll(filter.entrySet()))
            .collect(Collectors.toList());
过滤器是什么样子的

Map<String, String> filter = new HashMap<>();
    filter.put("color", "Color 1");
    filter.put("family", "Family 1");

假设您有如下输入数据

List<Product> products = Arrays.asList(first, second, ..);
您的过滤器映射是

Map<String, String> filters = new HashMap<>();

filters.put("color", "xyz");
然后你可以用这个过滤

List<Product> data = products
  .stream()
  .filter(element -> filter.entrySet()
            .stream()
            .allMatch(criteria -> Objects.equals(criteria.getValue(), 
                            element.getValue().get(criteria.getKey()))))
  .collect(Collectors.toList());

假设您有如下输入数据

List<Product> products = Arrays.asList(first, second, ..);
您的过滤器映射是

Map<String, String> filters = new HashMap<>();

filters.put("color", "xyz");
然后你可以用这个过滤

List<Product> data = products
  .stream()
  .filter(element -> filter.entrySet()
            .stream()
            .allMatch(criteria -> Objects.equals(criteria.getValue(), 
                            element.getValue().get(criteria.getKey()))))
  .collect(Collectors.toList());

如果您输入的是实际的映射,而不是Json,这将对所有人都有帮助。感谢WJS的输入。我已经添加了代码。我的想法是,这可能会让任何人感到困惑,所以我避免了它。如果你把实际的地图,而不是Json放进去,这将对所有人都有帮助。谢谢,WJS的输入。我已经添加了代码。我的想法是它可能会迷惑任何人,所以我避免了它。嘿,@lucid,我已经附加了我正在尝试的代码。你能指出那里的错误吗?我的代码没有抛出任何错误。它只是过滤空数据。但是,正如样本本身所述,至少有一种产品满足过滤标准。还有很多。@Plasmatiger的问题是allMatche->prod.getValue.gete.getKey==e.getValue部分,它将匹配引用。您可以用这个Objects.equalprod.getValue.gete.getKey、e.getvalues替换。非常感谢。我觉得很傻。Java在字符串匹配时的基本错误。谢谢你指出了正确的方向。拯救了这一天。很高兴能帮上忙:嘿,@lucid,我已经附加了我正在尝试的代码。你能指出那里的错误吗?我的代码没有抛出任何错误。它只是过滤空数据。但是,正如样本本身所述,至少有一种产品满足过滤标准。还有很多。@Plasmatiger的问题是allMatche->prod.getValue.gete.getKey==e.getValue部分,它将匹配引用。您可以用这个Objects.equalprod.getValue.gete.getKey、e.getvalues替换。非常感谢。我觉得很傻。Java在字符串匹配时的基本错误。谢谢你指出了正确的方向。拯救了这一天。很高兴能帮上忙:嘿,@harsh,我已经附加了我正在尝试的代码。你能指出那里的错误吗?我的代码没有抛出任何错误。它只是过滤空数据。但是,正如样本本身所述,至少有一种产品满足过滤标准。还有many@Plasmatiger它是==检查引用等价性,而不是在字符串的情况下检查内容等价性,但是最好使用filterproduct->product.value.entrySet.containsAllfilter.entrySet,这是一个更干净的解决方案是的,@lucid指出。感觉很傻。我看到的是谓词中的错误,而它是对象相等。谢谢你的评论。拯救了我的一天。嘿,@harsh,我已经附加了我正在尝试的代码。你能指出那里的错误吗?我的代码没有抛出任何错误。它只是过滤空数据。但是,正如样本本身所述,至少有一种产品满足过滤标准。还有many@Plasmatiger它是==检查引用等价性,而不是在字符串的情况下检查内容等价性,但是最好使用filterproduct->product.value.entrySet.containsAllfilter.entrySet,这是一个更干净的解决方案是的,@lucid指出。感觉很傻。我看到的是谓词中的错误,而它是对象相等。谢谢你的评论。救了我一天。