如果你想要一些闭包式的东西,那么它可能会有一个函数签名,比如:
interface KeyPairFunctor<T, K, V> {
Pair<K, V> getKeyValuePair(T item);
}
public static <T, K, V> Map<K, V> toMap(Iterable<T> items, KeyPairFunctor<T, K, V> functor) {
Map<K, V> m = new HashMap<K,V>();
for (T item : items) {
Pair<K, V> e = functor.getKeyValuePair(item);
m.put(e.getKey(), e.getValue());
}
return m;
}
public static void test() {
toMap(Arrays.asList(new String[] { "A:B" }), new KeyPairFunctor<String, String, String>() {
@Override
public Pair<String, String> getKeyValuePair(String item) {
String[] ss = item.split(":");
return new Pair(ss[0], ss[1]);
}});
}
Pair 的实现留给读者作为练习,我相信您可以看到如何将您的 , 拆分列表放在第一个参数中。