Skip to content

Commit 724e5d4

Browse files
committed
Improve map iteration into values, mapObject pairs and invert methods.
1 parent b0ac302 commit 724e5d4

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,37 +1245,33 @@ public static <K, V> Set<K> keys(final Map<K, V> object) {
12451245
return object.keySet();
12461246
}
12471247

1248-
public static <K, V> List<V> values(final Map<K, V> object) {
1249-
final List<V> result = newArrayList();
1250-
for (final Map.Entry<K, V> entry : object.entrySet()) {
1251-
result.add(entry.getValue());
1252-
}
1253-
return result;
1248+
public static <K, V> Collection<V> values(final Map<K, V> object) {
1249+
return object.values();
12541250
}
12551251

12561252
public static <K, V> List<Tuple<K, V>> mapObject(final Map<K, V> object, final Function1<? super V, V> func) {
1257-
return map(newArrayList(object.keySet()), new Function1<K, Tuple<K, V>>() {
1253+
return map(newArrayList(object.entrySet()), new Function1<Map.Entry<K, V>, Tuple<K, V>>() {
12581254
@Override
1259-
public Tuple<K, V> apply(K key) {
1260-
return Tuple.create(key, func.apply(object.get(key)));
1255+
public Tuple<K, V> apply(Map.Entry<K, V> entry) {
1256+
return Tuple.create(entry.getKey(), func.apply(entry.getValue()));
12611257
}
12621258
});
12631259
}
12641260

12651261
public static <K, V> List<Tuple<K, V>> pairs(final Map<K, V> object) {
1266-
return map(newArrayList(object.keySet()), new Function1<K, Tuple<K, V>>() {
1262+
return map(newArrayList(object.entrySet()), new Function1<Map.Entry<K, V>, Tuple<K, V>>() {
12671263
@Override
1268-
public Tuple<K, V> apply(K key) {
1269-
return Tuple.create(key, object.get(key));
1264+
public Tuple<K, V> apply(Map.Entry<K, V> entry) {
1265+
return Tuple.create(entry.getKey(), entry.getValue());
12701266
}
12711267
});
12721268
}
12731269

12741270
public static <K, V> List<Tuple<V, K>> invert(final Map<K, V> object) {
1275-
return map(newArrayList(object.keySet()), new Function1<K, Tuple<V, K>>() {
1271+
return map(newArrayList(object.entrySet()), new Function1<Map.Entry<K, V>, Tuple<V, K>>() {
12761272
@Override
1277-
public Tuple<V, K> apply(K key) {
1278-
return Tuple.create(object.get(key), key);
1273+
public Tuple<V, K> apply(Map.Entry<K, V> entry) {
1274+
return Tuple.create(entry.getValue(), entry.getKey());
12791275
}
12801276
});
12811277
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void keys() {
5555
*/
5656
@Test
5757
public void values() {
58-
List<Integer> result = $.values(new LinkedHashMap<String, Integer>() { {
58+
Collection<Integer> result = $.values(new LinkedHashMap<String, Integer>() { {
5959
put("one", 1); put("two", 2); put("three", 3); } });
6060
assertEquals("[1, 2, 3]", result.toString());
6161
}

0 commit comments

Comments
 (0)