ex3
This commit is contained in:
parent
b1f3bdefb2
commit
0eb4a53b68
13 changed files with 206 additions and 84 deletions
8
hw2/.idea/.gitignore
vendored
Normal file
8
hw2/.idea/.gitignore
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
9
hw2/.idea/hw2.iml
Normal file
9
hw2/.idea/hw2.iml
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
8
hw2/.idea/modules.xml
Normal file
8
hw2/.idea/modules.xml
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/hw2.iml" filepath="$PROJECT_DIR$/.idea/hw2.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
6
hw2/.idea/vcs.xml
Normal file
6
hw2/.idea/vcs.xml
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
13
hw3/code/Assignment3.iml
Normal file
13
hw3/code/Assignment3.iml
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_17">
|
||||
<output url="file://$MODULE_DIR$/target/classes" />
|
||||
<output-test url="file://$MODULE_DIR$/target/test-classes" />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
|
@ -3,6 +3,7 @@ package ch.usi.inf.ajp22;
|
|||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Album {
|
||||
|
||||
|
@ -44,19 +45,30 @@ public class Album {
|
|||
|
||||
/**
|
||||
* 3 Points
|
||||
* TODO: create a public Optional<Track> method called "searchLongestSong" that
|
||||
* return the longest Track in the album.
|
||||
* create a public Optional<Track> method called "searchLongestSong" that
|
||||
* return the longest Track on the album.
|
||||
* input: void
|
||||
* output: an Optional<Track>
|
||||
*/
|
||||
public Optional<Track> searchLongestSong() {
|
||||
return this.tracks.stream()
|
||||
.max(Comparator.comparingInt(Track::getLength));
|
||||
}
|
||||
|
||||
/**
|
||||
* 3 Points
|
||||
* TODO: create a public List<Track> method called "orderSongByTitle" that
|
||||
* create a public List<Track> method called "orderSongByTitle" that
|
||||
* return the track list ordered lexicographically using the title field.
|
||||
* input: void
|
||||
* output: a List<Track>
|
||||
*/
|
||||
public List<Track> orderSongByTitle() {
|
||||
// note: we assume lexicographical order requires letter case to be ignored
|
||||
return this.tracks.stream()
|
||||
.sorted((a, b) -> String.CASE_INSENSITIVE_ORDER.compare(a.getTitle(), b.getTitle()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
|
|
@ -5,107 +5,154 @@ import java.io.FileWriter;
|
|||
import java.io.IOException;
|
||||
import java.time.LocalDate;
|
||||
import java.util.*;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.BinaryOperator;
|
||||
import java.util.function.IntFunction;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class Main {
|
||||
|
||||
/**
|
||||
* 3 Points
|
||||
* TODO: create a public static Album method called "longestTitle".
|
||||
* create a public static Album method called "longestTitle".
|
||||
* If the maximum is not found throw a RuntimeException.
|
||||
* input: a list of Album
|
||||
* output: the Album with the longest title.
|
||||
*/
|
||||
|
||||
public static Album longestTitle(List<? extends Album> albums) {
|
||||
return albums.stream()
|
||||
.max(Comparator.comparingDouble(a -> a.getTitle().length()))
|
||||
.orElseThrow(RuntimeException::new);
|
||||
}
|
||||
|
||||
/**
|
||||
* 4 Points
|
||||
* TODO: create a public static int method called "sumOfRatingReduce".
|
||||
* create a public static int method called "sumOfRatingReduce".
|
||||
* input: an Album instance.
|
||||
* output: int that is the sum of all the tracks ratings in the album.
|
||||
* output: int that is the sum of all the tracks ratings on the album.
|
||||
* Use the method getTracks from the Album class to get the List of tracks.
|
||||
* You MUST use the reduce method, in the variant which takes as input an identity, an accumulator and a combiner.
|
||||
* If possible use method references.
|
||||
*/
|
||||
public static int sumOfRatingReduce(Album album) {
|
||||
return album.getTracks().stream()
|
||||
.map(Track::getLength)
|
||||
.reduce(0, Integer::sum, Integer::sum);
|
||||
}
|
||||
|
||||
/**
|
||||
* 4 Points
|
||||
* TODO: create a public static int method called "sumOfRatingCollection".
|
||||
* create a public static int method called "sumOfRatingCollection".
|
||||
* input: an Album instance.
|
||||
* output: int that is the sum of all the tracks ratings in the album
|
||||
* output: int that is the sum of all the tracks ratings on the album
|
||||
* Use the method getTracks from the Album class to get the List of tracks.
|
||||
* You MUST use one variant of the collect method. If possible use method references.
|
||||
*/
|
||||
public static int sumOfRatingCollection(Album album) {
|
||||
//noinspection SimplifyStreamApiCallChains
|
||||
return album.getTracks().stream().map(Track::getRating)
|
||||
.collect(Collectors.reducing(0, Integer::sum));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 4 Points
|
||||
* TODO: create a public static int method called "sumOfRatingMap"
|
||||
* input: an ch.usi.inf.ajp22.Album instance.
|
||||
* output: int that is the sum of all the tracks ratings in the album.
|
||||
* create a public static int method called "sumOfRatingMap"
|
||||
* input: a ch.usi.inf.ajp22.Album instance.
|
||||
* output: int that is the sum of all the tracks ratings on the album.
|
||||
* Use the method getTracks from the Album class to get the List of tracks.
|
||||
* You MUST use the mapToInt method. If possible use method references.
|
||||
*/
|
||||
public static int sumOfRatingMap(Album album) {
|
||||
return album.getTracks().stream().mapToInt(Track::getRating).sum();
|
||||
}
|
||||
|
||||
/**
|
||||
* 4 Points
|
||||
* TODO: create a public static Map<String, Long> method called "countTrackOccurrence"
|
||||
* create a public static Map<String, Long> method called "countTrackOccurrence"
|
||||
* input: a list of Track
|
||||
* output: a Map<String, Long> where the key is the track title and the value is, how many times
|
||||
* the same song occurs in the list.
|
||||
* Two songs can be considered the same if they have the same title.
|
||||
*/
|
||||
public static Map<String, Long> countTrackOccurrence(List<? extends Track> tracks) {
|
||||
return tracks.stream().collect(Collectors.groupingBy(Track::getTitle, Collectors.counting()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 4 Points
|
||||
* TODO: create a public static Map<Artist, List<Album>> method called "groupAlbumByArtist".
|
||||
* create a public static Map<Artist, List<Album>> method called "groupAlbumByArtist".
|
||||
* input: a list of Album
|
||||
* output: a Map<Artist, List<Album>> where the key is an Artist and the value is a List of Album which
|
||||
* this artist had produced.
|
||||
*/
|
||||
public static Map<Artist, List<Album>> groupAlbumByArtist(List<? extends Album> albums) {
|
||||
return albums.stream().collect(Collectors.groupingBy(Album::getArtist, Collectors.toList()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 4 Points
|
||||
* TODO: create a public static List<Track> method called "trackFilteredWithPredicate".
|
||||
* create a public static List<Track> method called "trackFilteredWithPredicate".
|
||||
* input: a stream of tracks and a predicate to apply to the track stream.
|
||||
* output: a List of Track that has been filtered according to the predicate taken as input.
|
||||
*/
|
||||
public static List<Track> trackFilteredWithPredicate(Stream<? extends Track> tracks, Predicate<? super Track> predicate) {
|
||||
return tracks.filter(predicate).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 5 Points
|
||||
* TODO: create a public static Stream<Album> method called getAlbumWithAtLeastNTracks
|
||||
* create a public static Stream<Album> method called getAlbumWithAtLeastNTracks
|
||||
* input: a list of Album AND an int called "n"
|
||||
* output: a List<Album> where every album in this stream has at least "n" tracks.
|
||||
*/
|
||||
public static Stream<Album> getAlbumWithAtLeastNTracks(List<Album> albums, int n) {
|
||||
return albums.stream().filter(a -> a.getTracks().size() >= n);
|
||||
}
|
||||
|
||||
/**
|
||||
* 5 Points
|
||||
* TODO: create a public static void method called "printTrackStatistics".
|
||||
* create a public static void method called "printTrackStatistics".
|
||||
* input: an Album instance
|
||||
* Print Track Statistics based on the length of the tracks of an album taken as input in the following format:
|
||||
* ===
|
||||
* "Stat for: %s\n", album.getTitle())
|
||||
* "Stat for: %s\n", album.getTitle()
|
||||
* "Max: %d\nMin: %d\nAve: %f\n"
|
||||
* ===
|
||||
* Use the method getTracks from the Album class to get the List of tracks.
|
||||
* Input: an Album
|
||||
*/
|
||||
public static void printTrackStatistics(Album album) {
|
||||
final Supplier<IntStream> tracksStreamSupplier = () -> album.getTracks().stream().mapToInt(Track::getLength);
|
||||
final Supplier<IllegalArgumentException> whenZero = () -> {
|
||||
throw new IllegalArgumentException("Album has no tracks");
|
||||
};
|
||||
|
||||
System.out.printf("===\nStat for: %s\nMax: %d\nMin: %d\nAve: %f\n===\n",
|
||||
album.getTitle(),
|
||||
tracksStreamSupplier.get().max().orElseThrow(whenZero),
|
||||
tracksStreamSupplier.get().min().orElseThrow(whenZero),
|
||||
tracksStreamSupplier.get().average().orElseThrow(whenZero));
|
||||
}
|
||||
|
||||
/**
|
||||
* 5 Points
|
||||
* TODO: create a public static String method called "getArtistNameAndNickNameFromAlbum".
|
||||
* create a public static String method called "getArtistNameAndNickNameFromAlbum".
|
||||
* Each author should appear only once.
|
||||
* input: a list of Album
|
||||
* output: a String in the following format:
|
||||
* [artist1.name - artist1.nickname, artist2.name - artist2.nickname, ...]
|
||||
*/
|
||||
public static String getArtistNameAndNickNameFromAlbum(List<? extends Album> albums) {
|
||||
return albums.stream().map(Album::getArtist).distinct()
|
||||
.map(a -> String.format("%s - %s", a.getName(), a.getNickname()))
|
||||
.collect(Collectors.joining(",", "[", "]"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 5 Points
|
||||
* TODO: create a public Track method called "combineAllTrackInAlbum"
|
||||
* create a public Track method called "combineAllTrackInAlbum"
|
||||
* input: a stream of track
|
||||
* output: return a new ch.usi.inf.ajp22.Track with the following values:
|
||||
* title -> "fake title"
|
||||
|
@ -116,25 +163,39 @@ public class Main {
|
|||
* You MUST use the collect method, in the variant which takes as input a supplier, an accumulator and a
|
||||
* combiner.
|
||||
*/
|
||||
public static Track combineAllTrackInAlbum(Stream<? extends Track> tracks) {
|
||||
return tracks.parallel().collect(
|
||||
() -> new Track("fake title", LocalDate.now(), Track.Genre.DISCO, 0),
|
||||
(acc, track) -> acc.setLength(acc.getLength() + track.getLength()),
|
||||
(acc, other) -> acc.setLength(acc.getLength() + other.getLength()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 5 Points
|
||||
* TODO: create a public static Map<Artist, List<Track>> method called "groupTrackByArtist".
|
||||
* create a public static Map<Artist, List<Track>> method called "groupTrackByArtist".
|
||||
* input: a list of Album
|
||||
* output: a Map<Artist, List<Track>> where the key is an Artist and the value is a List of Track which
|
||||
* this artist had produced.
|
||||
*/
|
||||
public static Map<Artist, List<Track>> groupTrackByArtist(List<? extends Album> albums) {
|
||||
return albums.stream().collect(
|
||||
Collectors.groupingBy(
|
||||
Album::getArtist,
|
||||
Collectors.flatMapping(a -> a.getTracks().stream(), Collectors.toList())));
|
||||
}
|
||||
|
||||
/**
|
||||
* 5 Bonus Points
|
||||
* TODO: create a public static <T> T[] method called "createArray".
|
||||
* create a public static <T> T[] method called "createArray".
|
||||
* This method must create a generic array where the elements are in the same order as in the
|
||||
* input Collection.
|
||||
* In this function you need neither streams nor lambda.
|
||||
* input: a Collection<T> and an IntFunction<T[]>
|
||||
* output: an array of generic type
|
||||
*/
|
||||
|
||||
public static <T> T[] createArray(Collection<T> collection, IntFunction<T[]> generator) {
|
||||
return collection.toArray(generator);
|
||||
}
|
||||
|
||||
private static void writeToFile(String s) throws IOException {
|
||||
BufferedWriter fw = new BufferedWriter(new FileWriter("artist.txt", true));
|
||||
|
@ -145,7 +206,7 @@ public class Main {
|
|||
public static void main(String[] args) {
|
||||
Random random = new Random();
|
||||
|
||||
/**
|
||||
/*
|
||||
* Initializing some SampleData
|
||||
*/
|
||||
SampleData.appetiteForDestruction
|
||||
|
@ -156,35 +217,40 @@ public class Main {
|
|||
.getTracks().forEach(track ->
|
||||
track.setRating(random.nextInt(5)));
|
||||
|
||||
/**
|
||||
/*
|
||||
* 2 Points
|
||||
* TODO: Replace the Anonymous class below with a lambda expression.
|
||||
* Replace the Anonymous class below with a lambda expression.
|
||||
*/
|
||||
BinaryOperator<Track> mixTrack = new BinaryOperator<>() {
|
||||
@Override
|
||||
public Track apply(Track t1, Track t2) {
|
||||
return new Track(t1.getTitle(),
|
||||
BinaryOperator<Track> mixTrack = (t1, t2) -> new Track(t1.getTitle(),
|
||||
t1.getReleasedDate(),
|
||||
t2.getGenre(),
|
||||
t2.getLength());
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
/*
|
||||
* 3 Points
|
||||
* TODO: Create a stream pipeline that:
|
||||
* Create a stream pipeline that:
|
||||
* 1) filter all the tracks that have at least 4 as rating
|
||||
* 2) sort the filtered tracks using the title field
|
||||
* 3) print the tracks
|
||||
*/
|
||||
SampleData.getShuffledTrack().stream().filter(t -> t.getRating() >= 4)
|
||||
.sorted(Comparator.comparing(Track::getTitle))
|
||||
.forEach(System.out::println);
|
||||
|
||||
/**
|
||||
/*
|
||||
* 4 Points
|
||||
* TODO: Print on file the artist's name, obtained with the method SampleData.getArtistList(),
|
||||
* Print on file the artist's name, obtained with the method SampleData.getArtistList(),
|
||||
* using the writeToFile method. Use a lambda expression and handle
|
||||
* the throw exception from the writeToFile method. If there is an IOException you must throw a
|
||||
* RuntimeException with the same event.
|
||||
*/
|
||||
SampleData.getArtistList().stream().map(Artist::getName).forEach(n -> {
|
||||
try {
|
||||
writeToFile(n);
|
||||
} catch (final IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
|
||||
System.out.printf("Longest track in %s is %s\n",
|
||||
SampleData.appetiteForDestruction.getTitle(),
|
||||
|
|
BIN
hw3/code/target/classes/ch/usi/inf/ajp22/Album.class
Normal file
BIN
hw3/code/target/classes/ch/usi/inf/ajp22/Album.class
Normal file
Binary file not shown.
BIN
hw3/code/target/classes/ch/usi/inf/ajp22/Artist.class
Normal file
BIN
hw3/code/target/classes/ch/usi/inf/ajp22/Artist.class
Normal file
Binary file not shown.
BIN
hw3/code/target/classes/ch/usi/inf/ajp22/Main.class
Normal file
BIN
hw3/code/target/classes/ch/usi/inf/ajp22/Main.class
Normal file
Binary file not shown.
BIN
hw3/code/target/classes/ch/usi/inf/ajp22/SampleData.class
Normal file
BIN
hw3/code/target/classes/ch/usi/inf/ajp22/SampleData.class
Normal file
Binary file not shown.
BIN
hw3/code/target/classes/ch/usi/inf/ajp22/Track$Genre.class
Normal file
BIN
hw3/code/target/classes/ch/usi/inf/ajp22/Track$Genre.class
Normal file
Binary file not shown.
BIN
hw3/code/target/classes/ch/usi/inf/ajp22/Track.class
Normal file
BIN
hw3/code/target/classes/ch/usi/inf/ajp22/Track.class
Normal file
Binary file not shown.
Reference in a new issue