문제

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

}
도움이 되었습니까?

해결책

Just switch HashSet for LinkedHashSet, which preserves insertion order while removing duplicates.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top