如何将HashSet类型的集合中的所有String都转换为小写 ?

问题描述 投票:-1回答:1

我不确定将集合中的所有字符串转换为小写的最佳方法。有什么想法吗?

    private Set<String> email;    

    if(userEmail instanceof Collection) {
    this.email = new HashSet<String>((Collection<String>) userEmail);
    model.put("userEmail", this.email); //need to convert this to lower case
}

提前感谢:-)

java string collections hashset lowercase
1个回答
0
投票

要将Set中的值转换为小写,请不要使用该构造函数,只需将字符串转换为小写,然后再将其添加到集合中:

this.email = ((Collection<String>) userEmail).stream()
        .map(String::toLowerCase).collect(Collectors.toSet());

this.email = new HashSet<>();
for (String s : (Collection<String>) userEmail)
    this.email.add(s.toLowerCase());
© www.soinside.com 2019 - 2024. All rights reserved.