如何将这个大代码转换为Java 8的lambda表达式

huangapple go评论67阅读模式
英文:

How to convert this big code to Java 8 lambda expressions

问题

我只会翻译代码部分,以下是代码的中文翻译:

public class charFile {

    public static void main(String args[]) {

        TreeMap<Character, Integer> hashMap = new TreeMap<Character, Integer>();
        File file = new File("new.txt");
        Scanner scanner = null;
        try {
            scanner = new Scanner(file, "utf-8");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        while (scanner.hasNext()) {
            char[] chars = scanner.nextLine().toLowerCase().toCharArray();
            for (Character c : chars) {
                if (!Character.isLetter(c)) {
                    continue;
                } else if (hashMap.containsKey(c)) {
                    hashMap.put(c, hashMap.get(c) + 1);
                } else {
                    hashMap.put(c, 1);
                }
            }
        }
        for (Map.Entry<Character, Integer> entry : hashMap.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }

    }
}

希望这有助于你更好地理解这段代码。

英文:

Im just new in java lambda and i struggling to make this code in lambda form please can someone at least tell me how to do it in big codes like this one
or should i use streams to make it easier and thx.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class charFile {

public static void main(String args[]) {


    TreeMap&lt;Character, Integer&gt; hashMap = new TreeMap&lt;Character, Integer&gt;();
    File file = new File(&quot;new.txt&quot;);
    Scanner scanner = null;
    try {
        scanner = new Scanner(file, &quot;utf-8&quot;);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    while (scanner.hasNext()) {
        char[] chars = scanner.nextLine().toLowerCase().toCharArray();
        for (Character c : chars) {
            if (!Character.isLetter(c)) {
                continue;
            } else if (hashMap.containsKey(c)) {
                hashMap.put(c, hashMap.get(c) + 1);
            } else {
                hashMap.put(c, 1);
            }
        }
    }
    for (Map.Entry&lt;Character, Integer&gt; entry : hashMap.entrySet()) {
        System.out.println(entry.getKey() + &quot;: &quot; + entry.getValue());
    }


}

}

答案1

得分: 4

Files.lines(Paths.get("new.txt"), StandardCharsets.UTF_8)
     .map(String::toLowerCase)
     .flatMapToInt(String::chars)
     .mapToObj(c -> (char)c)
     .filter(Character::isLetter)
     .collect(Collectors.groupingBy(c -> c, TreeMap::new, Collectors.counting()))
     .forEach((k, v) -> System.out.println(k + ": " + v));
英文:
Files.lines(Paths.get(&quot;new.txt&quot;), StandardCharsets.UTF_8)
        .map(String::toLowerCase)
        .flatMapToInt(String::chars)
        .mapToObj(c -&gt; (char)c)
        .filter(Character::isLetter)
        .collect(Collectors.groupingBy(c -&gt; c, TreeMap::new, Collectors.counting()))
        .forEach((k, v) -&gt; System.out.println(k + &quot;: &quot; + v));

huangapple
  • 本文由 发表于 2020年4月9日 06:23:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/61111015.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定