gpt4 book ai didi

java - 在java中的 map 数据列表上应用过滤器

转载 作者:行者123 更新时间:2023-11-30 06:46:08 25 4
gpt4 key购买 nike

我有一个List<Map<String, Object>>数据,我想应用一些按括号优先顺序的逻辑条件列表。示例条件类似于 ((firstname = john AND Lastname = Eleven) OR (salary = 15000 AND location = Mexico OR (firstname = mathew AND lastname = Thirteen)))

我想在 List 上运行这些条件并只返回匹配的数据

我写了下面的代码,如果有人可以修改以基于过滤器工作,那就太好了

package test;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class TestFilter {

public static void main(String argv[]) {
String[] firstnames = {"john", "david", "mathew", "john", "jerry", "Uffe", "Sekar", "Suresh", "Ramesh", "Raja"};
String[] secondnames = {"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty"};
String[] salary = {"10000", "20000", "15000", "5323", "2000", "5346", "1000", "4889", "7854", "2438"};
String[] location = {"India", "Iceland", "Mexico", "Slovenia", "Poland", "Australia", "1000", "USA", "England", "Canada"};

List<Map<String, Object>> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("firstname", firstnames[i]);
dataMap.put("secondname", secondnames[i]);
dataMap.put("salary", salary[i]);
dataMap.put("location", location[i]);
list.add(dataMap);
}
String filterRule = "((firstname = john AND Lastname = Eleven) OR (salary = 15000 AND location = Mexico OR (firstname = mathew AND lastname = Thirteen)))";
System.out.println(filter(list, filterRule));

}

public static List<Map<String, Object>> filter(List<Map<String, Object>> list, String filterRules) {
List<Map<String, Object>> filtered = list.stream()
.filter(p -> checkFilter(p, filterRules)).collect(Collectors.toList());
return filtered;

}

public static Boolean checkFilter(Map<String, Object> mapData, String filterRules) {
// Apply condition here and return true or false
// return (mapData.get("firstname") + "").equalsIgnoreCase("john");
//return true;
}

}

最佳答案

我相信我的答案并不完全是您正在寻找的内容,但它可能对您有所帮助或对如何过滤有新的想法。我的想法是通过创建 HashMap 来模拟数据库,然后您可以根据某些条件进行过滤搜索,以及向该数据库(HashMap)添加和删除..像这样:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class TestFilter {
public Map<Integer, ArrayList<String>> dataMap;
List<String> firstnames, lastnames, salarys, locations; // List is dynamic, you can add to it and delete at run time

public TestFilter(){
firstnames = new ArrayList<String>(Arrays.asList( new String[]{"john", "david", "mathew", "john", "jerry", "Uffe", "Sekar", "Suresh", "Ramesh", "Raja"}));
lastnames = new ArrayList<String>(Arrays.asList( new String[]{"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty"}));
salarys = new ArrayList<String>(Arrays.asList( new String[]{"10000", "20000", "15000", "5323", "2000", "5346", "1000", "4889", "7854", "2438"}));
locations = new ArrayList<String>(Arrays.asList( new String[]{"India", "Iceland", "Mexico", "Slovenia", "Poland", "Australia", "1000", "USA", "England", "Canada"}));
populateDatabase();


}

public static void main(String argv[]) {
TestFilter tf = new TestFilter(); // just example to test
Scanner in = new Scanner(System.in);
System.out.println("Insert First Name");
String firstName = in.nextLine();
System.out.println("Insert Last Name");
String lastName = in.nextLine();
System.out.println("Insert Salary");
String salary = in.nextLine();
System.out.println("Insert Location");
String location = in.nextLine();
System.out.println("Filter Result: " + tf.filter(firstName, lastName, salary, location));

tf.addRecord("Yahya", "Almardeny", "3000", "Ireland"); //add new record
System.out.println("After Adding: " + tf.dataMap); // test it

tf.deleteRecord(new String[]{"Yahya", "Almardeny"}); // delete old record
System.out.println("After Deleting: " + tf.dataMap); // test it

}


//this method will return the record (as ArrayList) if there is a match or null if there is not.
public ArrayList<String> filter(String firstName, String lastName, String salary,String location) {
//attempt to filter
for(Integer id : dataMap.keySet()){ //cycle through the database to find a match according to the conditions
if (dataMap.get(id).contains(firstName) && dataMap.get(id).contains(lastName) ||
dataMap.get(id).contains(salary) && dataMap.get(id).contains(location)){
return new ArrayList<String>(Arrays.asList(new String[]{id.toString(), dataMap.get(id).get(0),
dataMap.get(id).get(1), dataMap.get(id).get(2), dataMap.get(id).get(3)}));
}
}
return null;
}

public void populateDatabase(){
dataMap = new HashMap<Integer, ArrayList<String>>(); // create HashMap as a database give every new record auto increment integer as an Id
for(int i=0; i<firstnames.size(); i++){
dataMap.put(i, new ArrayList<String>(Arrays.asList(new String[]
{firstnames.get(i), lastnames.get(i), salarys.get(i), locations.get(i)})));
}
}

public void addRecord(String firstName, String lastName, String salary,String location){
firstnames.add(firstName);
lastnames.add(lastName);
salarys.add(salary);
locations.add(location);
populateDatabase();
}

public void deleteRecord(Object obj){
int position = -1;
// delete by a combination of first and last names or salary and location
if(obj instanceof String[]){ // first index is first name, second is last name OR first index is the salary and the second is the location
for(Integer id : dataMap.keySet()){ //cycle through the database to find a match according to the conditions
if (dataMap.get(id).contains(((String[]) obj)[0]) && dataMap.get(id).contains(((String[]) obj)[1]) ||
dataMap.get(id).contains(((String[]) obj)[0]) && dataMap.get(id).contains(((String[]) obj)[1])){

position = id;
}
}
}
if(position>-1){
firstnames.remove(position);
lastnames.remove(position);
salarys.remove(position);
locations.remove(position);
populateDatabase();
}

}

}

以及输出(例如):

Insert First Name
david
Insert Last Name
Twelve
Insert Salary
null
Insert Location
null
Filter Result: [1, david, Twelve, 20000, Iceland]
After Adding: {0=[john, Eleven, 10000, India], 1=[david, Twelve, 20000, Iceland], 2=[mathew, Thirteen, 15000, Mexico], 3=[john, Fourteen, 5323, Slovenia], 4=[jerry, Fifteen, 2000, Poland], 5=[Uffe, Sixteen, 5346, Australia], 6=[Sekar, Seventeen, 1000, 1000], 7=[Suresh, Eighteen, 4889, USA], 8=[Ramesh, Nineteen, 7854, England], 9=[Raja, Twenty, 2438, Canada], 10=[Yahya, Almardeny, 3000, Ireland]}
After Deleting: {0=[john, Eleven, 10000, India], 1=[david, Twelve, 20000, Iceland], 2=[mathew, Thirteen, 15000, Mexico], 3=[john, Fourteen, 5323, Slovenia], 4=[jerry, Fifteen, 2000, Poland], 5=[Uffe, Sixteen, 5346, Australia], 6=[Sekar, Seventeen, 1000, 1000], 7=[Suresh, Eighteen, 4889, USA], 8=[Ramesh, Nineteen, 7854, England], 9=[Raja, Twenty, 2438, Canada]}

关于java - 在java中的 map 数据列表上应用过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43659769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com