-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathCollection.java
More file actions
92 lines (77 loc) · 2.34 KB
/
Collection.java
File metadata and controls
92 lines (77 loc) · 2.34 KB
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
package ssj.algorithm;
import com.google.common.base.Preconditions;
import ssj.algorithm.collections.Vector;
import java.util.Comparator;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
/**
* Created by shenshijun on 15/2/1.
*/
public interface Collection<T> extends Iterable<T>, Cloneable {
public void add(T ele);
public default <R> Collection<R> map(Function<? super T, ? extends R> func) {
Preconditions.checkNotNull(func);
Collection<R> new_collection = newWithCapacity(size());
for (T ele : this) {
new_collection.add(func.apply(ele));
}
return new_collection;
}
public default Collection<T> filter(Predicate<? super T> is_func) {
Preconditions.checkNotNull(is_func);
Collection<T> new_collection = this.newWithZero();
for (T ele : this) {
if (is_func.test(ele)) {
new_collection.add(ele);
}
}
return new_collection;
}
public default <R> R reduce(BiFunction<? super T, ? super R, ? extends R> func, R start) {
Preconditions.checkNotNull(func);
Preconditions.checkNotNull(start);
R new_start = start;
for (T ele : this) {
new_start = func.apply(ele, new_start);
}
return new_start;
}
public default boolean isEmpty() {
return size() <= 0;
}
public void delete(T ele);
/**
* 这个方法用来实现在接口或者抽象类中创建功能的方式
* 因为Java中没有办法在接口中创建对象,因此只能使用这种方式来处理
*
* @param <R>
* @return
*/
public <R> Collection<R> newWithCapacity(int size);
public default <R> Collection<R> newWithZero() {
return newWithCapacity(0);
}
/**
* 集合的元素个数
*
* @return
*/
public int size();
/**
* 集合的容量,可能里面没有元素
*
* @return
*/
public int capacity();
public default Vector<T> toVector() {
Vector<T> result = new Vector<>(size());
for (T ele : this) {
result.add(ele);
}
return result;
}
public default Vector<T> sort(Comparator<? super T> comparator) {
return toVector().sortThis(comparator);
}
}