简体   繁体   English

如何从列表中删除重复项而无需在Java中重新排序?

[英]How to remove duplicates from list without reordering in java?

I am trying to remove duplicates from my List object of the type String using Set Interface but the problem i face is that it also reorders my list which is something i don't want. 我试图从使用Set Interface的String类型的List对象中删除重复项,但我遇到的问题是它也重新排序我的列表,这是我不想要的。 I want to preserver the order of the list and remove duplicates only ? 我要保留列表的顺序并仅删除重复项吗?

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);

}

只需为LinkedHashSet切换HashSet ,它会在删除重复项时保留插入顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM