├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── LinqArraylist ├── proguard-rules.pro ├── build.gradle └── src │ └── main │ └── java │ └── loukaspd │ └── gr │ └── linqarraylistproject │ └── LinqArrayList.java ├── License.md ├── gradlew.bat ├── Readme.md └── gradlew /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':LinqArraylist' 2 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/loukaspd/LinqArraylist/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /LinqArraylist/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /License.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 loukaspd 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LinqArraylist/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | 3 | ext { 4 | //For bintray and jcenter distribution 5 | bintrayRepo = 'LinqArraylist' 6 | bintrayName = 'LinqArraylist' 7 | 8 | publishedGroupId = 'gr.loukaspd' 9 | libraryName = 'LinqArraylist' 10 | artifact = 'LinqArraylist' 11 | 12 | libraryDescription = 'Linq-like functionality for Java Collections' 13 | libraryVersion = '1.1.0' 14 | 15 | siteUrl = 'https://github.com/loukaspd/LinqArraylist' 16 | gitUrl = 'https://github.com/loukaspd/LinqArraylist.git' 17 | 18 | developerId = 'gr.loukaspd' 19 | developerName = 'loukaspd' 20 | developerEmail = 'loukaspd@gmail.com' 21 | organization = '' 22 | licenseName = 'The Apache Software License, Version 2.0' 23 | licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt' 24 | allLicenses = ["Apache-2.0"] 25 | } 26 | 27 | //These two remote files contain Bintray configuration 28 | apply from: 'https://raw.githubusercontent.com/quangctkm9207/template-files/master/android/gradle/install.gradle' 29 | apply from: 'https://raw.githubusercontent.com/quangctkm9207/template-files/master/android/gradle/bintray.gradle' 30 | 31 | /* 32 | android { 33 | compileSdkVersion 27 34 | buildToolsVersion "28.0.0" 35 | defaultConfig { 36 | applicationId "loukaspd.gr.linqarraylistproject" 37 | minSdkVersion 21 38 | targetSdkVersion 27 39 | versionCode 1 40 | versionName "1.0" 41 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 42 | } 43 | buildTypes { 44 | release { 45 | minifyEnabled false 46 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 47 | } 48 | } 49 | } 50 | 51 | dependencies { 52 | implementation fileTree(dir: 'libs', include: ['*.jar']) 53 | implementation 'com.android.support:appcompat-v7:27.1.1' 54 | testImplementation 'junit:junit:4.12' 55 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 56 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 57 | } 58 | */ 59 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS="-Xmx64m" 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # LinqArraylist 2 | 3 | [ ![Download](https://api.bintray.com/packages/loukaspd/LinqArraylist/LinqArraylist/images/download.svg) ](https://bintray.com/loukaspd/LinqArraylist/LinqArraylist/_latestVersion) 4 | [![Android Arsenal]( https://img.shields.io/badge/Android%20Arsenal-LinqArraylist-green.svg?style=flat )]( https://android-arsenal.com/details/1/7060 ) 5 | use Linq-like functions to Java Collections 6 | 7 | 8 | ``` 9 | implementation 'gr.loukaspd:LinqArraylist:{latest version}' 10 | ``` 11 | where `{latest version}` corresponds to published version in [ ![Download](https://api.bintray.com/packages/loukaspd/LinqArraylist/LinqArraylist/images/download.svg) ](https://bintray.com/loukaspd/LinqArraylist/LinqArraylist/_latestVersion) 12 | [![Android Arsenal]( https://img.shields.io/badge/Android%20Arsenal-LinqArraylist-green.svg?style=flat )]( https://android-arsenal.com/details/1/7060 ) 13 | 14 | ## Usage 15 | 1. Instantiate a LinqArrayList object by passing it your original Collection 16 | 2. Apply linq methods (even chaining) like select, where etc.. 17 | 3. Get the resulting Collection by calling `getCollection` or `getArrayList()` to get a new ArrayList from the collection 18 | 19 | 20 | ## Examples 21 | 22 | ```java 23 | public static void main(String[] args) { 24 | ArrayList myList = new ArrayList<>(); 25 | myList.add(new SampleClass(1, "red")); 26 | myList.add(new SampleClass(2, "green")); 27 | myList.add(new SampleClass(3, "blue")); 28 | 29 | 30 | ArrayList result = new LinqArrayList<>(myList) 31 | .where(new LinqArrayList.BooleanFunc() { 32 | @Override 33 | public boolean lambda(SampleClass item) { 34 | return item.id > 1; 35 | } 36 | }) 37 | .getArrayList(); 38 | System.out.println(result); //[2: green, 3: blue] 39 | 40 | System.out.println(new LinqArrayList<>(myList) 41 | .firstOrDefault(new LinqArrayList.BooleanFunc() { 42 | @Override 43 | public boolean lambda(SampleClass item) { 44 | return item.color.equals("red"); 45 | } 46 | })); //1: red 47 | 48 | 49 | // chaining 50 | System.out.println( 51 | new LinqArrayList<>(myList) 52 | .where(new LinqArrayList.BooleanFunc() { 53 | @Override 54 | public boolean lambda(SampleClass item) { 55 | return item.id > 1; 56 | } 57 | }) 58 | .select(new LinqArrayList.SelectFunc() { 59 | @Override 60 | public String lambda(SampleClass item) { 61 | return item.color; 62 | } 63 | }) 64 | .getArrayList() 65 | ); //[green, blue] 66 | } 67 | ``` -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS='"-Xmx64m"' 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /LinqArraylist/src/main/java/loukaspd/gr/linqarraylistproject/LinqArrayList.java: -------------------------------------------------------------------------------- 1 | package loukaspd.gr.linqarraylistproject; 2 | 3 | import java.util.*; 4 | 5 | public class LinqArrayList { 6 | // ---------- Class variables - Constructor ---------- // 7 | private final Collection _collection; 8 | public ArrayList getArrayList() { 9 | if (_collection instanceof ArrayList) return (ArrayList) _collection; 10 | return new ArrayList<>(_collection); 11 | } 12 | public Collection getCollection() {return _collection;} 13 | 14 | public LinqArrayList(Collection collection) { 15 | _collection = collection; 16 | } 17 | 18 | 19 | // ---------- Interfaces ---------- // 20 | public interface VoidFunc { 21 | void lambda(E item); 22 | } 23 | public interface BooleanFunc { 24 | boolean lambda(E item); 25 | } 26 | public interface SelectFunc { 27 | T lambda(E item); 28 | } 29 | public interface SelectManyFunc { 30 | Collection lambda(E item); 31 | } 32 | public interface SumFunc { 33 | double lambda(E item); 34 | } 35 | 36 | 37 | 38 | // ---------- Methods ---------- // 39 | public void forEach(VoidFunc func) { 40 | Enumeration enumeration = Collections.enumeration(_collection); 41 | 42 | while(enumeration.hasMoreElements()) { 43 | E item = enumeration.nextElement(); 44 | 45 | func.lambda(item); 46 | } 47 | } 48 | 49 | public E first(BooleanFunc func){ 50 | Enumeration enumeration = Collections.enumeration(_collection); 51 | while(enumeration.hasMoreElements()) { 52 | E item = enumeration.nextElement(); 53 | 54 | if (func.lambda(item)) return item; 55 | } 56 | throw new IllegalStateException(); 57 | } 58 | 59 | public E firstOrDefault(BooleanFunc func){ 60 | Enumeration enumeration = Collections.enumeration(_collection); 61 | while(enumeration.hasMoreElements()) { 62 | E item = enumeration.nextElement(); 63 | 64 | if (func.lambda(item)) return item; 65 | } 66 | return null; 67 | } 68 | 69 | public LinqArrayList where(BooleanFunc func) { 70 | ArrayList result = new ArrayList(); 71 | Enumeration enumeration = Collections.enumeration(_collection); 72 | while(enumeration.hasMoreElements()) { 73 | E item = enumeration.nextElement(); 74 | 75 | if (func.lambda(item)) result.add(item); 76 | } 77 | return new LinqArrayList(result); 78 | } 79 | 80 | public LinqArrayList select(SelectFunc func) { 81 | ArrayList result = new ArrayList(); 82 | 83 | Enumeration enumeration = Collections.enumeration(_collection); 84 | while(enumeration.hasMoreElements()) { 85 | E item = enumeration.nextElement(); 86 | 87 | result.add(func.lambda(item)); 88 | } 89 | return new LinqArrayList(result); 90 | } 91 | 92 | public LinqArrayList selectMany(SelectManyFunc func) { 93 | ArrayList result = new ArrayList(); 94 | 95 | Enumeration enumeration = Collections.enumeration(_collection); 96 | while(enumeration.hasMoreElements()) { 97 | E item = enumeration.nextElement(); 98 | 99 | Collection collection = func.lambda(item); 100 | if (collection == null) continue; 101 | result.addAll(collection); 102 | } 103 | 104 | return new LinqArrayList(result); 105 | } 106 | 107 | public boolean any(BooleanFunc func) { 108 | Enumeration enumeration = Collections.enumeration(_collection); 109 | while(enumeration.hasMoreElements()) { 110 | E item = enumeration.nextElement(); 111 | 112 | if (func.lambda(item)) return true; 113 | } 114 | return false; 115 | } 116 | 117 | public boolean remove(BooleanFunc func) { 118 | Enumeration enumeration = Collections.enumeration(_collection); 119 | while(enumeration.hasMoreElements()) { 120 | E item = enumeration.nextElement(); 121 | 122 | if (func.lambda(item)) { 123 | _collection.remove(item); 124 | return true; 125 | } 126 | } 127 | return false; 128 | } 129 | 130 | public LinqArrayList distinct() { 131 | ArrayList result = new ArrayList<>(); 132 | 133 | Enumeration enumeration = Collections.enumeration(_collection); 134 | while(enumeration.hasMoreElements()) { 135 | E item = enumeration.nextElement(); 136 | 137 | if(!result.contains(item)) { 138 | result.add(item); 139 | } 140 | } 141 | return new LinqArrayList<>(result); 142 | } 143 | 144 | public double sum(SumFunc func) { 145 | double result = 0; 146 | 147 | Enumeration enumeration = Collections.enumeration(_collection); 148 | while(enumeration.hasMoreElements()) { 149 | E item = enumeration.nextElement(); 150 | 151 | result += func.lambda(item); 152 | } 153 | return result; 154 | } 155 | 156 | public Hashtable> groupBy(SelectFunc func) { 157 | Hashtable> dictionary = new Hashtable>(); 158 | 159 | Enumeration 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 group = new ArrayList<>(); 167 | group.add(item); 168 | dictionary.put(key, group); 169 | } 170 | } 171 | 172 | return dictionary; 173 | } 174 | } 175 | --------------------------------------------------------------------------------