【问题标题】:How to remove duplicates from list without reordering in java?java - 如何在不重新排序的情况下从列表中删除重复项?
【发布时间】:2014-02-20 03:10:43
【问题描述】:

我正在尝试使用 Set 接口从我的 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);

}

【问题讨论】:

  • 使用 LinkedHashSet 代替

标签: java collections linked-list linkedhashset


【解决方案1】:

只需将HashSet 切换为LinkedHashSet,这样可以在删除重复项的同时保留插入顺序。

【讨论】:

    猜你喜欢
    • 2020-08-28
    • 1970-01-01
    • 2018-12-24
    • 1970-01-01
    • 2021-02-25
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    相关资源
    最近更新 更多