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