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

时间:2017-04-27 13:47:47

标签: java collections filter

我有一个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;
    }

}

1 个答案:

答案 0 :(得分:1)

我相信我的回答并不完全符合您的要求,但它可能会对您有所帮助或者对如何过滤的新想法有所帮助。 我的想法是通过创建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]}