-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinder.java
More file actions
46 lines (39 loc) · 1.39 KB
/
Finder.java
File metadata and controls
46 lines (39 loc) · 1.39 KB
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
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class Finder {
private SongLibrary songlib;
private List<Song> queryList;
public Finder(SongLibrary sl){
this.songlib = sl;
this.queryList = new ArrayList<>();
}
//1 - find the longest song of the playlist
public Optional<Song> longestSongFinder(){
if(!(songlib.isEmpty())){
return Optional.of(songlib.getMyLibrary().stream().max(Comparator.comparing(Song::getSecondsDuration)).get());
}
return Optional.empty();
}
//2 - find all the songs by X authors, if present
public List<Song> songsByWhom(String author){
clearQuery();
if(songlib.getMyLibrary().stream().anyMatch(x->x.getAuthor() == author)){
this.queryList = songlib.getMyLibrary().stream().filter(x->x.getAuthor() == author).collect(Collectors.toList());
}
return this.queryList;
}
//3 - find and print all authors names
public List<String> songAuthors(){
return songlib.getMyLibrary().stream().map(x->x.getAuthor()).distinct().collect(Collectors.toList());
}
public void clearQuery(){
if(!(queryList.isEmpty()))
this.queryList.clear();
}
public void changeLibrary(SongLibrary sl){
this.songlib = sl;
}
}