mirror of
https://github.com/sjsdfg/effective-java-3rd-chinese.git
synced 2025-03-24 16:20:48 +08:00
improve:更新代码排版
This commit is contained in:
parent
814f2bcca0
commit
580c686cbf
@ -25,8 +25,8 @@ public class Anagrams {
|
|||||||
try (Scanner s = new Scanner(dictionary)) {
|
try (Scanner s = new Scanner(dictionary)) {
|
||||||
while (s.hasNext()) {
|
while (s.hasNext()) {
|
||||||
String word = s.next();
|
String word = s.next();
|
||||||
groups.computeIfAbsent(alphabetize(word),
|
groups.computeIfAbsent(alphabetize(word),
|
||||||
(unused) -> new TreeSet<>()).add(word);
|
(unused) -> new TreeSet<>()).add(word);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,19 +50,19 @@ public class Anagrams {
|
|||||||
```java
|
```java
|
||||||
// Overuse of streams - don't do this!
|
// Overuse of streams - don't do this!
|
||||||
public class Anagrams {
|
public class Anagrams {
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
Path dictionary = Paths.get(args[0]);
|
Path dictionary = Paths.get(args[0]);
|
||||||
int minGroupSize = Integer.parseInt(args[1]);
|
int minGroupSize = Integer.parseInt(args[1]);
|
||||||
try (Stream<String> words = Files.lines(dictionary)) {
|
try (Stream<String> words = Files.lines(dictionary)) {
|
||||||
words.collect(
|
words.collect(
|
||||||
groupingBy(word -> word.chars().sorted()
|
groupingBy(word -> word.chars().sorted()
|
||||||
.collect(StringBuilder::new,
|
.collect(StringBuilder::new,
|
||||||
(sb, c) -> sb.append((char) c),
|
(sb, c) -> sb.append((char) c),
|
||||||
StringBuilder::append).toString()))
|
StringBuilder::append).toString()))
|
||||||
.values().stream()
|
.values().stream()
|
||||||
.filter(group -> group.size() >= minGroupSize)
|
.filter(group -> group.size() >= minGroupSize)
|
||||||
.map(group -> group.size() + ": " + group)
|
.map(group -> group.size() + ": " + group)
|
||||||
.forEach(System.out::println);
|
.forEach(System.out::println);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -76,18 +76,18 @@ public class Anagrams {
|
|||||||
// Tasteful use of streams enhances clarity and conciseness
|
// Tasteful use of streams enhances clarity and conciseness
|
||||||
public class Anagrams {
|
public class Anagrams {
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
Path dictionary = Paths.get(args[0]);
|
Path dictionary = Paths.get(args[0]);
|
||||||
int minGroupSize = Integer.parseInt(args[1]);
|
int minGroupSize = Integer.parseInt(args[1]);
|
||||||
|
|
||||||
try (Stream<String> words = Files.lines(dictionary)) {
|
try (Stream<String> words = Files.lines(dictionary)) {
|
||||||
words.collect(groupingBy(word -> alphabetize(word)))
|
words.collect(groupingBy(word -> alphabetize(word)))
|
||||||
.values().stream()
|
.values().stream()
|
||||||
.filter(group -> group.size() >= minGroupSize)
|
.filter(group -> group.size() >= minGroupSize)
|
||||||
.forEach(g -> System.out.println(g.size() + ": " + g));
|
.forEach(g -> System.out.println(g.size() + ": " + g));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// alphabetize method is the same as in original version
|
// alphabetize method is the same as in original version
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user