为什么String.chars()是Java 8中的一个int流? [英] Why is String.chars() a stream of ints in Java 8?

查看:189
本文介绍了为什么String.chars()是Java 8中的一个int流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java 8中,有一种新方法 String.chars() 返回 int s的流( IntStream )代表字符代码。我猜很多人会想到这里有一个 char 的流。以这种方式设计API的动机是什么?

In Java 8, there is a new method String.chars() which returns a stream of ints (IntStream) that represent the character codes. I guess many people would expect a stream of chars here instead. What was the motivation to design the API this way?

推荐答案

正如其他人已经提到的那样,这背后的设计决策是为了防止方法和类的爆炸。

As others have already mentioned, the design decision behind this was to prevent the explosion of methods and classes.

但是,我个人认为这是一个非常糟糕的决定,并且应该,因为他们不想做 CharStream ,这是合理的,不同的方法,而不是 chars(),我想:

Still, personally I think this was a very bad decision, and there should, given they do not want to make CharStream, which is reasonable, different methods instead of chars(), I would think of:


  • Stream< Character> chars(),它提供了一个盒子字符串流,这会对性能造成轻微损失。

  • IntStream unboxedChars(),用于性能代码。

  • Stream<Character> chars(), that gives a stream of boxes characters, which will have some light performance penalty.
  • IntStream unboxedChars(), which would to be used for performance code.

然而,而非聚焦在为什么目前以这种方式完成的时候,我认为这个答案应该集中在使用我们用Java 8获得的API来实现它。

However, instead of focusing on why it is done this way currently, I think this answer should focus on showing a way to do it with the API that we have gotten with Java 8.

在Java 7中我会这样做:

In Java 7 I would have done it like this:

for (int i = 0; i < hello.length(); i++) {
    System.out.println(hello.charAt(i));
}

我认为在Java 8中使用它的合理方法如下:

And I think a reasonable method to do it in Java 8 is the following:

hello.chars()
        .mapToObj(i -> (char)i)
        .forEach(System.out::println);

这里我获得了一个 IntStream 并映射它通过lambda来对象 i - > (char)i ,这会自动将其装入 Stream< Character> ,然后我们可以做我们想要的,仍然使用方法引用为加号。

Here I obtain an IntStream and map it to an object via the lambda i -> (char)i, this will automatically box it into a Stream<Character>, and then we can do what we want, and still use method references as a plus.

请注意尽管你必须 mapToObj ,如果你忘了并使用 map ,那么什么都不会抱怨,但你仍然会得到 IntStream ,你可能会想知道它为什么打印整数值而不是代表字符的字符串。

Be aware though that you must do mapToObj, if you forget and use map, then nothing will complain, but you will still end up with an IntStream, and you might be left off wondering why it prints the integer values instead of the strings representing the characters.

Java 8的其他丑陋的替代品:

通过保留 IntStream 并希望最终打印它们,您不能再使用方法引用用于打印:

By remaining in an IntStream and wanting to print them ultimately, you cannot use method references anymore for printing:

hello.chars()
        .forEach(i -> System.out.println((char)i));

此外,使用对自己方法的方法引用不再有效!请考虑以下事项:

Moreover, using method references to your own method do not work anymore! Consider the following:

private void print(char c) {
    System.out.println(c);
}

然后

hello.chars()
        .forEach(this::print);

这会产生编译错误,因为可能存在有损转换。

This will give a compile error, as there possibly is a lossy conversion.

结论:

API是这样设计的,因为不想添加 CharStream ,我个人认为该方法应返回 Stream< Character> ,目前的解决方法是使用 IntStream 上的mapToObj(i - >(char)i),以便能够正常使用它们。

The API was designed this way because of not wanting to add CharStream, I personally think that the method should return a Stream<Character>, and the workaround currently is to use mapToObj(i -> (char)i) on an IntStream to be able to work properly with them.

这篇关于为什么String.chars()是Java 8中的一个int流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆