统计单词个数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.TreeMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {

public static void main(String[] args) {
String text = "Good morning. Have a good class. " + "Have a good visit. Have fun!";
Stream.of(text.split("[\\s+\\p{P}]")).parallel().filter(e -> e.length() > 0)
.collect(Collectors.groupingBy(String::toLowerCase, TreeMap::new, Collectors.counting()))
.forEach((k, v) -> System.out.println(k + " " + v));
}

}

计算文件大小

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.io.File;
import java.nio.file.Files;
import java.util.Scanner;

public class Main {

public static long getSize(File file) {
if (file.isFile()) {
return file.length();
} else {
try {
return Files.list(file.toPath()).parallel().mapToLong(e -> getSize(e.toFile())).sum();
} catch (Exception e) {
return 0;
}
}
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String dir = in.nextLine();
System.out.println(getSize(new File(dir)) + "bytes");
}

}