在java中对地图数据列表应用过滤器

在java中对地图数据列表应用过滤器,java,collections,filter,Java,Collections,Filter,我有一个列表数据,我想应用一些逻辑条件列表,这些逻辑条件按括号排序。 示例条件类似于((firstname=john和Lastname=11)或(工资=15000,地点=Mexico或(firstname=mathew和Lastname=13)) 我想在列表中运行这些条件,只返回匹配的数据 我写了下面的代码,如果有人可以修改工作的基础上的过滤器,这将是伟大的 package test; import java.util.List; import java.util.ArrayList; imp

我有一个
列表
数据,我想应用一些逻辑条件列表,这些逻辑条件按括号排序。 示例条件类似于
((firstname=john和Lastname=11)或(工资=15000,地点=Mexico或(firstname=mathew和Lastname=13))

我想在
列表中运行这些条件
,只返回匹配的数据

我写了下面的代码,如果有人可以修改工作的基础上的过滤器,这将是伟大的

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;
    }

}
封装测试;
导入java.util.List;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.Map;
导入java.util.stream.collector;
公共类测试过滤器{
公共静态void main(字符串argv[]){
String[]firstnames={“john”、“david”、“mathew”、“john”、“jerry”、“Uffe”、“Sekar”、“Suresh”、“Ramesh”、“Raja”};
String[]secondnames={“十一”、“十二”、“十三”、“十四”、“十五”、“十六”、“十七”、“十八”、“十九”、“二十”};
字符串[]salary={“10000”、“20000”、“15000”、“5323”、“2000”、“5346”、“1000”、“4889”、“7854”、“2438”};
字符串[]位置={“印度”、“冰岛”、“墨西哥”、“斯洛文尼亚”、“波兰”、“澳大利亚”、“1000”、“美国”、“英国”、“加拿大”};
列表=新的ArrayList();
对于(int i=0;i<10;i++){
Map dataMap=newhashmap();
dataMap.put(“firstname”,firstname[i]);
dataMap.put(“secondname”,secondnames[i]);
dataMap.put(“工资”,工资[i]);
dataMap.put(“位置”,位置[i]);
列表。添加(数据映射);
}
String filterRule=“((firstname=john,Lastname=11)或(salary=15000,location=Mexico或(firstname=mathew,Lastname=13))”;
System.out.println(过滤器(列表,过滤器规则));
}
公共静态列表筛选器(列表、字符串筛选器规则){
List filtered=List.stream()
.filter(p->checkFilter(p,filterRules)).collect(collector.toList());
返回过滤;
}
公共静态布尔检查过滤器(映射映射数据、字符串过滤器规则){
//在此处应用条件并返回true或false
//return(mapData.get(“firstname”)+).equalsIgnoreCase(“john”);
//返回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();
        }

    }

}

如果您想支持动态规则。。。你必须研究一些规则引擎,比如Drools,或者至少一个规则表达式grammarI无法理解你想要实现什么!你想把什么样的东西还给你?ArrayList可以吗?您是否接受用户的输入来指定过滤器?@Yahya在函数中,
List
是返回值type@SarveshKumarSingh看起来很有希望,我会尝试一下,如果你有java的任何示例程序,请告诉我
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]}