首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >转换为linkedhashset

转换为linkedhashset
EN

Stack Overflow用户
提问于 2022-10-19 21:34:20
回答 1查看 33关注 0票数 0

下面是我遇到的情景

我这里有一套绳子

Set<String> attributes = new LinkedHashSet<>();

不过,我还有一个带有签名LinkedHashSet的函数。

public static List<String> rerankAttributesByType (LinkedHashSet<String> attributeList)

对我来说,只将属性转换为LinkedHashSet并继续函数调用安全吗?

List<String> newAttributes = rerankAttributesByType((LinkedHashSet<String>) unselectedAttribute)

EN

回答 1

Stack Overflow用户

发布于 2022-10-19 21:37:47

这在技术上是安全的,因为你知道实际的混凝土类型,但不是很好的做法。

相反,改变

代码语言:javascript
复制
public static List<String> rerankAttributesByType(LinkedHashSet<String> attributeList)

代码语言:javascript
复制
public static List<String> rerankAttributesByType (Set<String> attributeList)

因此,您不必执行强制转换,并且可以在整个程序中正确地使用接口类型。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74132144

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档