Skip to content

Instantly share code, notes, and snippets.

@Shivamy2
Created April 10, 2021 09:22
Show Gist options
  • Save Shivamy2/76ead3729ab1d14d8e958a28578f3897 to your computer and use it in GitHub Desktop.
Save Shivamy2/76ead3729ab1d14d8e958a28578f3897 to your computer and use it in GitHub Desktop.
List Of Videos
import java.math.BigInteger;
import java.util.*;
class YoutubeVideo {
private final long videoId;
private String name;
private BigInteger numberOfLikes;
private int duration;
public long getVideoId() {
return videoId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BigInteger getNumberOfLikes() {
return numberOfLikes;
}
public void setNumberOfLikes(BigInteger numberOfLikes) {
this.numberOfLikes = numberOfLikes;
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
public YoutubeVideo(long videoId, String name, BigInteger numberOfLikes, int duration) {
this.videoId = videoId;
this.name = name;
this.numberOfLikes = numberOfLikes;
this.duration = duration;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
YoutubeVideo that = (YoutubeVideo) o;
return videoId == that.videoId && duration == that.duration && Objects.equals(name, that.name) && Objects.equals(numberOfLikes, that.numberOfLikes);
}
@Override
public int hashCode() {
return Objects.hash(videoId, name, numberOfLikes, duration);
}
@Override
public String toString() {
return "YoutubeVideo{" +
"videoId=" + videoId +
", name='" + name + '\'' +
", numberOfLikes=" + numberOfLikes +
", duration=" + duration +
'}';
}
}
class VideoAnalytics {
public static Set<YoutubeVideo> getTopTenViralVideo(List<YoutubeVideo> videos) {
Set<YoutubeVideo> hashSet = new HashSet<>();
int j=0;
// videos.stream().forEach(items -> {
// j==3 ? : hashSet.add(items);
// });
for (int i = 0; i < 5; i++) {
hashSet.add(videos.get(i));
}
return hashSet;
}
public static List<String> showDetailsOfShortVideos(Set<YoutubeVideo> videos) {
List<String> list = new ArrayList<>();
videos.stream().forEach(items -> {
if(items.getDuration() <= 10) {
list.add(items.getName() + " - " + items.getNumberOfLikes() + " - " + items.getDuration());
}
});
return list;
}
}
class Main {
public static void main(String[] args) {
List<YoutubeVideo> listOfVideos = new ArrayList<>();
// BigInteger value1 = new BigInteger(String.valueOf(1200));
listOfVideos.addAll(
List.of(
new YoutubeVideo(1, "A", BigInteger.valueOf(1200), 5),
new YoutubeVideo(2, "B", BigInteger.valueOf(1200), 4),
new YoutubeVideo(3, "C", BigInteger.valueOf(1000), 6),
new YoutubeVideo(4, "D", BigInteger.valueOf(900000000), 3),
new YoutubeVideo(5, "E", BigInteger.valueOf(400), 2),
new YoutubeVideo(6, "F", BigInteger.valueOf(400), 1),
new YoutubeVideo(7, "G", BigInteger.valueOf(1400), 6),
new YoutubeVideo(8, "H", BigInteger.valueOf(1400), 20),
new YoutubeVideo(9, "I", BigInteger.valueOf(34400), 4),
new YoutubeVideo(10, "J", BigInteger.valueOf(1400), 2)
));
// listOfVideos.sort(new Comparator<YoutubeVideo>() {
// @Override
// public int compare(YoutubeVideo o1, YoutubeVideo o2) {
// return o2.getNumberOfLikes().compareTo(o1.getNumberOfLikes());
// }
// });
listOfVideos.sort(new Comparator<YoutubeVideo>() {
@Override
public int compare(YoutubeVideo o1, YoutubeVideo o2) {
if(o1.getNumberOfLikes().equals(o2.getNumberOfLikes())) {
return o2.getDuration()-o1.getDuration();
}
return o2.getNumberOfLikes().compareTo(o1.getNumberOfLikes());
}
});
System.out.println("\nList Of Porn Videos: \n");
listOfVideos.stream().forEach(System.out::println);
// for (var items :
// listOfVideos) {
// System.out.println(items.hashCode() % 15 + " " + items);
// }
System.out.println("\n\nValues of HashSet: \n");
Set<YoutubeVideo> hashSet = new HashSet<>();
hashSet = VideoAnalytics.getTopTenViralVideo(listOfVideos);
hashSet.stream().forEach(System.out::println);
System.out.println("\n\nValues of List<String>: \n");
VideoAnalytics.showDetailsOfShortVideos(hashSet).forEach(System.out::println);
// for (var items :
// VideoAnalytics.getTopTenViralVideo(listOfVideos)) {
// System.out.println(items.hashCode() % 15 + " " + items);
// }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment