cast set to linkedhashset

432 Views Asked by At

following is the scenario that I am encountering

I have this set of string here

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

yet I have another function with a signature LinkedHashSet

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

is it safe for me to just cast attributes to LinkedHashSet and proceed with the function calls?

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

1

There are 1 best solutions below

0
Louis Wasserman On

This is technically safe, since you know the actual concrete type, but not good practice.

Instead, change

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

to

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

so you don't have to do a cast, and you can properly use the interface type throughout your program.