Consider above Example with Notes object and TestNotes Main class.
public class TestNotes {
public static void main(String[] args) {
List<Notes> noteLst = new ArrayList<>();
noteLst.add(new Notes(1, "note1", 11));
noteLst.add(new Notes(2, "note2", 22));
noteLst.add(new Notes(3, "note3", 33));
noteLst.add(new Notes(4, "note4", 44));
noteLst.add(new Notes(5, "note5", 55));
noteLst.add(new Notes(6, "note4", 66));
Map<String, Long> notesRecords = noteLst.stream()
.sorted(Comparator.comparingLong(Notes::getTagId).reversed())
.collect(
Collectors.toMap(Notes::getTagName, Notes::getTagId,
(oldValue, newValue) -> oldValue,
LinkedHashMap::new
)
);
System.out.println("Notes : " + notesRecords);
}
}
Notes : {note5=55, note4=44, note3=33, note2=22, note1=11}