我正在尝试使用 Set 接口(interface)从我的 String 类型的 List 对象中删除重复项,但我面临的问题是它还会重新排序我的列表,这是我不想要的。我想保留列表的顺序并仅删除重复项?
static List<String> StopNames = new ArrayList<String>();
StopNames.add(sCurrentLine);
Set<String> set = new HashSet<String>(StopNames);
for (String string : set) {
System.out.println("Printing Set "+string);
}
最佳答案
只需将 HashSet
切换为 LinkedHashSet
,这样可以在删除重复项的同时保留插入顺序。
关于java - java - 如何在不重新排序的情况下从列表中删除重复项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21897314/