Skip to content

Instantly share code, notes, and snippets.

@kuroidoruido
Last active Mar 14, 2018
Embed
What would you like to do?
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import model.Character;
import static model.Character.Gender.*;
public class App {
private static List<Character> characters = Arrays.asList(new Character[] {
new Character("First Doctor", 1963, "William Hartnell", MALE, "classic"),
new Character("Second Doctor", 1966, "Patrick Troughton", MALE, "classic"),
new Character("Third Doctor", 1970, "Jon Pertwee", MALE, "classic"),
new Character("Fourth Doctor", 1974, "Tom Baker", MALE, "classic"),
new Character("Fifth Doctor", 1981, "Peter Davison", MALE, "classic"),
new Character("Sixth Doctor", 1984, "Colin Baker", MALE, "classic"),
new Character("Seventh Doctor", 1987, "Sylvester McCoy", MALE, "classic"),
new Character("Eighth Doctor", 1996, "Paul McGann", MALE, "TV film"),
new Character("War Doctor", 2013, "John Hurt", MALE, "2005 show"),
new Character("Ninth Doctor", 2005, "Christopher Eccleston", MALE, "2005 show"),
new Character("Tenth Doctor", 2005, "David Tennant", MALE, "2005 show"),
new Character("Eleventh Doctor", 2010, "Matt Smith", MALE, "2005 show"),
new Character("Twelfth Doctor", 2013, "Peter Capaldi", MALE, "2005 show"),
new Character("Thirteenth Doctor", 2017, "Jodie Whittaker", FEMALE, "2005 show"),
new Character("River Song", 2008, "Alex Kingston", FEMALE, "2005 show"),
new Character("Jack Harkness", 2005, "John Barrowman", MALE, "2005 show"),
new Character("Rose Tyler", 2005, "Billie Piper", FEMALE, "2005 show"),
new Character("Martha Jones", 2007, "Freema Agyeman", FEMALE, "2005 show"),
new Character("Donna Noble", 2006, "Catherine Tate", FEMALE, "2005 show"),
new Character("Amelia « Amy » Pond", 2010, "Karen Gillan", FEMALE, "2005 show"),
new Character("Clara Oswald", 2010, "Jenna Coleman", FEMALE, "2005 show"),
new Character("Nardole", 2015, "Matt Lucas", MALE, "2005 show"),
new Character("Bill Potts", 2017, "Pearl Mackie", FEMALE, "2005 show")
});
public static void main(String[] args) {
//printAll()
//sortAndPrint();
//filterAndPrint();
filterSortAndPrint();
}
private static void printAll() {
System.out.println("--- Java 7");
for(Character c : characters) {
System.out.println(c);
}
System.out.println("--- Java 8");
characters.forEach(new Consumer<Character>() {
public void accept(Character c) {
System.out.println(c);
}
});
System.out.println("--- Java 8 lambda");
characters.forEach(c -> System.out.println(c));
System.out.println("--- Java 8 method reference");
characters.forEach(System.out::println);
}
private static void sortAndPrint() {
System.out.println("--- Java 7");
System.out.println("------ Alphabetical Order");
Collections.sort(characters,new Comparator<Character>() {
@Override
public int compare(Character c1, Character c2) {
return c1.getName().compareTo(c2.getName());
}
});
characters.forEach(System.out::println);
System.out.println("------ Apparition Order");
Collections.sort(characters,new Comparator<Character>() {
@Override
public int compare(Character c1, Character c2) {
return c1.getAppearanceYear() - c2.getAppearanceYear();
}
});
characters.forEach(System.out::println);
System.out.println("--- Java 8 lambda");
System.out.println("------ Alphabetical Order");
final Comparator<Character> compAlphaName = (c1,c2) -> c1.getName().compareTo(c2.getName());
characters.sort(compAlphaName);
characters.forEach(System.out::println);
System.out.println("------ Apparition Order");
characters.sort((c1,c2) -> c1.getAppearanceYear() - c2.getAppearanceYear());
characters.forEach(System.out::println);
System.out.println("--- Java 8 stream");
System.out.println("------ Alphabetical Order");
characters.stream()
.sorted((c1,c2) -> c1.getName().compareTo(c2.getName()))
.forEach(System.out::println);
System.out.println("------ Apparition Order");
characters.stream()
.sorted((c1,c2) -> c1.getAppearanceYear() - c2.getAppearanceYear())
.forEach(System.out::println);
}
private static void filterAndPrint() {
System.out.println("--- Java 7");
System.out.println("------ Female characters");
for(Character c : characters) {
if(c.getGender() == FEMALE) {
System.out.println(c.getName() + " " + c.getGender());
}
}
System.out.println("------ Characters appeared between 2005 and 2008");
for(Character c : characters) {
if(2005 <= c.getAppearanceYear() && c.getAppearanceYear() <= 2008) {
System.out.println(c.getName() + " " + c.getAppearanceYear());
}
}
System.out.println("--- Java 8 simple stream");
System.out.println("------ Female characters");
characters.stream()
.filter(c -> c.getGender() == FEMALE)
.forEach(c -> System.out.println(c.getName() + " " + c.getGender()));
System.out.println("------ Characters appeared between 2005 and 2008");
characters.stream()
.filter(c -> 2005 <= c.getAppearanceYear())
.filter(c -> c.getAppearanceYear() <= 2008)
.forEach(c -> System.out.println(c.getName() + " " + c.getAppearanceYear()));
System.out.println("--- Java 8 stream and map");
System.out.println("------ Female characters");
characters.stream()
.filter(c -> c.getGender() == FEMALE)
.map(c -> c.getName() + " " + c.getGender())
.forEach(System.out::println);
System.out.println("------ Characters appeared between 2005 and 2008");
characters.stream()
.filter(c -> 2005 <= c.getAppearanceYear())
.filter(c -> c.getAppearanceYear() <= 2008)
.map(c -> c.getName() + " " + c.getAppearanceYear())
.forEach(System.out::println);
}
private static void filterSortAndPrint() {
filterSortAndPrintJava7();
filterSortAndPrintJava8();
}
private static void filterSortAndPrintJava7() {
System.out.println("--- Java 7");
System.out.println("------ Female characters");
final Comparator<Character> compAlphaName = new Comparator<Character>() {
@Override
public int compare(Character c1, Character c2) {
return c1.getName().compareTo(c2.getName());
}
};
final List<Character> femaleCharacters = new ArrayList<>();
characters.sort(compAlphaName);
for(Character c : characters) {
if(c.getGender() == FEMALE) {
femaleCharacters.add(c);
System.out.println(c.getName() + " " + c.getGender());
}
}
System.out.println("------ Characters appeared between 2005 and 2008");
final Comparator<Character> compAppearanceYear = new Comparator<Character>() {
@Override
public int compare(Character c1, Character c2) {
return c1.getAppearanceYear() - c2.getAppearanceYear();
}
};
final List<Character> charactersAppearedBetween2005and2008 = new ArrayList<>();
characters.sort(compAppearanceYear);
for(Character c : characters) {
if(2005 <= c.getAppearanceYear() && c.getAppearanceYear() <= 2008) {
charactersAppearedBetween2005and2008.add(c);
System.out.println(c.getName() + " " + c.getAppearanceYear());
}
}
System.out.println("------ Female characters appeared between 2005 and 2008");
femaleCharacters.retainAll(charactersAppearedBetween2005and2008);
femaleCharacters.sort(compAppearanceYear);
for(Character c : femaleCharacters) {
System.out.println(c.getAppearanceYear() + " " + c.getName() + " " + c.getGender());
}
}
private static void filterSortAndPrintJava8() {
System.out.println("--- Java 8");
System.out.println("------ Female characters");
final Comparator<Character> compAlphaName = (c1,c2) -> c1.getName().compareTo(c2.getName());
final List<Character> femaleCharacters = characters.stream()
.filter(c -> c.getGender() == FEMALE)
.collect(Collectors.toList());
femaleCharacters.stream()
.sorted(compAlphaName)
.forEach(c -> System.out.println(c.getName() + " " + c.getGender()));
System.out.println("------ Characters appeared between 2005 and 2008");
final Comparator<Character> compAppearanceYear = (c1,c2) -> c1.getAppearanceYear() - c2.getAppearanceYear();
final List<Character> charactersAppearedBetween2005and2008 = characters.stream()
.filter(c -> 2005 <= c.getAppearanceYear() && c.getAppearanceYear() <= 2008)
.collect(Collectors.toList());
charactersAppearedBetween2005and2008.stream()
.sorted(compAppearanceYear)
.forEach(c -> System.out.println(c.getName() + " " + c.getAppearanceYear()));
System.out.println("------ Female characters appeared between 2005 and 2008");
charactersAppearedBetween2005and2008.stream()
.filter(c -> femaleCharacters.contains(c))
.sorted(compAppearanceYear)
.forEach(c -> System.out.println(c.getAppearanceYear() + " " + c.getName() + " " + c.getGender()));
}
}
package model;
public class Character {
public static enum Gender {
MALE,FEMALE
}
private String name;
private int appearanceYear;
private String actor;
private String show;
private Gender gender;
public Character(String name, int appearanceYear, String actor, Gender gender, String show) {
this.name = name;
this.appearanceYear = appearanceYear;
this.actor = actor;
this.show = show;
this.gender = gender;
}
/**
* Returns value of name
* @return
*/
public String getName() {
return name;
}
/**
* Sets new value of name
* @param
*/
public void setName(String name) {
this.name = name;
}
/**
* Returns value of appearanceYear
* @return
*/
public int getAppearanceYear() {
return appearanceYear;
}
/**
* Sets new value of appearanceYear
* @param
*/
public void setAppearanceYear(int appearanceYear) {
this.appearanceYear = appearanceYear;
}
/**
* Returns value of actor
* @return
*/
public String getActor() {
return actor;
}
/**
* Sets new value of actor
* @param
*/
public void setActor(String actor) {
this.actor = actor;
}
/**
* Returns value of show
* @return
*/
public String getShow() {
return show;
}
/**
* Sets new value of show
* @param
*/
public void setShow(String show) {
this.show = show;
}
/**
* Returns value of gender
* @return
*/
public Gender getGender() {
return gender;
}
/**
* Sets new value of gender
* @param
*/
public void setGender(Gender gender) {
this.gender = gender;
}
/**
* Create string representation of Character for printing
* @return
*/
@Override
public String toString() {
return "Character [name=" + name + ", appearanceYear=" + appearanceYear + ", actor=" + actor + ", show=" + show + ", gender=" + gender + "]";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment