Implement paging, fuzzy search, and column filtering functions through streams

Implement paging, fuzzy search, and column filtering functions through streams

background

After some data is queried through the database, it needs to undergo certain logical processing before it can be displayed on the front end. At this time, corresponding paging, fuzzy search, and column filtering need to be performed in the program. These functions may be cumbersome to process through ordinary logic, but processing through streams will be much simpler.

logical display

This is a written template. I feel that it can also be written as a general process based on this. If it is used again, I will add it later
Insert image description here
Code explanation
①: Reconstruct the list that has completed data processing into a new list. Of course, the data references in it are the same, but the new list can be added, deleted or modified
②: Set paging The total number, this value is often used by the front end.
③: Fuzzy search, obtain the fuzzy search value passed in from the front end
④: This can support fuzzy search of two fields, use filter combined with || operation to obtain Result
5: Filtering by column, here also through the filter function, the code is processed as follows:
Insert image description here
Filtering by column is similar to fuzzy search, but what is passed in is a map<k, v>, k is the field name, and v is the fuzzy search content. Then iterate through this map. If it can match the object value in the filter, keep it; if it cannot match, discard it. Finally return this list
Because the size will change after filtering is completed, total is reset

⑥: Sorting, sorting can also be done by column sorting, use the sorted method, the parameter needs a compartor type, the custom sortBy() method is generated as follows:
Insert image description here
To sort the fields, there are string, numeric and default sorting, sorting method desc or asc, etc. If you want to add time class or other types, you need to manually implement the comparter interface to construct the comparison method.

⑦Paging: Paging uses the skip and limit functions to implement the current page and the number displayed on each page, which is relatively simple
⑧Save and return the results

Sample code

public class Person {
    
    
    private String name;
    private int age;
    private String city;

    // 构造函数、getter和setter方法
}
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class PersonStreamExample {
    
    

    public static void main(String[] args) {
    
    
        List<Person> persons = getSampleData(); // 获取示例数据

        // 分页
        int pageSize = 10;
        int pageNumber = 1;
        List<Person> page = persons.stream()
                .skip((pageNumber - 1) * pageSize)
                .limit(pageSize)
                .collect(Collectors.toList());

        // 按列过滤
        String filterColumn = "city";
        String filterValue = "New York";
        List<Person> filtered = persons.stream()
                .filter(person -> filterColumn.equals("name") && person.getName().contains(filterValue))
                .filter(person -> filterColumn.equals("age") && person.getAge() >= Integer.parseInt(filterValue))
                .filter(person -> filterColumn.equals("city") && person.getCity().equals(filterValue))
                .collect(Collectors.toList());

        // 模糊搜索
        String keyword = "John";
        List<Person> searchResults = persons.stream()
                .filter(person -> person.getName().contains(keyword) || person.getCity().contains(keyword))
                .collect(Collectors.toList());

        // 按列排序
        String sortColumn = "age";
        List<Person> sorted = persons.stream()
                .sorted(Comparator.comparingInt(person -> {
    
    
                    if (sortColumn.equals("name")) {
    
    
                        return person.getName().hashCode();
                    } else if (sortColumn.equals("age")) {
    
    
                        return person.getAge();
                    } else if (sortColumn.equals("city")) {
    
    
                        return person.getCity().hashCode();
                    } else {
    
    
                        return 0;
                    }
                }))
                .collect(Collectors.toList());
    }

    // 获取示例数据
    private static List<Person> getSampleData() {
    
    
        // 创建和返回示例数据的逻辑
    }
}

It will be used later, and the content extracted as a general method will be supplemented.

Guess you like

Origin blog.csdn.net/qq_40454136/article/details/132887563