Java LinqCollection 仿Linq的list常用函数

目前支持find,findAll,sort,select,remove等,java不支持lamda函数,因此用接口代替

1
2
3
4
5
6
7
8
9
10
11
12
public interface Fun<T1,T2> {
    public T2 process(T1 item);
}
 
public interface Predicate<T> {
    /**
     * 是否满足
     * @param item
     * @return
     */
    public boolean evaluate(T item);
}

接下来是具体的实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
public class LinqCollection<T> extends java.util.ArrayList<T> {
 
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -5042194313851746204L;
 
    public LinqCollection(){
        super();
    }
     
    public LinqCollection(Collection<? extends T> c){
        super(c);
    }
     
    /**
     * 查找首条符合条件的记录
     * @param predicate 条件
     * @return
     */
    public T find(Predicate<T> predicate){
        for(T item: this){
            if(predicate.evaluate(item)){
                return item;
            }
        }
        return null;
    }
     
    /**
     * 按条件删除
     * @param predicate
     */
    public void remove(Predicate<T> predicate){
         ListIterator<T> i = this.listIterator();
         while(i.hasNext()){
             T c = i.next();
             if(predicate.evaluate(c)){
                 i.remove();
             }
         }
    }
     
    class ComparableItem<T> implements Comparable{
 
        T data;
        Fun<T,  ?> keySelect =null;
        public  <T2 extends Comparable<? super T2>> ComparableItem(T item,Fun<T,T2> keySelect){
            this.keySelect = keySelect;
            this.data = item;
        }
         
        @Override
        public int compareTo(Object o) {
            return ((Comparable)(this.keySelect.process(this.data))).compareTo((Comparable)(this.keySelect.process(((ComparableItem<T>)o).data)));
        }
         
    }
     
    /**
     * 选择
     * @param keySelect
     * @return
     */
    public <T2> LinqCollection<T2> select(Fun<T,T2> keySelect){
        LinqCollection<T2> result = new LinqCollection<T2>();
        for(T item : this){
            result.add(keySelect.process(item));
        }
        return result;
    }
     
    /**
     * 按指定字段排序
     * @param keySelect(选择排序的字段)
     */
    public <T2 extends Comparable<? super T2>> void sort(Fun<T,T2> keySelect){
        List<ComparableItem<T>> items = Lists.newArrayList();
        for(T item : this){
            items.add(new ComparableItem<T>(item, keySelect));
        }
       Collections.sort(items);
       ListIterator i = this.listIterator();
        for (int j=0; j<items.size(); j++) {
            i.next();
            i.set(items.get(j).data);
        }
    }
     
    /**
     * 查找所有符合条件的记录
     * @param predicate
     * @return
     */
    public LinqCollection<T> findAll(Predicate<T> predicate){
        LinqCollection<T> result = new LinqCollection<T>();
        for(T item: this){
            if(predicate.evaluate(item)){
                result.add(item);
            }
        }
        return result;
    }
     
    /**
     * 是否存在
     * @param predicate
     * @return
     */
    public boolean exist(Predicate<T> predicate){
        return this.find(predicate)!=null;
    }
}

  使用举例:

1
2
3
4
5
6
cleanItems.sort(new Fun<SameNameSinger, Integer>() {
                @Override
                public Integer process(SameNameSinger item) {
                    return item.getNameIndex();
                }          
            });

  

 

 

关注作者

欢迎关注作者微信公众号, 一起交流软件开发:欢迎关注作者微信公众号

posted @   JadePeng  阅读(3570)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示