???Java???
???????????? ???????[ 2015/7/22 11:14:30 ] ???????????????????
?????·????????????????????????????????????????????????????????????????????????????????????????嵥????????????70?С?
????1. import java.util.*;
????2.
????3. import static java.util.Arrays.asList;
????4. import static java.util.function.Function.identity;
????5. import static java.util.stream.Collectors.*;
????6.
????7. public class Main {
????8.
????9. public static void p(String s) {
????10. System.out.println(s.replaceAll("[\]\[]"?? ""));
????11. }
????12.
????13. private static List uniq(List letters) {
????14. return new ArrayList(new HashSet(letters));
????15. }
????16.
????17. private static List sort(List letters) {
????18. return letters.stream().sorted().collect(toList());
????19. }
????20.
????21. private static Map uniqueCount(List letters) {
????22. return letters.stream().
????23. collect(groupingBy(identity()?? counting()));
????24. }
????25.
????26. private static String getWordsLongerThan(int length?? List words) {
????27. return String.join(" | "?? words
????28. .stream().filter(w -> w.length() > length)
????29. .collect(toList())
????30. );
????31. }
????32.
????33. private static String getWordLengthsLongerThan(int length?? List words)
????34. {
????35. return String.join(" | "?? words
????36. .stream().filter(w -> w.length() > length)
????37. .mapToInt(String::length)
????38. .mapToObj(n -> String.format("%" + n + "s"?? n))
????39. .collect(toList()));
????40. }
????41.
????42. public static void main(String[] args) {
????43.
????44. String s = "The quick brown fox jumped over the lazy dog.";
????45. String sentence = s.toLowerCase().replaceAll("[^a-z ]"?? "");
????46.
????47. List words = asList(sentence.split(" "));
????48. List letters = asList(sentence.split(""));
????49.
????50. p("Sentence : " + sentence);
????51. p("Words : " + words.size());
????52. p("Letters : " + letters.size());
????53.
????54. p(" Letters : " + letters);
????55. p("Sorted : " + sort(letters));
????56. p("Unique : " + uniq(letters));
????57.
????58. Map m = uniqueCount(letters);
????59. p(" Counts");
????60.
????61. p("letters");
????62. p(m.keySet().toString().replace("??"?? ""));
????63. p(m.values().toString().replace("??"?? ""));
????64.
????65. p(" words");
????66. p(getWordsLongerThan(3?? words));
????67. p(getWordLengthsLongerThan(3?? words));
????68. }
????69. }
???????????????????
????Sentence : the quick brown fox jumped over the lazy dog
????Words : 9
????Letters : 44
????Letters : t?? h?? e?? ?? q?? u?? i?? c?? k?? ?? b?? r?? o?? w?? n?? ?? f?? o?? x?? ?? j?? u?? m?? p?? e?? d?? ?? o?? v?? e?? r?? ?? t?? h?? e?? ?? l?? a?? z?? y?? ?? d?? o?? g
????Sorted : ?? ?? ?? ?? ?? ?? ?? ?? a?? b?? c?? d?? d?? e?? e?? e?? e?? f?? g?? h?? h?? i?? j?? k?? l?? m?? n?? o?? o?? o?? o?? p?? q?? r?? r?? t?? t?? u?? u?? v?? w?? x?? y?? z
????Unique : ?? a?? b?? c?? d?? e?? f?? g?? h?? i?? j?? k?? l?? m?? n?? o?? p?? q?? r?? t?? u?? v?? w?? x?? y?? z
????Counts
????letters
????a b c d e f g h i j k l m n o p q r t u v w x y z
????8 1 1 1 2 4 1 1 2 1 1 1 1 1 1 4 1 1 2 2 2 1 1 1 1 1
????words
????quick | brown | jumped | over | lazy
????5 | 5 | 6 | 4 | 4
????????????????????????????????Щ?????????????汾??Java?ж????У???????Щ??????????????????????????????????????汾??Java????β??????????????????????????????????????????????洢????????????????Σ????????????????????????Java??δ?????????μ????????????????????????????????????????????????????????????????????????????????????
???????Щ????£?????????汾?е???Java????????????????????????????????????磬??????????е???import????е?Java????????java.util?μ??????????????????????????á???System.out.println???????滻?????????p???????????????????η?????????????????????????9-11???????????Υ???ЩJava?????淶????Щ??主?????飬??????????????????????????????Щ???????????????к?????
?????????Щ????£??????????JDK8??????????????????????????????3-5????????????????????????????????????????????10??45??????????????????????????????Ч??????????????????Щ??????????????????????????????????????????????????ɡ??????????????Щ???????????????????????????????????????????????????????????
???????????????????JDK 8????????Streaming API???????Streaming API?д???????????б???й???????????????17-40??????????IDE????????????????????????????????????????????????ЩAPI??????????????????????????????±????????????????????????η?????????????
????uniq()????13????sort()????17?????????????????Unix??ù????????sort??????????????ε???????????????????????????????????????б??С?UniqueCount()????21????uniq -c????????????map??????????????????????????????????????????????????????“getWords”????????26????33?????????????????????????getWordLengthsLongerThan()???????????Щ??????????????????????????????????String????
???????δ???δ?????κ???Lambda??????????????????????????????Java Stream API???????ó?????
???????
??????????????????????????????????????????£?“???????????????????????????????”Lambda???????μ?Stream API???????????????????????????????????????ó???????????????????????????????????
????????????????????????????????????????????????????Java?????????????????μ???????е???????????????????Щ?μ??????????????????????????????Java??????????????????????????????????????????????????г?????Щ??????????????????Java??С?
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11