使用流操作字符串
假设我想从我的.String
String s = "abc-de3-2fg";
我可以使用 一个来做到这一点:IntStream
s.stream().filter(ch -> Character.isLetter(ch)). // But then what?
为了将此流转换回实例,我该怎么办?String
另一方面,为什么我不能将 a 视为类型的对象流?String
Character
String s = "abc-de3-2fg";
// Yields a Stream of char[], therefore doesn't compile
Stream<Character> stream = Stream.of(s.toCharArray());
// Yields a stream with one member - s, which is a String object. Doesn't compile
Stream<Character> stream = Stream.of(s);
根据 javadoc,的创建签名如下所示:Stream
流.of(T... 值)
我能想到的唯一(糟糕的)方式是:
String s = "abc-de3-2fg";
Stream<Character> stream = Stream.of(s.charAt(0), s.charAt(1), s.charAt(2), ...)
当然,这还不够好...我错过了什么?