我有一个字符串列表 ["abc", "xyz", "abc", "mno", "123"]
现在我想要这个列表中的唯一值。为此,我将 List 转换为 HashSet。
我能够实现相同但元素的顺序受到影响。输出非常随机,其中一个结果是 ["123", "xyz", "abc", "mno"]
。
但我想要结果集包含与数组列表顺序相同的项目。我怎样才能做到这一点?
List<String> parameters = new ArrayList<String>();
//add the parameter to List
Set<String> parameterSet=new HashSet<String>(parameters);
最佳答案
使用LinkedHashSet
List<String> parameters = new ArrayList<String>();
Call the List here
|
Set<String> parameterSet=new LinkedHashSet<String>(parameters);
关于java - List to Set 不影响元素的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20095221/