October 22, 2024
Chicago 12, Melborne City, USA
java

Java Stream Convert HashMap into ArrayList which joins both Key and % Value using String.format


How to do this:

int size = ...
var a = new ArrayList<String>();
for (Map.Entry<String,Integer> e : myHashMap) {      
  a.add( String.format("%s %.3f", e.getKey(), 100.0 * e.getValue() / size));
}

:using Stream?

int size = ...
var a = myHashMap.entrySet()     
  .stream()
  .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
  .map(Map.Entry::getValue)
  .toList();

How to both getValue and getKey and combine them using String.format ?



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video