Printing HashMap In Java

后端 未结 15 1729
半阙折子戏
半阙折子戏 2020-11-28 19:32

I have a HashMap:

private HashMap example = new HashMap();

Now I would lik

15条回答
  •  无人及你
    2020-11-28 19:53

    Worth mentioning Java 8 approach, using BiConsumer and lambda functions:

    BiConsumer consumer = (o1, o2) -> 
               System.out.println(o1 + ", " + o2);
    
    example.forEach(consumer);
    

    Assuming that you've overridden toString method of the two types if needed.

提交回复
热议问题