如何忽略列表中的重复项并添加一个项目?

问题描述 投票:1回答:5

如果两个或更多用户具有相同的身高和体重,则忽略这些项目并添加一个项目并将名称设置为多个。我尝试下面的代码,但我不能删除相同的项目。

Info.class

public class Info {

    public String name;
    public int height;
    public int weight;

    public Info(String name,int height, int weight) {
        this.height = height;
        this.weight = weight;
        this.name=name;
    }
}

Main.class

    List<Info> infoList = new ArrayList<>();
    List<Info> infoListFiltered = new ArrayList<>();

    infoList.add(new Info("a",1, 2));
    infoList.add(new Info("b",2, 2));
    infoList.add(new Info("c",1, 3));
    infoList.add(new Info("d",1, 2));
    infoList.add(new Info("e",1, 2));



    for (int i = 0; i < infoList.size(); i++) {
        for (int j = i+1; j < infoList.size(); j++) {
            if(infoList.get(i).height==infoList.get(j).height&&infoList.get(i).weight==infoList.get(j).weight) {
                infoList.get(i).name="multi";
                infoListFiltered.add(infoList.get(i));
            }else {
                infoListFiltered.add(infoList.get(i));
            }
        }
    }

有什么建议?

java android algorithm
5个回答
4
投票

尝试这种方式看看它是否适合你。

public void processData()  {
    List<Info> infoList = new ArrayList<>();
    List<Info> infoListFiltered = new ArrayList<>();
    infoList.add(new Info("a",1, 2));
    infoList.add(new Info("b",2, 2));
    infoList.add(new Info("c",1, 3));
    infoList.add(new Info("d",1, 2));
    infoList.add(new Info("e",1, 2));
    for (Info info:infoList){
        int index=getDuplicateIndex(infoListFiltered,info);
        if(index==-1){
            infoListFiltered.add(info);
        }else{
            infoListFiltered.get(index).name+=","+info.name;
        }
    }
}

private int getDuplicateIndex(List<Info> infoListFiltered,Info info){
    for (int i = 0; i < infoListFiltered.size(); i++){
        if(infoListFiltered.get(i).height==info.height && infoListFiltered.get(i).weight==info.weight){
            return i;
        }
    }
    return -1;
}

1
投票

equals中创建自己的Info.class方法(这是一个很好的做法),以及一个setter,例如:

public boolean equals(Info info) {
    return this.height == info.height && this.weight == info.weight;
}

public void setName(String name) {
    this.name = name;
}

然后在你Main.class,只需创建另一个名为Info和相同的"multi"heightweight对象并添加它:

Info tmp = infoList.get(i);
if (tmp.equals(infoList.get(j))) {
    infoList.get(i).setName("multi");
    infoListFiltered.add(new Info("multi", tmp.height, tmp.weight));
} else {
    infoListFiltered.add(new Info(tmp.name, tmp.height, tmp.weight));
}

如果您创建另一个Info对象,您将有2个不同的对象。您的方式添加了相同的对象(例如相同的引用),因此在两个列表中都更改了名称。


0
投票

使用ArrayList的contains方法。 它通过在Info类中使用等于@Override的方法来比较您给出的对象和ArrayList中的对象。

另外还有来自Rebellabs for Java Collections的整齐的作弊表:https://zeroturnaround.com/rebellabs/java-collections-cheat-sheet/


0
投票

您可以尝试从infoListFiltered中删除重复的项目:

for (int i = 0; i < infoListFiltered.size(); i++) {
        for (int j = i+1; j < infoListFiltered.size(); j++) {
            if(infoListFiltered.get(i).name.equals(infoListFiltered.get(j).name)&&infoListFiltered.get(i).height==infoListFiltered.get(j).height&&infoListFiltered.get(i).weight==infoListFiltered.get(j).weight) {
                infoListFiltered.remove(j);
                j--; //adjust index on element removal
            }
        }
}

在打印infoListFiltered时,您将获得所需的输出


0
投票

可能有点过于复杂:

List<Info> infoListFiltered = infoList.stream().
       collect(
           // create a map with {height + " " + weight} as a key
           // and an Info object as result of reduction
           groupingBy(
                   i -> i.getHeight()+ " " + i.getWeight(), //use height + " " + weight as map key
                   collectingAndThen(
                           reducing(
                                   (a,b) -> new Info("multi", a.getHeight(), a.getWeight())
                                   // if there are 2 info with the same height and weight
                                   // create a new info object with name "multi"
                           ),
                           Optional::get
                   )
           )
       ).values().stream().collect(toList());

System.out.println(infoListFiltered);
// output [Info{name='multi', height=1, weight=2}, Info{name='c', height=1, weight=3}, Info{name='b', height=2, weight=2}]
© www.soinside.com 2019 - 2024. All rights reserved.