在Java中,有一个更优雅的方式来删除重复的字符串和ArrayList字符串的? [英] In Java, is there a more elegant way to remove duplicate strings from and ArrayList of Strings?

查看:210
本文介绍了在Java中,有一个更优雅的方式来删除重复的字符串和ArrayList字符串的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,长话短说,我有一个需要字符串长ArrayList的一个Java的家庭作业以各种方式来操纵(我们正在做这样的事情的话显示出组合,添加和ArrayList中,没有什么太移除特别)。我注意到,一些提供的ArrayList中有重复的条目(和重复是没有必要为这个任务),所以我就从我的老师还好通过删除重复项消毒的数据。
以下是我想出了:

So, a long story short, I have a Java homework assignment that requires a long ArrayList of Strings to be manipulated in various ways (we're doing things like showing combinations of words, adding and removing from the ArrayList, nothing too special). I noticed that a few of the provided ArrayLists have duplicate entries (and the duplicates aren't necessary for this assignment), so I got the okay from my teacher to sanitize the data by removing duplicate entries. Here's what I came up with:

private static ArrayList<String> KillDups(ArrayList<String> ListOfStrings) {  

    for (int i = 0 ; i < ListOfStrings.size(); i++) {
        for (int j = i + 1; j < ListOfStrings.size(); j++) {
            //don't start on the same word or you'll eliminate it.
            if ( ListOfStrings.get(i).toString().equalsIgnoreCase( ListOfStrings.get(j).toString() )  ) {
                ListOfStrings.remove(j);//if they are the same, DITCH ONE.
                j = j -1; //removing the word basically changes the index, so swing down one.
            }                                
        }
    }
    return ListOfStrings;
}

这是罚款,我的任务,但我怀疑它会在现实世界中非常有用的。有没有办法做到这一点,将忽略比较期间的空白和特殊字符?是否有一个普遍更清洁的方式(不嵌套For循环也许)来处理呢?难道还有其他的问题,我应该问,我不知道要问?

This is fine for my assignment, but I doubt it would be very useful in the real world. Is there a way to do this that would ignore white space and special characters during the comparison? Is there a cleaner way in general to handle this (maybe without the nested For Loops)? Is there another question I should be asking that I don't know to ask?

推荐答案

是的。它可以在短短1(高雅)行完成:

Yes. And it can be done in just 1 (elegant) line:

List<String> noDups = new ArrayList<String>(new LinkedHashSet<String>(list));

中间 设置 保证没有重复。该 LinkedHashSet 实施设置的被选为preserve列表的顺序。

The intermediate Set ensures no duplicates. The LinkedHashSet implementation of Set was chosen to preserve the order of the list.



此外,在一个样式注意:


Also, on a style note:


  • 您的名字方法和参数的开头以小写字母名称

  • 总是指抽象的(即列表),而不是具体的(即的ArrayList )指定的方法签名时,

  • name your methods and parameters with names starting with a lowercase letter
  • always refer to the abstract (ie List) rather than the concrete (ie ArrayList) when specifying method signatures

您整个的方法则是:

private static List<String> killDups(List<String> list) {
    return new ArrayList<String>(new LinkedHashSet<String>(list));
}

有关额外印象分使该方法通用的,所以它的任何作品的类型列表

For extra brownie points make the method generic, so it works with any type of List:

private static <T> List<T> killDups(List<T> list) {
    return new ArrayList<T>(new LinkedHashSet<T>(list));
}

如果你想忽略某些字符,我想创建一个类,并将这些名单。无论是 散code() 和的 等于() 方法经的 HashSets 来删除的DUP:

If you wanted to ignore certain characters, I'd create a class for that and have a list of those. Both the hashCode() and the equals() methods are relied upon by HashSets to remove dups:

public class MungedString {
    // simplified code
    String s;

    public boolean equals(Object o) {
        // implement how you want to compare them here
    }

    public int hashCode() {
        // keep this consistent with equals()
    }
}

然后

List<MungedString> list;
List<MungedString> noDupList = killDups(list);

这篇关于在Java中,有一个更优雅的方式来删除重复的字符串和ArrayList字符串的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆