想从java中的char数组创建一个字符流

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31554025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:14:47  来源:igfitidea点击:

Want to create a stream of characters from char array in java

javajava-8java-stream

提问by Sangjin Kim

From a char array, I want to construct a stream to use java 8 features such as filters and maps.

从一个字符数组,我想构造一个流来使用 java 8 的特性,比如过滤器和映射。

char[] list = {'a','c','e'};
Stream<Character> cStream = Stream.of(list);
// Stream<Character> cStream = Arrays.stream(list);

The first method does not work (Reason: change cStream to Stream<char[]>). The commented line does not also work (Reason: The method stream(T[])in the type Arrays is not applicable for the arguments (char[])).

第一种方法不起作用(原因:将 cStream 更改为Stream<char[]>)。注释行也不起作用(原因:stream(T[])数组类型中的方法不适用于参数 ( char[]))。

I know that if char[] listis changed to int[], everything works fine using IntStream. But I do not want to convert every char[]to int[]each time or change into a list when I need to use stream library on chararray.

我知道如果char[] list更改为int[],则使用IntStream. 但是当我需要在数组上使用流库时,我不想每次都转换char[]int[]每次或更改为列表char

回答by T.J. Mazeika

The simplest change you can make to the code is change char[]to Character[].

您可以对代码进行的最简单更改是更改char[]Character[].

Another way is to create a new ArrayList of the boxed chars:

另一种方法是创建一个新的盒装 ArrayList char

char[] list = {'a','c','e'};
List<Character> listArray = new ArrayList<>();
for (char c : list)
    listArray.add(c);
Stream<Character> cStream = listArray.stream();

In addition, you can use Google Guava's Charsclass, to replace the forloop with:

此外,您可以使用 Google Guava 的Chars类,将for循环替换为:

List<Character> listArray = Chars.asList(list);

回答by Alexis C.

You can use an IntStreamto generate the indices followed by mapToObj:

您可以使用 anIntStream生成索引,后跟mapToObj

char[] arr = {'a','c','e'};
Stream<Character> cStream = IntStream.range(0, arr.length).mapToObj(i -> arr[i]);

回答by Tagir Valeev

A short and efficient way to create an IntStreamfrom char[]array is to use java.nio.CharBuffer:

创建IntStreamfromchar[]数组的一种简短有效的方法是使用java.nio.CharBuffer

char[] list = {'a','c','e'};
IntStream stream = CharBuffer.wrap(list).chars();

This way you can use an IntStreaminterpreting the int values as characters. If you want a boxed Stream<Character>(which may be less efficient), use

这样您就可以IntStream将 int 值解释为字符。如果您想要盒装Stream<Character>(可能效率较低),请使用

Stream<Character> stream = CharBuffer.wrap(list).chars().mapToObj(ch -> (char)ch);

Using CharBuffercan be a little bit faster than IntStream.rangeas it has custom spliterator inside, so it does not have to execute an additional lambda (possibly as slow polymorphic call). Also it refers to the char[]array only once and not inside the lambda, so it can be used with non-final array variable or function return value (like CharBuffer.wrap(getCharArrayFromSomewhere()).chars()).

使用CharBuffer可能比IntStream.range它内部有自定义拆分器快一点,因此它不必执行额外的 lambda(可能是慢的多态调用)。此外,它只引用char[]一次数组,而不是在 lambda 内部,因此它可以与非最终数组变量或函数返回值(如CharBuffer.wrap(getCharArrayFromSomewhere()).chars())一起使用。

回答by Lars Hartviksen

A way to do this is via a String object:

一种方法是通过 String 对象:

char[] list = {'a','c','e'};
Stream<Character> charStream = new String(list).chars().mapToObj(i->(char)i);

I like to do it this way because all the complexity of transforming the array is wrapped into the String creation, and the wrapping of char is also performed behind the scene for me so I can focus on the business logic.

我喜欢这样做,因为转换数组的所有复杂性都包含在 String 创建中,并且 char 的包装也在幕后为我执行,因此我可以专注于业务逻辑。

回答by Rakesh Chauhan

I believe the simplest way to convert to Character stream is

我相信转换为字符流的最简单方法是

    char[] list = {'a','c','e'};

    Stream<Character> characters = Stream.ofAll(list);

回答by Arpan Saini

Getting substream of Characters

获取字符子流

 String givenString = "MyNameIsArpan";

       Object[] ints = givenString.chars().mapToObj(i -> (char)i).toArray();

       String subString = Arrays.stream(ints,2,6).
                             map(i -> (char)i).
                             map(String::valueOf).
                             collect(Collectors.joining());

      System.out.println(subString);

OUTPUT : Name

输出:名称