0 votes
in JAVA by
How to convert a List of objects into a Map by considering duplicated keys and store them in sorted order?

1 Answer

0 votes
by

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()) // sorting is based on TagId 55,44,33,22,11
		.collect(
                Collectors.toMap(Notes::getTagName, Notes::getTagId,
                        (oldValue, newValue) -> oldValue, // consider old value 44 for dupilcate key
						LinkedHashMap::new           // it keeps order           
                )
        );

        System.out.println("Notes : " + notesRecords);
    }
}
//Output - for (oldValue, newValue) -> oldValue, it took old value note4=44
Notes : {note5=55, note4=44, note3=33, note2=22, note1=11}
...