Skip to content

Commit febc204

Browse files
committed
Add support for the toMap() method with list of tuples.
1 parent 2c71164 commit febc204

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,14 @@ public <K, V> Map<K, V> toMap() {
662662
return toMap((Iterable<Map.Entry<K, V>>) iterable);
663663
}
664664

665+
public static <K, V> Map<K, V> toMap(final List<Tuple<K, V>> tuples) {
666+
final Map<K, V> result = newLinkedHashMap();
667+
for (final Tuple<K, V> entry : tuples) {
668+
result.put(entry.fst(), entry.snd());
669+
}
670+
return result;
671+
}
672+
665673
public static int size(final Iterable<?> iterable) {
666674
if (iterable instanceof Collection) {
667675
return ((Collection) iterable).size();

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,10 @@ public void toMap() {
12941294
put("name1", "one");
12951295
put("name2", "two");
12961296
} }).entrySet()).toMap().toString());
1297+
assertEquals("{name1=one, name2=two}", $.toMap(new ArrayList<Tuple<String, String>>() { {
1298+
add(Tuple.create("name1", "one"));
1299+
add(Tuple.create("name2", "two"));
1300+
} }).toString());
12971301
}
12981302

12991303
/*

0 commit comments

Comments
 (0)