Java collections convert a string to a list of characters

前端 未结 10 1997
礼貌的吻别
礼貌的吻别 2020-12-02 12:51

I would like to convert the string containing abc to a list of characters and a hashset of characters. How can I do that in Java ?

List

        
10条回答
  •  借酒劲吻你
    2020-12-02 13:44

    In Java8 you can use streams I suppose. List of Character objects:

    List chars = str.chars()
        .mapToObj(e->(char)e).collect(Collectors.toList());
    

    And set could be obtained in a similar way:

    Set charsSet = str.chars()
        .mapToObj(e->(char)e).collect(Collectors.toSet());
    

提交回复
热议问题