如何使用 java8 中的方法参考打印多个参数
How to print multiple parameters using Method reference in java8
我正在尝试使用 twoin java 打印基本 hashmap
。
Map<Integer, String> mp = new HashMap<Integer, String>();
mp.put(10, "apple");
mp.put(20, "orange");
mp.put(30, "banana");
但是当涉及到 java8 中的 method reference
时,我不知道如何打印多个参数。
我试过这样的东西。但它给我编译错误。
mp.forEach(System.out::println(i+" "+s););
请帮我解决这个问题。
谢谢。
可以单独写一个方法,例如:
public static <K, V> void printEntry(Map.Entry<K, V> e) {
System.out.println(e.getKey() + " " + e.getValue());
}
map.entrySet().forEach(Demo::printEntry);
或者,如果 Map.Entry<K, V>.toString()
满足您的要求:
map.entrySet().forEach(System.out::println);
// 20=orange
// 10=apple
// 30=banana
编辑: 另外,按照@Holger 的建议,您可以安全地省略类型参数,只要方法中的代码不依赖于它们:
public static void printEntry(Object k, Object v) {
System.out.println(k + " " + v);
}
map.forEach(Demo::printEntry);
你不能。该语言不允许这样做,那里没有可以通过这种方式传递给方法引用的隐式 i 和 s 。你能做什么,不知道为什么,但你可以:
private static <K, V> void consumeBoth(K k, V v) {
//Log how u want this
}
并将其用于:
map.forEach(Yourclass::consumeBoth)
但这可以用 lambda 表达式就地完成,我真的看不出这个小例子有什么好处
您不能使用方法参考来指定空格 System.out::println
。
传递给 System.out::println
的参数由 Map.forEach(BiConsumer)
的 BiConsumer
参数推断。
但是你可以用 map()
格式化预期的 String
,这样,在 System.out::println
中推断的参数将是格式化的字符串,你需要什么:
mp.entrySet()
.stream()
.map(e-> e.getKey() + " " + e.getValue())
.forEach(System.out::println);
可能与其他答案相矛盾,但我真的认为您不需要在此处使用方法参考。恕我直言,
mp.forEach((i, s) -> System.out.println(i + " " + s));
对于这样的用例来说比方法参考要好得多。
您也可以使用 entrySet 进行打印
mp.entrySet().forEach(e->System.out.println(e.getKey()+"="+e.getValue()));
我终于找到了解决方案,但是在Java 8.
中使用Apache API Pair class (ImmutablePair)
Stream.of(ImmutablePair.of("A", "1"), ImmutablePair.of("B", "0")) .collect(Collectors.toMap(Pair::getLeft, Pair::getRight));
希望对您有所帮助。
我正在尝试使用 twoin java 打印基本 hashmap
。
Map<Integer, String> mp = new HashMap<Integer, String>();
mp.put(10, "apple");
mp.put(20, "orange");
mp.put(30, "banana");
但是当涉及到 java8 中的 method reference
时,我不知道如何打印多个参数。
我试过这样的东西。但它给我编译错误。
mp.forEach(System.out::println(i+" "+s););
请帮我解决这个问题。 谢谢。
可以单独写一个方法,例如:
public static <K, V> void printEntry(Map.Entry<K, V> e) {
System.out.println(e.getKey() + " " + e.getValue());
}
map.entrySet().forEach(Demo::printEntry);
或者,如果 Map.Entry<K, V>.toString()
满足您的要求:
map.entrySet().forEach(System.out::println);
// 20=orange
// 10=apple
// 30=banana
编辑: 另外,按照@Holger 的建议,您可以安全地省略类型参数,只要方法中的代码不依赖于它们:
public static void printEntry(Object k, Object v) {
System.out.println(k + " " + v);
}
map.forEach(Demo::printEntry);
你不能。该语言不允许这样做,那里没有可以通过这种方式传递给方法引用的隐式 i 和 s 。你能做什么,不知道为什么,但你可以:
private static <K, V> void consumeBoth(K k, V v) {
//Log how u want this
}
并将其用于:
map.forEach(Yourclass::consumeBoth)
但这可以用 lambda 表达式就地完成,我真的看不出这个小例子有什么好处
您不能使用方法参考来指定空格 System.out::println
。
传递给 System.out::println
的参数由 Map.forEach(BiConsumer)
的 BiConsumer
参数推断。
但是你可以用 map()
格式化预期的 String
,这样,在 System.out::println
中推断的参数将是格式化的字符串,你需要什么:
mp.entrySet()
.stream()
.map(e-> e.getKey() + " " + e.getValue())
.forEach(System.out::println);
可能与其他答案相矛盾,但我真的认为您不需要在此处使用方法参考。恕我直言,
mp.forEach((i, s) -> System.out.println(i + " " + s));
对于这样的用例来说比方法参考要好得多。
您也可以使用 entrySet 进行打印
mp.entrySet().forEach(e->System.out.println(e.getKey()+"="+e.getValue()));
我终于找到了解决方案,但是在Java 8.
中使用Apache API Pair class (ImmutablePair)Stream.of(ImmutablePair.of("A", "1"), ImmutablePair.of("B", "0")) .collect(Collectors.toMap(Pair::getLeft, Pair::getRight));
希望对您有所帮助。