Преобразуйте HashMap в List, который объединяет ключ и значение с помощью String.format. ⇐ JAVA

Программисты JAVA общаются здесь
Anonymous
Преобразуйте HashMap в List, который объединяет ключ и значение с помощью String.format.

Сообщение Anonymous »

Как это сделать:

Код: Выделить всё

int size = ...
var a = new ArrayList();
for (Map.Entry e : myHashMap) {
a.add( String.format("%s %.3f", e.getKey(), 100.0 * e.getValue() / size));
}
используя Stream?

Код: Выделить всё

int size = ...
var a = myHashMap.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.map(Map.Entry::getValue)
.toList();
Как получить getValue и getKey и объединить их с помощью String.format ?

Подробнее здесь: https://stackoverflow.com/questions/791 ... sing-strin

Вернуться в «JAVA»