Total Pageviews

2018/08/07

[Java 8] Sorting With JDK 8

Scenario
I have a Song class, and instantiate five instances as bellows:


How-To
Here has an example to do sorting:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  package test.albert.sorting;
  
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Comparator;
  import java.util.List;
  import java.util.stream.Collectors;
  
  import lombok.extern.slf4j.Slf4j;
  
  @Slf4j
  public class SortingExample {
  
      public static void main(String[] args) {
  
          SortingExample sortingExample = new SortingExample();
  
          List<Song> songs = sortingExample.createSongs();
          sortingExample.sortedByYear(songs);
          sortingExample.sortedByArtistAlbum(songs);
          sortingExample.sortedByYearArtistAlbum(songs);
      }
  
      private List<Song> sortedByYear(List<Song> songs) {
          List<Song> sortedSongs = new ArrayList<Song>();
          sortedSongs = songs.stream().sorted(Comparator.comparingInt(Song::getReleaseYear)).collect(Collectors.toList());
  
          log.debug("sorted by rlease year");
          sortedSongs.forEach(s -> log.debug(s.toString()));
          log.debug("\n");
  
          return sortedSongs;
      }
  
      private List<Song> sortedByArtistAlbum(List<Song> songs) {
          List<Song> sortedSongs = new ArrayList<Song>();
          sortedSongs = songs.stream().sorted(Comparator.comparing(Song::getArtist).thenComparing(Song::getAlbum))
                  .collect(Collectors.toList());
          
          log.debug("sorted by artist, album");
          sortedSongs.forEach(s -> log.debug(s.toString()));
          log.debug("\n");
  
          return sortedSongs;
      }
  
      private List<Song> sortedByYearArtistAlbum(List<Song> songs) {
          List<Song> sortedSongs = new ArrayList<Song>();
          sortedSongs = songs.stream().sorted(Comparator.comparingInt(Song::getReleaseYear).thenComparing(Song::getArtist)
                  .thenComparing(Song::getAlbum)).collect(Collectors.toList());
  
          log.debug("sorted by rlease year, artist, album");
          sortedSongs.forEach(s -> log.debug(s.toString()));
          log.debug("\n");
  
          return sortedSongs;
      }
  
      private List<Song> createSongs() {
          Song jay = new Song("告白氣球", "周杰倫的床邊故事", "周杰倫", 2016);
          Song mayday = new Song("任意門", "自傳", "五月天", 2016);
          Song jamHsiao = new Song("阿飛的小蝴蝶", "王妃", "蕭敬騰", 2009);
          Song hebe = new Song("不醉不會", "渺小", "田馥甄", 2013);
          Song lala = new Song("尋人啟事", "尋人啟事", "徐佳瑩", 2015);
          return Arrays.asList(jay, mayday, jamHsiao, hebe, lala);
      }
  
  }


Sorting result:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
  sorted by rlease year 
  Song(title=阿飛的小蝴蝶, album=王妃, artist=蕭敬騰, releaseYear=2009) 
  Song(title=不醉不會, album=渺小, artist=田馥甄, releaseYear=2013) 
  Song(title=尋人啟事, album=尋人啟事, artist=徐佳瑩, releaseYear=2015) 
  Song(title=告白氣球, album=周杰倫的床邊故事, artist=周杰倫, releaseYear=2016) 
  Song(title=任意門, album=自傳, artist=五月天, releaseYear=2016) 
  
 
  sorted by artist, album 
  Song(title=任意門, album=自傳, artist=五月天, releaseYear=2016) 
  Song(title=告白氣球, album=周杰倫的床邊故事, artist=周杰倫, releaseYear=2016) 
  Song(title=尋人啟事, album=尋人啟事, artist=徐佳瑩, releaseYear=2015) 
  Song(title=不醉不會, album=渺小, artist=田馥甄, releaseYear=2013) 
  Song(title=阿飛的小蝴蝶, album=王妃, artist=蕭敬騰, releaseYear=2009) 
  
 
  sorted by rlease year, artist, album 
  Song(title=阿飛的小蝴蝶, album=王妃, artist=蕭敬騰, releaseYear=2009) 
  Song(title=不醉不會, album=渺小, artist=田馥甄, releaseYear=2013) 
  Song(title=尋人啟事, album=尋人啟事, artist=徐佳瑩, releaseYear=2015) 
  Song(title=任意門, album=自傳, artist=五月天, releaseYear=2016) 
  Song(title=告白氣球, album=周杰倫的床邊故事, artist=周杰倫, releaseYear=2016) 


No comments: