1
1
package loukaspd .gr .linqarraylistproject ;
2
2
3
- import java .util .ArrayList ;
4
- import java .util .Collection ;
3
+ import java .util .*;
5
4
6
5
public class LinqArrayList <E > {
7
6
// ---------- Class variables - Constructor ---------- //
8
- private final ArrayList <E > _arrayList ;
7
+ private final Collection <E > _collection ;
9
8
public ArrayList <E > getArrayList () {
10
- return _arrayList ;
9
+ if (_collection instanceof ArrayList ) return (ArrayList <E >) _collection ;
10
+ return new ArrayList <>(_collection );
11
11
}
12
+ public Collection <E > getCollection () {return _collection ;}
12
13
13
- public LinqArrayList (ArrayList <E > arrayList ){
14
- _arrayList = arrayList ;
15
- }
16
14
public LinqArrayList (Collection <E > collection ) {
17
- _arrayList = new ArrayList < E >( collection ) ;
15
+ _collection = collection ;
18
16
}
19
17
20
18
21
19
// ---------- Interfaces ---------- //
22
20
public interface VoidFunc <E > {
23
21
void lambda (E item );
24
- };
22
+ }
25
23
public interface BooleanFunc <E > {
26
24
boolean lambda (E item );
27
- };
25
+ }
28
26
public interface SelectFunc <E ,T > {
29
27
T lambda (E item );
30
- };
28
+ }
29
+ public interface SelectManyFunc <E ,T > {
30
+ Collection <T > lambda (E item );
31
+ }
31
32
public interface SumFunc <E > {
32
33
double lambda (E item );
33
- };
34
+ }
34
35
35
36
36
37
37
38
// ---------- Methods ---------- //
38
39
public void forEach (VoidFunc <E > func ) {
39
- for (E item : _arrayList ) {
40
+ Enumeration <E > enumeration = Collections .enumeration (_collection );
41
+
42
+ while (enumeration .hasMoreElements ()) {
43
+ E item = enumeration .nextElement ();
44
+
40
45
func .lambda (item );
41
46
}
42
47
}
43
48
44
49
public E first (BooleanFunc <E > func ){
45
- for (E item : _arrayList ) {
50
+ Enumeration <E > enumeration = Collections .enumeration (_collection );
51
+ while (enumeration .hasMoreElements ()) {
52
+ E item = enumeration .nextElement ();
53
+
46
54
if (func .lambda (item )) return item ;
47
55
}
48
56
throw new IllegalStateException ();
49
57
}
50
58
51
59
public E firstOrDefault (BooleanFunc <E > func ){
52
- for (E item : _arrayList ) {
60
+ Enumeration <E > enumeration = Collections .enumeration (_collection );
61
+ while (enumeration .hasMoreElements ()) {
62
+ E item = enumeration .nextElement ();
63
+
53
64
if (func .lambda (item )) return item ;
54
65
}
55
66
return null ;
56
67
}
57
68
58
69
public LinqArrayList <E > where (BooleanFunc <E > func ) {
59
70
ArrayList <E > result = new ArrayList <E >();
60
- for (E item : _arrayList ) {
71
+ Enumeration <E > enumeration = Collections .enumeration (_collection );
72
+ while (enumeration .hasMoreElements ()) {
73
+ E item = enumeration .nextElement ();
74
+
61
75
if (func .lambda (item )) result .add (item );
62
76
}
63
77
return new LinqArrayList <E >(result );
64
78
}
65
79
66
80
public <T > LinqArrayList <T > select (SelectFunc <E ,T > func ) {
67
81
ArrayList <T > result = new ArrayList <T >();
68
- for (E item : _arrayList ) {
82
+
83
+ Enumeration <E > enumeration = Collections .enumeration (_collection );
84
+ while (enumeration .hasMoreElements ()) {
85
+ E item = enumeration .nextElement ();
86
+
69
87
result .add (func .lambda (item ));
70
88
}
71
89
return new LinqArrayList <T >(result );
72
90
}
73
91
92
+ public <T > LinqArrayList <T > selectMany (SelectManyFunc <E ,T > func ) {
93
+ ArrayList <T > result = new ArrayList <T >();
94
+
95
+ Enumeration <E > enumeration = Collections .enumeration (_collection );
96
+ while (enumeration .hasMoreElements ()) {
97
+ E item = enumeration .nextElement ();
98
+
99
+ Collection <T > collection = func .lambda (item );
100
+ if (collection == null ) continue ;
101
+ result .addAll (collection );
102
+ }
103
+
104
+ return new LinqArrayList <T >(result );
105
+ }
106
+
74
107
public boolean any (BooleanFunc <E > func ) {
75
- for (E item : _arrayList ) {
108
+ Enumeration <E > enumeration = Collections .enumeration (_collection );
109
+ while (enumeration .hasMoreElements ()) {
110
+ E item = enumeration .nextElement ();
111
+
76
112
if (func .lambda (item )) return true ;
77
113
}
78
114
return false ;
79
115
}
80
116
81
117
public boolean remove (BooleanFunc <E > func ) {
82
- for (E item : _arrayList ) {
118
+ Enumeration <E > enumeration = Collections .enumeration (_collection );
119
+ while (enumeration .hasMoreElements ()) {
120
+ E item = enumeration .nextElement ();
121
+
83
122
if (func .lambda (item )) {
84
- _arrayList .remove (item );
123
+ _collection .remove (item );
85
124
return true ;
86
125
}
87
126
}
@@ -90,7 +129,11 @@ public boolean remove(BooleanFunc<E> func) {
90
129
91
130
public LinqArrayList <E > distinct () {
92
131
ArrayList <E > result = new ArrayList <>();
93
- for (E item : _arrayList ) {
132
+
133
+ Enumeration <E > enumeration = Collections .enumeration (_collection );
134
+ while (enumeration .hasMoreElements ()) {
135
+ E item = enumeration .nextElement ();
136
+
94
137
if (!result .contains (item )) {
95
138
result .add (item );
96
139
}
@@ -100,9 +143,32 @@ public LinqArrayList<E> distinct() {
100
143
101
144
public double sum (SumFunc <E > func ) {
102
145
double result = 0 ;
103
- for (E item : _arrayList ) {
146
+
147
+ Enumeration <E > enumeration = Collections .enumeration (_collection );
148
+ while (enumeration .hasMoreElements ()) {
149
+ E item = enumeration .nextElement ();
150
+
104
151
result += func .lambda (item );
105
152
}
106
153
return result ;
107
154
}
155
+
156
+ public <T > Hashtable <T ,ArrayList <E >> groupBy (SelectFunc <E ,T > func ) {
157
+ Hashtable <T ,ArrayList <E >> dictionary = new Hashtable <T ,ArrayList <E >>();
158
+
159
+ Enumeration <E > enumeration = Collections .enumeration (_collection );
160
+ while (enumeration .hasMoreElements ()) {
161
+ E item = enumeration .nextElement ();
162
+
163
+ T key = func .lambda (item );
164
+ if (dictionary .containsKey (key )) dictionary .get (key ).add (item );
165
+ else {
166
+ ArrayList <E > group = new ArrayList <>();
167
+ group .add (item );
168
+ dictionary .put (key , group );
169
+ }
170
+ }
171
+
172
+ return dictionary ;
173
+ }
108
174
}
0 commit comments