Created
October 1, 2015 02:08
-
-
Save twinkfrag/a1813a389b6f02bead58 to your computer and use it in GitHub Desktop.
アルゴ演習15-2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by @twinkfrag on 2015/09/26. | |
*/ | |
import java.nio.file.*; | |
import java.util.*; | |
import java.io.*; | |
import java.util.stream.*; | |
/** | |
* アルゴ演習15の課題に関して、解説とJava8などでの改良点 | |
* 問題に対する解答として間違っている可能性もあるので注意 | |
* <p> | |
* 2週目 | |
*/ | |
public class Algo15_ex2 { | |
public static void main(String[] args) { | |
} | |
} | |
/** | |
* 以下、提出したクラス | |
*/ | |
class TestCollectionSort { | |
public static void main(String[] args) { | |
Path inputFile = Paths.get("rand2.txt"); | |
Path newFile = Paths.get("result_ex2-1.txt"); | |
// Streamで読み込み | |
// try-with-resource statement (先週も書いたので省略) | |
try (Stream<String> inputStream = Files.lines(inputFile)) { | |
List<String> list = inputStream.collect(Collectors.toList()); | |
System.out.println(list); | |
// sortメソッドの種類に関しては課題3あたりで全部書く | |
Collections.sort(list); | |
System.out.println(list); | |
Files.write(newFile, list); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
class SortScoreComparable { | |
public static void main(String[] args) { | |
// Arrays.asList()で同じ型の引数(いくつでも)からArrayListを一気に作ることが可能 | |
List<Score> score = Arrays.asList( | |
new Score("taro", 12, 97), | |
new Score("jiro", 42, 54), | |
new Score("sabu", 42, 47), | |
new Score("siro", 57, 97), | |
new Score("goro", 87, 40), | |
new Score("roku", 99, 99), | |
new Score("nana", 14, 23), | |
new Score("hati", 42, 54) | |
); | |
System.out.println("ソート前"); | |
// ListのforEachにラムダ式や関数への参照を渡すことで要素各々への適用が可能 (Java8) | |
// つまり以下と同じ | |
// for (Score s : score) { | |
// System.out.println(s); | |
// } | |
// System.out::println は Systemクラスのout静的フィールド(PrintStream型)のprintlnインスタンスメソッドへの参照 | |
// また以下とも同等 | |
// score.forEach(x -> System.out.println(x)); | |
score.forEach(System.out::println); | |
Collections.sort(score); | |
System.out.println("ソート後"); | |
score.forEach(System.out::println); | |
} | |
static class Score implements Comparable<Score> { | |
private String name; | |
private int math; | |
private int english; | |
public Score(String name, int math, int english) { | |
this.name = name; | |
this.math = math; | |
this.english = english; | |
} | |
public String getName() { | |
return name; | |
} | |
public int getMath() { | |
return math; | |
} | |
public int getEnglish() { | |
return english; | |
} | |
// 従来の実装 | |
@Override | |
public int compareTo(Score another) { | |
int compMath = -Integer.compare(getMath(), another.getMath()); | |
return compMath != 0 | |
? compMath | |
: -Integer.compare(getEnglish(), another.getEnglish()); | |
} | |
@Override | |
public String toString() { | |
return String.format("%s : Math: %s : English : %s", getName(), getMath(), getEnglish()); | |
} | |
} | |
} | |
class SortScoreComparator { | |
public static void main(String[] args) { | |
List<Score2> score = Arrays.asList( | |
new Score2("taro", 12, 97), | |
new Score2("jiro", 42, 54), | |
new Score2("sabu", 42, 47), | |
new Score2("siro", 57, 97), | |
new Score2("goro", 87, 40), | |
new Score2("roku", 99, 99), | |
new Score2("nana", 14, 23), | |
new Score2("hati", 42, 54) | |
); | |
System.out.println("ソート前"); | |
score.forEach(System.out::println); | |
Comparator<Score2> score2Comparator = new ScoreComparator(); | |
Collections.sort(score, score2Comparator); | |
// 実用的には、Comparator<T>の匿名クラスのインスタンスを作って使い回すのが | |
// 無駄なクラスもインスタンスも生成しないのでよい | |
Comparator<Score2> score2Comparator1 = new Comparator<Score2>() { | |
@Override | |
public int compare(Score2 o1, Score2 o2) { | |
int compMath = -Integer.compare(o1.getMath(), o2.getMath()); | |
return compMath != 0 | |
? compMath | |
: -Integer.compare(o1.getEnglish(), o2.getEnglish()); | |
} | |
}; | |
// また、Java8では単一メソッドの匿名クラスの生成はラムダ式で置き換えることができる | |
Comparator<Score2> score2Comparator2 = (o1, o2) -> { | |
int compMath = -Integer.compare(o1.getMath(), o2.getMath()); | |
return compMath != 0 | |
? compMath | |
: -Integer.compare(o1.getEnglish(), o2.getEnglish()); | |
}; | |
// あるいは、Comparatorの静的メソッドを使用して宣言的にComparatorを生成する | |
// import staticを使えばさらに短くなるが紛らわしくなるので使わないでおく | |
Comparator<Score2> score2Comparator3 = Comparator | |
.comparing(Score2::getMath, Comparator.<Integer>reverseOrder()) | |
.thenComparing(Score2::getEnglish, Comparator.<Integer>reverseOrder()); | |
// 上記score2Comparator3と同等コード | |
// import static java.util.Comparator.*; | |
// Comparator<Score2> score2Comparator3 = | |
// comparing(Score2::getMath).reversed() | |
// .thenComparing(Score2::getEnglish).reversed(); | |
System.out.println("ソート後"); | |
score.forEach(System.out::println); | |
} | |
static class Score2 { | |
private String name; | |
private int math; | |
private int english; | |
public Score2(String name, int math, int english) { | |
this.name = name; | |
this.math = math; | |
this.english = english; | |
} | |
public String getName() { | |
return name; | |
} | |
public int getMath() { | |
return math; | |
} | |
public int getEnglish() { | |
return english; | |
} | |
@Override | |
public String toString() { | |
return String.format("%s : Math: %s : English : %s", getName(), getMath(), getEnglish()); | |
} | |
} | |
static class ScoreComparator implements Comparator<Score2> { | |
// 従来の実装 | |
@Override | |
public int compare(Score2 o1, Score2 o2) { | |
int compMath = -Integer.compare(o1.getMath(), o2.getMath()); | |
return compMath != 0 | |
? compMath | |
: -Integer.compare(o1.getEnglish(), o2.getEnglish()); | |
} | |
} | |
} | |
class SortByNumeric { | |
public static void main(String[] args) { | |
// 入力のStringを数値として比較するComparator | |
// 同一のインスタンスを使い回すためにクラス定義ではなく | |
// ローカル変数にラムダ式で用意 | |
Comparator<String> numericComparator = (s1, s2) -> { | |
try { | |
return Integer.compare(Integer.parseInt(s1), Integer.parseInt(s2)); | |
} catch (NumberFormatException e) { | |
return s1.compareTo(s2); | |
} | |
}; | |
// 同じ意味 | |
Comparator<String> numericComparator2 = Comparator.comparingInt((String s) -> { | |
try { | |
return Integer.parseInt(s); | |
} catch (NumberFormatException e) { | |
return 0; | |
} | |
}).thenComparing(Comparator.naturalOrder()); | |
// try-catchがいらなければ | |
Comparator<String> numericComparator3 = Comparator.comparingInt(Integer::parseInt); | |
List<String> list = Arrays.asList( | |
"2", "20", "10", "300", "1010", "1", "100", "11", "hoge", "fuga" | |
); | |
System.out.println("ソート前"); | |
System.out.println(list); | |
Collections.sort(list); | |
System.out.println("辞書順ソート"); | |
System.out.println(list); | |
// sort API 1 | |
List<String> sortedList1 = list.stream() | |
.sorted(numericComparator).collect(Collectors.toList()); | |
System.out.println("sorted by stream API"); | |
System.out.println(sortedList1); | |
// sort API 2 | |
List<String> sortedList2 = new LinkedList<>(list); | |
sortedList2.sort(numericComparator2); | |
System.out.println("sorted by List<>#sort"); | |
System.out.println(sortedList2); | |
// sort API 3 | |
List<String> sortedList3 = new LinkedList<>(list); | |
Collections.sort(sortedList3, numericComparator3); | |
System.out.println("sorted by Collections.sort:"); | |
System.out.println(sortedList3); | |
} | |
} | |
class SortByNumeric2 { | |
public static void main(String[] args) { | |
Path inputFile = Paths.get("rand2.txt"); | |
Path newFile = Paths.get("result_ex2-5.txt"); | |
// Streamで読み込み | |
try (Stream<String> inputStream = Files.lines(inputFile)) { | |
Comparator<String> numericComparator = Comparator.comparingInt(Integer::parseInt); | |
List<String> sorted = inputStream.sorted(numericComparator).collect(Collectors.toList()); | |
Files.write(newFile, sorted); | |
} catch (IOException | NumberFormatException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment