【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
import java.util.*;
public class TestArgsWords {
private static final Integer ONE = new Integer(1);
public static void main(String[] args) {
Map<String, Integer> m = new HashMap<>();
for (int i = 0; i < args.length; i++) {
Integer freq = m.get(args[i]);
m.put(args[i], freq == null ? ONE : new Integer(freq.intValue() + 1));
}
System.out.println(
m.size() + " distinct words detected:");
System.out.println(m);
}
}
或者利用 auto_boxing / auto_unboxing 机制写成:
import java.util.*;
public class TestArgsWords {
private static final int ONE = 1;
public static void main(String[] args) {
Map<String, Integer> m = new HashMap<>();
for (int i = 0; i < args.length; i++) {
int freq = m.get(args[i]) == null ? 0 : m.get(args[i]);
m.put(args[i], freq == 0 ? ONE : freq + 1);
}
System.out.println(
m.size() + " distinct words detected:");
System.out.println(m);
}
}
运行结果:
来源:oschina
链接:https://my.oschina.net/u/3637389/blog/3151951