Underscore-java by javadev
Underscore-java is a java port of the popular Underscore.js library. In addition to porting Underscore's functionality, Underscore-java includes matching unit tests. Underscore-java requires java 1.6 or greater.
Underscore-java is hosted on GitHub and open sourced under the MIT license.
Download
You can download this project in either zip or tar formats.
Table of Contents
Object-Oriented and Static Styles
Collections
each, map, reduce, reduceRight, find, filter, reject, all, any, include, invoke, pluck, max, min, groupBy,sortBy, sortedIndex, shuffle, toArray, size
Arrays
first, initial, rest, last, compact, flatten, without, uniq, union, intersection, difference, zip, indexOf,lastIndexOf, range
Functions
bind, bindAll memoize, delay, defer, throttle, debounce, once, after, wrap, compose
Objects
keys, values, functions, extend, defaults, clon, tap, has, isEqual, isEmpty, isElement, isObject, isArray,isArguments, isFunction, isString, isNumber, isBoolean, isDate, isRegExp, isNaN, isNull, isUndefined
Utility
noConflict, identity, times, mixin, uniqueId, escape, template
Chaining
Some functions were not ported from Underscore.js to Underscore-java for technical reasons or because they weren't applicable to java.
Collections (Arrays or Objects)
each _.each(collection, iterator)
Iterates over the collection and yield each in turn to the iterator function. Arguments passed to iterator are (value, key, collection).
_.each(asList(1, 2, 3), new Block<Integer>() { public void apply(Integer item) { System.out.println(item + ","); } }); // 1,2,3, int multiplier = 2; int index = 0; _.each(asList(1, 2, 3), new Block<Integer>() { public void apply(Integer item) { System.out.println(index + "=" + (item * multiplier) + ","); index += 1; } }); // 0=2,1=4,2=6,
map _.map(collection, iterator)
Alias: collect
Returns an array of values by mapping each in collection through the iterator. Arguments passed to iterator are (value, key, collection).
_.map(asList(1, 2, 3), new Function1<Integer, Integer>() { public Integer apply(Integer item) { return item * 3; } }); // [3, 6, 9] _.map(new LinkedHashMap<Integer, String>() {{ put(1, "one"); put(2, "two"); put(3, "three"); }} .entrySet(), new Function1<Map.Entry<Integer, String>, Integer>() { public Integer apply(Map.Entry<Integer, String> item) { return item.getKey() * 3; } }); // [3, 6, 9]
reduce _.reduce(collection, memo)
Aliases: inject, foldl
Reduce the collection into a single value. Memo is the initial state of the reduction, updated by the return value of the iterator.
_.reduce(asList(1, 2, 3), new FunctionAccum<Integer, Integer>() { public Integer apply(Integer item1, Integer item2) { return item1 + item2; } }, 0 ); // 6
reduceRight _.reduceRight(collection, memo, iterator)
Alias: foldr
Right-associative version of reduce.
_.reduceRight(asList(asList(0, 1), asList(2, 3), asList(4, 5)), new FunctionAccum<List<Integer>, List<Integer>>() { public List<Integer> apply(List<Integer> item1, List<Integer> item2) { List<Integer> list = new ArrayList<Integer>(item1); list.addAll(item2); return list; }, Collections.<Integer>emptyList() }); // [4, 5, 2, 3, 0, 1]
find _.find(collection, iterator)
Alias: detect
Return the value of the first item in the collection that passes the truth test (iterator).
_.find(asList(1, 2, 3, 4, 5, 6), new Predicate<Integer>() { public Boolean apply(Integer item) { return item % 2 == 0; } }); // 2
filter _.filter(collection, iterator)
Alias: select
Return the values in the collection that pass the truth test (iterator).
_.filter(asList(1, 2, 3, 4, 5, 6), new Predicate<Integer>() { public Boolean apply(Integer item) { return item % 2 == 0; } }); // [2, 4, 6]
reject _.reject(collection, iterator)
Return an array where the items failing the truth test (iterator) are removed.
_.reject(asList(1, 2, 3, 4), new Predicate<Integer>() { public Boolean apply(Integer item) { return item % 2 == 0; } }); // [1, 3]
all _.all(collection, iterator)
Returns true if all values in the collection pass the truth test (iterator).
_.all(asList(1, 2, 3, 4), new Predicate<Integer>() { public Boolean apply(Integer item) { return item % 2 == 0; } }); // false _.all(asList(1, 2, 3, 4), new Predicate<Integer>() { public Boolean apply(Integer item) { return item < 5; } }); // true
any _.any(collection, iterator)
Returns true if any values in the collection pass the truth test (iterator).
_.any(asList(1, 2, 3, 4), new Predicate<Integer>() { public Boolean apply(Integer item) { return item % 2 == 0; } }); // true _.any(asList(1, 2, 3, 4), new Predicate<Integer>() { public Boolean apply(Integer item) { return item == 5; } }); // false
include _.include(collection, value)
Alias: contains
Returns true if value is found in the collection using == to test equality.
_.include(asList(1, 2, 3), 3); // true
invoke _.invoke(collection, functionName)
Returns a copy of the collection after running functionName across all elements.
_.invoke(asList(" foo", " bar "), "trim"); // ["foo", "bar"]
pluck _.pluck(collection, propertyName)
Extract an array of property values
class Person { public final String name; public final Integer age; public Person(final String name, final Integer age) { this.name = name; this.age = age; } }; _.pluck(asList(new Person("moe", 40), new Person("larry", 50), new Person("curly", 40)), "name"); // ["moe", "larry", "curly"]
max _.max(collection, [iterator])
Returns the maximum value from the collection. If passed an iterator, max will return max value returned by the iterator.
_.max(asList(10, 5, 100, 2, 1000)); // 100
min _.min(collection, [iterator])
Returns the minimum value from the collection. If passed an iterator, min will return min value returned by the iterator.
_.min(asList(10, 5, 100, 2, 1000)); // 2