Java 将InternetAddress集转换为字符串集

Java 将InternetAddress集转换为字符串集,java,collections,java-stream,Java,Collections,Java Stream,我有一组Internet地址收件人,它们是我的收件人。我需要把它转换成一套。我曾经 Set<String> reci = new HashSet<>(); for(InternetAddress recipient : recipients){ reci.add(recipient.toString()); } 这会产生错误“无法解析构造函数字符串”。映射(字符串::新建)意味着元素->新建字符串(元素),具有InternetAddress的字符串构造函数不存在

我有一组Internet地址
收件人
,它们是我的收件人。我需要把它转换成一套。我曾经

Set<String> reci = new HashSet<>();
for(InternetAddress recipient : recipients){
    reci.add(recipient.toString());
}
这会产生错误“无法解析构造函数字符串”。

映射(字符串::新建)意味着
元素->新建字符串(元素)
,具有
InternetAddress的字符串构造函数不存在

你需要

recipients.stream().map(InternetAddress::toString).collect(Collectors.toSet());
.map(String::new)
表示
元素->新字符串(element)
,具有
InternetAddress的字符串构造函数不存在

你需要

recipients.stream().map(InternetAddress::toString).collect(Collectors.toSet());

除了
.map(String::new)
的问题之外,构建HashSet的确切等价物是
collect(Collectors.toCollection(HashSet::new))
而不是
collect(Collectors.toSet())
除了
.map(String::new)
的问题之外,构建HashSet的确切等价物是
collect(Collectors.toCollection(HashSet::new))
而不是
collect(Collectors.toSet())