使用另一个哈希映射 Java 中给出的过滤条件过滤哈希映射列表



让我们假设我有一个对象P的列表。

Class P {
int id;
Map<String,String> value;
}

值看起来像

value = {
"Category" : "Category 1",
"family" : "Family 1",
"color" : "Color 1"
}

如上所述,我有P列表。假设列表是列表P。

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

我想过滤各种条件。条件以地图的形式出现。

filter = {
"color" : "Color 1",
"family" : "Family 1"
}

过滤后,很明显,输出将是

List<P> listP = [
P(1, map1),
P(4, map4),
]

map1 和map4是指上面代码片段中定义的映射。长话短说,我想要下面提到的 Python 代码的替代品(它不适用于这些类,但想法是相同的。

filterC = {
"color" : "Color 1",
"family" : "Family 1"
}
inputL = [
{
"Category" : "Category 1",
"family" : "Family 1",
"color" : "Color 1"
},
{
"Category" : "Category 1",
"family" : "Family 1",
"color" : "Color 2"
},
{
"Category" : "Category 1",
"family" : "Family 2",
"color" : "Color 1"
},
{
"Category" : "Category 2",
"family" : "Family 1",
"color" : "Color 1"
},
]
outputL = [inp for inp in inputL if all(inp[k] == v for k,v in filterC.items())]

如评论中所建议的,我已经为类产品的值属性添加了我的代码和示例值(上面称为 P(

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());

样本

{Category=7018, Hydrogen=5, Moisture=0.3, Product Name=New Product, Ferrite (Fn)=, Vanadium=0.025, Phosphorus=0.0175, Hardness=, Carbon=0.075, Molybdenum=0.1, UTS=550, Columbium=, Chromium=0.1, YS=200, Nickel=0.15, Silicon=0.375, Hardness_Scale=HRC, Sulfur=0.0175, Copper=, Elongation=11, Manganese=0.8}

我能够使用正常循环和基本 lambda 来完成任务,但我无法使用过滤器和收集。任何方向上的帮助将不胜感激。我学习了几个教程,但在函数式编程方面,我是Java的新手。谢谢,提前。

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

假设您有如下输入数据

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> 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");

最新更新