11package loukaspd .gr .linqarraylistproject ;
22
3- import java .util .ArrayList ;
4- import java .util .Collection ;
3+ import java .util .*;
54
65public class LinqArrayList <E > {
76 // ---------- Class variables - Constructor ---------- //
8- private final ArrayList <E > _arrayList ;
7+ private final Collection <E > _collection ;
98 public ArrayList <E > getArrayList () {
10- return _arrayList ;
9+ if (_collection instanceof ArrayList ) return (ArrayList <E >) _collection ;
10+ return new ArrayList <>(_collection );
1111 }
12+ public Collection <E > getCollection () {return _collection ;}
1213
13- public LinqArrayList (ArrayList <E > arrayList ){
14- _arrayList = arrayList ;
15- }
1614 public LinqArrayList (Collection <E > collection ) {
17- _arrayList = new ArrayList < E >( collection ) ;
15+ _collection = collection ;
1816 }
1917
2018
2119 // ---------- Interfaces ---------- //
2220 public interface VoidFunc <E > {
2321 void lambda (E item );
24- };
22+ }
2523 public interface BooleanFunc <E > {
2624 boolean lambda (E item );
27- };
25+ }
2826 public interface SelectFunc <E ,T > {
2927 T lambda (E item );
30- };
28+ }
29+ public interface SelectManyFunc <E ,T > {
30+ Collection <T > lambda (E item );
31+ }
3132 public interface SumFunc <E > {
3233 double lambda (E item );
33- };
34+ }
3435
3536
3637
3738 // ---------- Methods ---------- //
3839 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+
4045 func .lambda (item );
4146 }
4247 }
4348
4449 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+
4654 if (func .lambda (item )) return item ;
4755 }
4856 throw new IllegalStateException ();
4957 }
5058
5159 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+
5364 if (func .lambda (item )) return item ;
5465 }
5566 return null ;
5667 }
5768
5869 public LinqArrayList <E > where (BooleanFunc <E > func ) {
5970 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+
6175 if (func .lambda (item )) result .add (item );
6276 }
6377 return new LinqArrayList <E >(result );
6478 }
6579
6680 public <T > LinqArrayList <T > select (SelectFunc <E ,T > func ) {
6781 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+
6987 result .add (func .lambda (item ));
7088 }
7189 return new LinqArrayList <T >(result );
7290 }
7391
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+
74107 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+
76112 if (func .lambda (item )) return true ;
77113 }
78114 return false ;
79115 }
80116
81117 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+
83122 if (func .lambda (item )) {
84- _arrayList .remove (item );
123+ _collection .remove (item );
85124 return true ;
86125 }
87126 }
@@ -90,7 +129,11 @@ public boolean remove(BooleanFunc<E> func) {
90129
91130 public LinqArrayList <E > distinct () {
92131 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+
94137 if (!result .contains (item )) {
95138 result .add (item );
96139 }
@@ -100,9 +143,32 @@ public LinqArrayList<E> distinct() {
100143
101144 public double sum (SumFunc <E > func ) {
102145 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+
104151 result += func .lambda (item );
105152 }
106153 return result ;
107154 }
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+ }
108174}
0 commit comments