在 Java 中将字符数组转换为 IntStream


假设我们有以下字符数组

Character arr[] = { 'V', 'e', 'h', 'i', 'c', 'l' , 'e' };

要将以上字符数组转换到 IntStream

IntStream stream = Stream.of(arr).flatMapToInt(IntStream::of);

为此我们使用了 flatMapToInt() 方法。

以下是在 Java 中将字符数组转换为 IntStream 的示例

示例

 在线演示

import java.util.stream.*;
public class Main {
   public static void main(String[] args) {
      Character arr[] = { 'V', 'e', 'h', 'i', 'c', 'l' , 'e' };
      System.out.println("The character array = ");
      for (char value : arr) {
         System.out.println("Value = " + value);
      }
      IntStream stream = Stream.of(arr).flatMapToInt(IntStream::of);
      System.out.println("
Character Array to IntStream = ");       stream.forEach(System.out::println);    } }

输出

The character array =
Value = V
Value = e
Value = h
Value = i
Value = c
Value = l
Value = e
Character Array to IntStream =
86
101
104
105
99
108
101

更新于: 30-Jul-2019

457 次浏览

开启你的 职业生涯

完成课程后取得认证

开始学习
广告
© . All rights reserved.