public class CountLongWords {
public static void main (String[] args) throws IOException {
byte [] a;
a = Files.readAllBytes(Paths.get ("src/J01/a.txt" ));
String content = new String(a,StandardCharsets.UTF_8);
List<String> word = Arrays.asList(content.split("\\PL+" ));
long count = 0 ;
System.out .println(word);
for (String string : word) {
if (string .length()>12 )
count++;
}
System.out .println(count);
count = word.stream().filter(w->w.length()>12 ).count();
System.out .println(count);
count = word.parallelStream().filter(w->w.length()>12 ).count();
System.out .println(count);
}
}
public static void main(String[] args) throws IOException {
Path path = Paths.get("src/J01/a.txt" );
String content = new String(Files.readAllBytes(path),StandardCharsets.UTF_8);
Stream<String> word = Stream.of (content.split("//PL+" ));
// show("words" ,word);
Stream<String> song = Stream.of ("gently" ,"down" ,"the" ,"stream" );
// show("song" ,song);
Stream<String> silence = Stream.empty();
// show("silence" ,silence);
// 用"" 内内容填充流
Stream<String> echo = Stream.generate(()->"abc" ) ;
// show ("echo" ,echo) ;
Stream <Double > randoms = Stream .generate (Math::random) ;
// show ("randoms" ,randoms) ;
Stream <BigInteger > integers = Stream .iterate (BigInteger.ONE,n->n.add(BigInteger.ONE)) ;
// show ("BigIntegers" ,integers) ;
Stream <String > wordsAnotherWay = Pattern .compile ("\\PL+" ) .splitAsStream (content) ;
show ("wordsAnotherWay" ,wordsAnotherWay) ;
//一行一行输出
try (Stream<String> lines = Files.lines(path,StandardCharsets.UTF_8)) {
show ("lines" ,lines) ;
}
}
private static <T > void show (String title, Stream<T> stream) {
final int SIZE =10;
List <T > firstElements = stream .limit (SIZE+1 ) .collect (Collectors.toList()) ;
System .out .println (title+":" ) ;
for (int i = 0 ; i < firstElements.size(); i++) {
if (i>0 ) System .out .println ("," ) ;
if (i<SIZE) System .out .println (firstElements.get(i)) ;
else System .out .println (".." ) ;
}
System .out .println () ;
}
public static void main(String[] args) throws IOException {
String contents = new String(Files.readAllBytes(Paths.get("a.txt" )), StandardCharsets.UTF_8);
List<String> wordlist = Arrays.asList(contents.split("\\PL+" ));
Optional<String> optionalValue = wordlist.stream().filter(s -> s.contains("fred" )).findFirst();
System.out.println(optionalValue.orElse("No word" ) + "contains fred" );
Optional<String> optionalString = Optional.empty();
// 没有则返回N/A
String result = optionalString.orElse("N/A" );
System.out.println("result:" + result);
result = optionalString.orElseGet(() -> Locale.getDefault().getDisplayName()) ;
System .out .println (result) ;
try {
result = optionalString .orElseThrow (IllegalStateException::new ) ;
System .out .println (result) ;
} catch (Throwable e) {
}
System .out .println () ;
optionalValue = wordlist .stream () .filter (s -> s.contains("red" )) .findFirst () ;
optionalValue .ifPresent (s->System.out.println(s+" contains red" )) ;
Set <String > results = new HashSet <>() ;
optionalValue .ifPresent (results::add) ;
Optional <Boolean > added = optionalValue .map (results::add) ;
System .out .println (added) ;
System .out .println (inverse(4.0 ).flatMap(OptionalTest::squareRoot)) ;
System .out .println (inverse(-1.0 ).flatMap(OptionalTest::squareRoot)) ;
System .out .println (inverse(0.0 ).flatMap(OptionalTest::squareRoot)) ;
System .out .println () ;
//有值就执行OptionalTest 后的函数
Optional <Double > result2 = Optional .of (-4.0 ) .flatMap (OptionalTest::inverse) .flatMap (OptionalTest::squareRoot) ;
System .out .println (result2) ;
}
private static Optional <Double > inverse (double d) {
return d ==0?Optional .empty () :Optional .of (1 /d) ;
}
private static Optional <Double > squareRoot (double d) {
return d <0?Optional .empty () :Optional .of (Math.sqrt(d)) ;
}