【问题标题】:How to convert a stream of Character into array of Character using stream functions in java如何使用java中的流函数将字符流转换为字符数组
【发布时间】:2021-12-02 05:43:57
【问题描述】:
static public void reverseString(char[] s){
    Object[] upperCaseArray = IntStream.range(0,s.length)
            .mapToObj(index -> s[index])
            .map(Character::toUpperCase)
            .toArray();
}

这里的upperCaseArray是Object类型的,如何通过流函数将其转换为char[]

【问题讨论】:

  • 也许你问错了问题,因为你全神贯注地认为char[] 是处理字符串的最佳方式。您的方法名称甚至是用词不当,因为您既没有处理字符串也没有反转它。考虑int[] a = IntStream.range(0, s.length) .map(index -> s[index]) .map(Character::toUpperCase) .toArray(); String str = new String(a, 0, a.length);,或者如果您使用字符串作为起点,int[] a = sourceString.codePoints().map(Character::toUpperCase).toArray(); 当然,String str = sourceString.toUpperCase(); 更简单。

标签: java java-stream


【解决方案1】:
static public void reverseString(char[] s) {
    Character[] upperCaseArray = IntStream.range(0, s.length)
            .mapToObj(index -> s[index])
            .map(Character::toUpperCase)
            .toArray(Character[]::new);
}

【讨论】:

  • char[]Character[] 不同
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多