Answer the question
In order to leave comments, you need to log in
How to display keys in Java Map in a different way?
Have a collection
Map<String, String> map = new HashMap<>();
map.put("Sim", "Sim");
map.put("Tom", "Tom");
for(String keys: map.keySet()){
System.out.println(keys);
}
map.forEach((k, v) -> System.out.println("Key: " + k + " Value: " + v));
map.forEach((k) -> System.out.println(k));
error: incompatible types: incompatible parameter types in lambda expression
map.forEach((k) -> System.out.println(k));
^
Answer the question
In order to leave comments, you need to log in
This is what happens when you don't understand the meaning of the code you copy.
(k, v) -> ...
it's a lambda, a syntactic sugar for implementing functional interfaces (interfaces that have one abstract method).
Map::foreach just accepts such an interface
BiConsumer<? super K,? super V>
с
методом
void accept(T t, U u)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question