Java 8 Stream — Sorting Of Collection

Kablumndl
2 min readMay 24, 2021

Create List Of Data

List Of Data

List<BookDto> bookList = new ArrayList<>();bookList.add(new BookDto(“Way to success”, 1954, “0395489318”));bookList.add(new BookDto(“The Gandhi”, 1953, “0345339711”));bookList.add(new BookDto(“The Lion King”, 1955, “0618129111”));

Year As Key and Book Name As A Value

Map<Integer, String> mapByKeyAsYearAndValueAsName = bookList.stream().collect(Collectors.toMap(BookDto::getYear, BookDto::getName));System.out.println(“==Map By Key As Year And Value As Name==”);System.out.println(mapByKeyAsYearAndValueAsName);System.out.println(“==Map By Key As Year And Value As Name==”);

Year As Key and Book DTO As A Value

Map<Integer, BookDto> yeasAsKeyDtoAsAValue = bookList.stream().collect(Collectors.toMap(BookDto::getYear, Function.identity()));System.out.println(“=======”);System.out.println(yeasAsKeyDtoAsAValue);System.out.println(“========”);

Create New List Of Data

List<MovieDto> movieList = new ArrayList<>();movieList.add(new MovieDto(“Phool Aur Kaante”, 1990));movieList.add(new MovieDto(“Love Doze”, 1998));movieList.add(new MovieDto(“Dillagi”, 2000));movieList.add(new MovieDto(“Zigar”, 2001));

Convert Map With Duplicate Key ( First Key Persists)

System.out.println(“===Convert Map With Duplicate Key===”);Map<Integer, MovieDto> mapWithDuplicateKey = movieList.stream().collect(Collectors.toMap(MovieDto::getYear, Function.identity(), (existing, replacement) -> existing));System.out.println(mapWithDuplicateKey);System.out.println(“==Convert Map With Duplicate Key==”);

Concurrent Map

System.out.println(“==ConcurrentMap==”);ConcurrentHashMap<Integer, BookDto> concurrentMap = bookList.stream().collect(Collectors.toMap(BookDto::getYear, Function.identity(), (o1, o2) -> o1, ConcurrentHashMap::new));System.out.println(concurrentMap);System.out.println(“==ConcurrentMap==”);System.out.println(“==SortKey==”);

Sort Key Of HashMap

TreeMap<Integer, MovieDto> collect5 = movieList.stream().collect(Collectors.toMap(MovieDto::getYear, Function.identity(), (o1,o2) -> o1, TreeMap::new));System.out.println(collect5);System.out.println(“==SortKey==”);System.out.println(“==Sort Values==”);LinkedHashMap<Integer, String> collect6 = movieList.stream().collect(Collectors.toMap(MovieDto::getYear, MovieDto::getName)).entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.naturalOrder())).collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new));System.out.println(collect6);System.out.println(“==Sort Values End==”);System.out.println(“==Sort Values Using ForEach Ordred==”);Map<Integer, String> map = new HashMap<Integer, String>();movieList.stream().collect(Collectors.toMap(MovieDto::getYear, MovieDto::getName)).entrySet().stream().sorted(Map.Entry.comparingByValue()).forEachOrdered( x-> map.put(x.getKey(), x.getValue()));System.out.println(“map:” + map);System.out.println(“==Sort Values Using ForEach Ordred==”);Map<String, Integer> jobs = new HashMap<>();jobs.put(“Java”, 100);jobs.put(“C”, 200);jobs.put(“Node”, 50);jobs.put(“PHP”, 70);jobs.put(“AWS”, 200);LinkedHashMap<String, Integer> collect = jobs.entrySet().stream().sorted((a, b) -> a.getValue().compareTo(b.getValue())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (x, y) -> x, LinkedHashMap::new));System.out.println();System.out.println(“==Multiple Sort Of List==”);Collections.sort(movieList, new MovieNameComparator().thenComparing(new MovieYearComparator()));System.out.println(movieList);

System.out.println(“==END==”);

--

--

Kablumndl

Java Developer, Software Engineer, Spring, Spark, MicroService, PostgresSQL