Skip to content

Commit ef41aed

Browse files
committed
Add U.countBy(iterator) method.
1 parent 276e40b commit ef41aed

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

src/main/java/com/github/underscore/U.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,11 +1020,28 @@ public static <K, E> Map<K, Integer> countBy(final Iterable<E> iterable, Functio
10201020
return retVal;
10211021
}
10221022

1023+
public static <K> Map<K, Integer> countBy(final Iterable<K> iterable) {
1024+
final Map<K, Integer> retVal = newLinkedHashMap();
1025+
for (K key : iterable) {
1026+
if (retVal.containsKey(key)) {
1027+
retVal.put(key, 1 + retVal.get(key));
1028+
} else {
1029+
retVal.put(key, 1);
1030+
}
1031+
}
1032+
return retVal;
1033+
}
1034+
10231035
@SuppressWarnings("unchecked")
10241036
public <K, E> Map<K, Integer> countBy(Function<E, K> func) {
10251037
return countBy((Iterable<E>) iterable, func);
10261038
}
10271039

1040+
@SuppressWarnings("unchecked")
1041+
public <K> Map<K, Integer> countBy() {
1042+
return countBy((Iterable<K>) iterable);
1043+
}
1044+
10281045
/*
10291046
* Documented, #toArray
10301047
*/
@@ -2885,6 +2902,10 @@ public <F> Chain<Map<F, Integer>> countBy(final Function<T, F> func) {
28852902
return new Chain<Map<F, Integer>>(U.countBy(list, func));
28862903
}
28872904

2905+
public Chain<Map<T, Integer>> countBy() {
2906+
return new Chain<Map<T, Integer>>(U.countBy(list));
2907+
}
2908+
28882909
public Chain<T> shuffle() {
28892910
return new Chain<T>(U.shuffle(list));
28902911
}

src/main/java/com/github/underscore/lodash/U.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ public <F> Chain<Map<F, Integer>> countBy(final Function<T, F> func) {
269269
return new Chain<Map<F, Integer>>(U.countBy(value(), func));
270270
}
271271

272+
public Chain<Map<T, Integer>> countBy() {
273+
return new Chain<Map<T, Integer>>(U.countBy(value()));
274+
}
275+
272276
public Chain<T> shuffle() {
273277
return new Chain<T>(U.shuffle(value()));
274278
}

src/test/java/com/github/underscore/CollectionsTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,6 +1712,9 @@ public String apply(Person person) {
17121712
}
17131713
}).item();
17141714
assertEquals("{moe=2, curly=1}", resultChain.toString());
1715+
U.countBy(asList(1, 2, 3));
1716+
new U<Integer>(asList(1, 2, 3)).countBy();
1717+
U.chain(asList(1, 2, 2, 3)).countBy().item();
17151718
}
17161719

17171720
/*

src/test/java/com/github/underscore/lodash/LodashTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,7 @@ public Integer apply(Integer a, Integer b) {
843843
U.chain(new Integer[] {0}).indexBy("");
844844
U.chain(new Integer[] {0}).countBy(new Function<Integer, Integer>() {
845845
public Integer apply(Integer value) { return value; } });
846+
U.chain(new Integer[] {0}).countBy();
846847
U.chain(new Integer[] {0}).shuffle();
847848
U.chain(new Integer[] {0}).sample();
848849
U.chain(new Integer[] {0}).sample(1);
@@ -1017,4 +1018,16 @@ public void sqlru1() {
10171018

10181019
assertEquals("[memory, cpu, hdd]", U.keys((Map<String, Object>) U.fromJson(json)).toString());
10191020
}
1021+
1022+
@SuppressWarnings("unchecked")
1023+
@Test
1024+
public void sqlru2() {
1025+
System.out.println(U.countBy(U.words("Маша ищет Мишу а Миша ищет Машу"),
1026+
new Function<String, String>() {
1027+
public String apply(String item) {
1028+
return item;
1029+
}
1030+
}
1031+
).get("ищет"));
1032+
}
10201033
}

0 commit comments

Comments
 (0)