1.集合中元素个数,10M
2.根据上限和下限从一个Set中过滤出满足要求的元素集合.

测试效果:集合大小100K, 运算时间 3000ms+

1     void filerSet(Set<BigDecimal> targetSet, String lower, String higher) {
2         BigDecimal bdLower = new BigDecimal(Double.parseDouble(lower));
3         BigDecimal bdHigher = new BigDecimal(Double.parseDouble(higher));
4
5         Set<BigDecimal> returnSet = new HashSet<BigDecimal>();
6         for (BigDecimal object : targetSet) {
7             if (isInRange(object, bdLower, bdHigher)) {
9             }
10         }
11     }
12
13     private boolean isInRange(BigDecimal object, BigDecimal bdLower,
14             BigDecimal bdHigher) {
15         return object.compareTo(bdLower) >= 0
16                 && object.compareTo(bdHigher) <= 0;
17     }

测试效果: 集合大小10M, 运算时间: 12000ms+(获得TreeSet) , 200ms(获得结果)

1     Set<BigDecimal> getSubSet(TreeSet<BigDecimal> targetSet, String lower,
2             String higher) {
3
4         BigDecimal bdLower = new BigDecimal(Double.parseDouble(lower));
5         BigDecimal bdHigher = new BigDecimal(Double.parseDouble(higher));
6
7         if ((bdHigher.compareTo(targetSet.first()) == -1)
8                 || (bdLower.compareTo(targetSet.last()) == 1)) {
9             return null;
10         }
11
12         boolean hasLower = targetSet.contains(bdLower);
13         boolean hasHigher = targetSet.contains(bdHigher);
14         if (hasLower) {
15             if (hasHigher) {
16                 System.out.println("get start:" + bdLower);
17                 System.out.println("get end:" + bdHigher);
18                 return targetSet.subSet(bdLower, true, bdHigher, true);
19             } else {
20                 BigDecimal newEnd = null;
21                 System.out.println("get start:" + bdLower);
22                 SortedSet<BigDecimal> returnSet = null;
23                 if (bdHigher.compareTo(targetSet.last()) != -1) {
24                     newEnd = targetSet.last();
25                 } else {
26                     SortedSet<BigDecimal> newTargetSet = targetSet
27                             .tailSet(bdLower);
28                     for (BigDecimal object : newTargetSet) {
29                         if (object.compareTo(bdHigher) == 1) {
30                             newEnd = object;
31                             break;
32                         } else if (object.compareTo(bdHigher) == 0) {
33                             newEnd = object;
34                             break;
35                         }
36                     }
37                 }
38                 returnSet = targetSet.subSet(bdLower, true, newEnd, true);
39                 if (newEnd.compareTo(bdHigher) == 1) {
40                     returnSet.remove(newEnd);
41                 }
42                 return returnSet;
43             }
44
45         } else {
46             if (hasHigher) {
47                 System.out.println("get end:" + bdHigher);
48                 TreeSet<BigDecimal> newTargetSet = (TreeSet<BigDecimal>) targetSet
50                 BigDecimal newStart = null;
51                 SortedSet<BigDecimal> returnSet = null;
52
53                 if (bdLower.compareTo(targetSet.first()) == -1) {
54                     newStart = targetSet.first();
55                 } else {
56                     for (BigDecimal object : newTargetSet) {
57                         if (object.compareTo(bdLower) != -1) {
58                             newStart = object;
59                             break;
60                         }
61                     }
62                 }
63                 returnSet = targetSet.subSet(newStart, true, bdHigher, true);
64
65                 return returnSet;
66             } else {
67                 System.out.println("Not get start:" + bdLower);
68                 System.out.println("Not get end:" + bdHigher);
69                 BigDecimal newStart = null;
70                 BigDecimal newEnd = null;
71                 if (bdHigher.compareTo(targetSet.last()) != -1) {
72                     newEnd = targetSet.last();
73                 }
74                 if (bdLower.compareTo(targetSet.first()) == -1) {
75                     newStart = targetSet.first();
76                 }
77                 for (BigDecimal object : targetSet) {
78                     if (newStart == null) {
79                         if (object.compareTo(bdLower) != -1) {
80                             newStart = object;
81                             if (newEnd != null) {
82                                 break;
83                             }
84                         }
85                     }
86
87                     if (newEnd == null) {
88                         if (object.compareTo(bdHigher) != -1) {
89                             newEnd = object;
90                             if (newStart != null) {
91                                 break;
92                             }
93                         }
94                     }
95                 }
96
97                 if (newStart == null) {
98                     if (newEnd == null) {
99                         if ((bdHigher.compareTo(targetSet.first()) == -1)
100                                 || (bdLower.compareTo(targetSet.last()) == 1)) {
101                             return null;
102                         }
103                         return targetSet;
104                     } else {
106                                 newEnd, true);
107                         if (newEnd.compareTo(bdHigher) == 1) {
108                             newTargetSet.remove(newEnd);
109                         }
110                         return newTargetSet;
111                     }
112                 } else {
113                     if (newEnd == null) {
114                         SortedSet<BigDecimal> newTargetSet = targetSet.tailSet(
115                                 newStart, true);
116                         return newTargetSet;
117                     } else {
118                         SortedSet<BigDecimal> newTargetSet = targetSet.subSet(
119                                 newStart, true, newEnd, true);
120                         if (newEnd.compareTo(bdHigher) == 1) {
121                             newTargetSet.remove(newEnd);
122                         }
123                         return newTargetSet;
124                     }
125                 }
126             }
127         }
128     }

测试效果:集合大小10M,过滤结果1M, 运算时间: 1000ms+

1 //过滤的主体逻辑
2     void filterSet(Set<BigDecimal> targetSet, String lower, String higher) {
3         final BigDecimal bdLower = new BigDecimal(Double.parseDouble(lower));
4         final BigDecimal bdHigher = new BigDecimal(Double.parseDouble(higher));
5
6         Predicate predicate = new Predicate() {
7             public boolean evaluate(Object object) {
8                 BigDecimal bDObject = (BigDecimal) object;
9                 return bDObject.compareTo(bdLower) >= 0
10                         && bDObject.compareTo(bdHigher) <= 0;
11             }
12         };
13
14         CollectionUtils.filter(targetSet, predicate);
15     }

测试效果:集合大小10M,过滤结果1M, 运算时间: 100ms-

1 //guava filter
2
3     Set<BigDecimal> filterSet(Set<BigDecimal> targetSet, String lower,
4             String higher) {
5         final BigDecimal bdLower = new BigDecimal(Double.parseDouble(lower));
6         final BigDecimal bdHigher = new BigDecimal(Double.parseDouble(higher));
7
8         Set<BigDecimal> filterCollection = Sets.filter(targetSet,
9                 new Predicate<BigDecimal>() {
10                     @Override
11                     public boolean apply(BigDecimal input) {
12                         BigDecimal bDObject = (BigDecimal) input;
13                         return bDObject.compareTo(bdLower) >= 0
14                                 && bDObject.compareTo(bdHigher) <= 0;
15                     }
16                 });
17
18         return filterCollection;
19     }

posted on 2014-06-21 23:33 混沌中立 阅读(7242) 评论(10)  编辑  收藏 所属分类: about java & j2ee

# re: 一个简单的Java集合范围过滤的多个方式对比 2014-06-22 18:07 | java论坛

http://www.itqx.net  回复  更多评论

# re: 一个简单的Java集合范围过滤的多个方式对比 2014-06-22 18:59 | 混沌中立

@java论坛
回复  更多评论

# re: 一个简单的Java集合范围过滤的多个方式对比 2014-06-23 00:07 | 亚历山大
guava并没有立即执行，而是缓执行，遍历下就知道了。无序的话，无论如何复杂度都是N。  回复  更多评论

# re: 一个简单的Java集合范围过滤的多个方式对比 2014-06-23 10:43 | 金利锁业

# re: 一个简单的Java集合范围过滤的多个方式对比 2014-06-23 11:39 | java论坛
@混沌中立

回复  更多评论

# re: 一个简单的Java集合范围过滤的多个方式对比 2014-06-23 21:55 | 混沌中立

@亚历山大
回复  更多评论

# re: 一个简单的Java集合范围过滤的多个方式对比 2014-06-24 05:35 | 金利锁业

# re: 一个简单的Java集合范围过滤的多个方式对比 2014-07-08 10:03 | 博客写什么

# re: 一个简单的Java集合范围过滤的多个方式对比 2014-07-08 20:27 | 旺达锁业

# re: 一个简单的Java集合范围过滤的多个方式对比 2014-07-19 14:56 | 个人品牌

 只有注册用户登录后才能发表评论。 网站导航: 相关文章: