【问题标题】:Java-- characters in list and mapping to uppercaseJava——列表中的字符并映射到大写
【发布时间】:2017-11-18 03:49:44
【问题描述】:

我正在尝试将列表中的所有内容都转换为大写,但我遇到了麻烦。最后一行出现错误:

方法映射(功能不适用于 参数(Character::toUppercase)

但我不确定如何解决它。另外,如何显示此列表中“g”之后的所有字符?

 public static void main(String[] args)  {
      List<Character> list = new ArrayList<>();
      String alphabet = "abcdefghijklmnopqrstuvwxyz";
      SecureRandom random = new SecureRandom();

      for (int i = 0; i < 30; ++i)
      {
         list.add(alphabet.charAt(random.nextInt(26)));
      }

     System.out.println("Ascending: ");

     list.stream().sorted().forEach(System.out::print);

     System.out.println("Characters in uppercase: ");

     list.stream().map(Character::toUpperCase).collect(toList());
}  

【问题讨论】:

  • System.out.println(list.stream().map(String::valueOf).map(String::toUpperCase).collect(Collectors.joining()));System.out.println(list.stream().filter(ch -&gt; ch &gt; 'g').map(String::valueOf).map(String::toUpperCase).collect(Collectors.joining()));
  • “最后一行出现错误” - 错误是什么?我无法复制...
  • 关于第二个问题:list.subList(list.indexOf('G')+1, list.size())
  • 这是我得到的错误:“Stream 类型中的方法 map(Function
  • 对于第二个,我该怎么做才能按字典顺序显示 'g' 之后的字母?

标签: java list java-stream


【解决方案1】:

正如 Oleg 所说,您的错误消息表明您的编译代码引用了 getUppercase,其中 c 插入了 C。您的代码示例看起来不错,对我有用。也许你只需要重新编译?

为了过滤掉前六个字母,你可以像整数一样比较字符:

Stream.of('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
          'u', 'v', 'w', 'x', 'y', 'z')
      .map(Character::toUpperCase)
      .filter(c -> c > 'G')
      .forEach(System.out::print);

// HIJKLMNOPQRSTUVWXYZ

【讨论】:

    【解决方案2】:

    您的代码拼写错误 请在 args 后添加“)”并使用正确的方法名称:toUpperCase

    请检查Oracle Character docs

    【讨论】:

      猜你喜欢
      • 2018-07-20
      • 2011-02-19
      • 1970-01-01
      • 1970-01-01
      • 2021-06-20
      • 2019-04-17
      • 2018-08-06
      • 1970-01-01
      • 2013-02-25
      相关资源
      最近更新 更多