且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Java中将Char数组转换为List

更新时间:2023-01-16 10:20:08

在Java 8中, CharSequence :: chars 方法提供了一种将 String 转换为 List< Character>的单行方式;

In Java 8, CharSequence::chars method provides a one-line way to convert a String into List<Character>:

myString.chars().mapToObj(c -> (char) c).collect(Collectors.toList());

如果你需要转换 char [] 列表<字符> ,您可以先从它创建字符串,然后应用上述解决方案。虽然它不太可读和漂亮,但它会很短。

And if you need to convert char[] to List<Character>, you might create a String from it first and then apply the above solution. Though it won't be very readable and pretty, it will be quite short.