Java 是否可以从包含自定义类型元素的列表中删除重复项?

Java 是否可以从包含自定义类型元素的列表中删除重复项?,java,Java,这是我的密码 package com.dto; public class OtherBrands { private String otherbrandsname ; public String getOtherbrandsname() { return otherbrandsname; } public void setOtherbrandsname(String otherbrandsname) { this.otherbr

这是我的密码

package com.dto;

public class OtherBrands {

    private String otherbrandsname ;
    public String getOtherbrandsname() {
        return otherbrandsname;
    }
    public void setOtherbrandsname(String otherbrandsname) {
        this.otherbrandsname = otherbrandsname;
    }
    public String getDealerBrandQty() {
        return dealerBrandQty;
    }
    public void setDealerBrandQty(String dealerBrandQty) {
        this.dealerBrandQty = dealerBrandQty;
    }
    private String dealerBrandQty ;

}

import java.util.ArrayList;
import java.util.List;
import com.dto.OtherBrands;

public class Test {
    public static void main(String args[])
    {
        List < OtherBrands > otherBrandsList = new ArrayList < OtherBrands > ();
        for (int k = 0; k < 3; k++) {
            OtherBrands otherbrands = new OtherBrands();
            String otherbrandsname = "Test";
            String dealerBrandQty = "2";
            otherbrands.setOtherbrandsname(otherbrandsname);
            otherbrands.setDealerBrandQty(dealerBrandQty);
            otherBrandsList.add(otherbrands);
        }

        for(int i=0;i<otherBrandsList.size();i++)
        {
            System.out.println(otherBrandsList.get(i).getOtherbrandsname()+"\t"+otherBrandsList.get(i).getDealerBrandQty());

        }
    }
}
如果键和值相同,则应将其视为重复


是否可以从列表中删除重复项?

首先,如果要避免重复项,请使用哈希集而不是列表

第二,您必须重写<代码> hash码< /COD>和<代码>等于 >以便<代码> hShSET 知道您认为哪些元素是相等的。< /P>

public class OtherBrands {

    @Override
    public boolean equals (Object other)
    {
        if (!(other instanceof OtherBrands))
            return false;
        OtherBrands ob = (OtherBrands) other;
        // add some checks here to handle any of the properties being null
        return otherbrandsname.equals(ob.otherbrandsname) &&
               dealerBrandQty.equals(ob.dealerBrandQty);
    }

    @Override
    public int hashCode ()
    {
        return Arrays.hashCode(new String[]{dealerBrandQty,otherbrandsname});
    }
}

首先,如果要避免重复,请使用哈希集而不是列表

第二,您必须重写<代码> hash码< /COD>和<代码>等于 >以便<代码> hShSET 知道您认为哪些元素是相等的。< /P>

public class OtherBrands {

    @Override
    public boolean equals (Object other)
    {
        if (!(other instanceof OtherBrands))
            return false;
        OtherBrands ob = (OtherBrands) other;
        // add some checks here to handle any of the properties being null
        return otherbrandsname.equals(ob.otherbrandsname) &&
               dealerBrandQty.equals(ob.dealerBrandQty);
    }

    @Override
    public int hashCode ()
    {
        return Arrays.hashCode(new String[]{dealerBrandQty,otherbrandsname});
    }
}

您应该使用
HashSet
而不是
ArrayList
,因为它可以保证删除重复项。它只需要在
OtherBrands
类中实现
hashCode()
equals()
方法


作为提示,如果您使用Eclipse:您可以使用编辑器菜单功能“Source/GenerateHashCodeandEquals”生成这两个方法。然后选择定义
OtherBrands
项目(名称、数量)标识的所有属性。

您应该使用
HashSet
而不是
ArrayList
,因为它保证删除重复项目。它只需要在
OtherBrands
类中实现
hashCode()
equals()
方法


作为提示,如果您使用Eclipse:您可以使用编辑器菜单功能“Source/GenerateHashCodeandEquals”生成这两个方法。然后选择定义
OtherBrands
项目(名称、数量)标识的所有属性。

您可以改为使用
set
,并在数据中记住覆盖
hashCode()
equals()
方法。您是否尝试了
Map
type?向
列表添加元素时,您可以检查新元素是否已经存在。这可以通过重写
hashCode()
equals()
方法来实现。这可以通过
HashSet
来完成,而且您还需要覆盖这两种方法。阅读这些方法的合同规则您可以使用
set
,并在数据中记住覆盖
hashCode()
equals()
方法。您是否尝试过
Map
类型?在向
列表添加元素时,您可以检查新元素是否已经存在。这可以通过重写
hashCode()
equals()
方法来实现。这可以通过
HashSet
来完成,而且您还需要覆盖这两种方法。阅读这些方法的合同规则我不能使用Hashset,因为相同的名称可以以不同的数量出现两次。根据
name
quantity
定义equals方法。你有一些正确的标准,你说的是重复。以相同的方式实现equals方法。@PreethiJain如果定义两个对象具有相同的名称和相同的数量,则可以使用HashSet。我不能使用HashSet,因为相同的名称可以以不同的数量出现两次。根据
名称
数量
定义equals方法。你有一些正确的标准,你说的是重复。以相同的方式实现equals方法。@PreethiJain如果定义两个对象具有相同名称和相同数量,则可以使用HashSet。我仍然可以使用HashSet吗??相同的名称可以以不同的数量出现两次。可以,但这取决于hashCode()和equals()的实现,因为这些方法确定HashSet的标识。我仍然可以使用HashSet吗??相同的名称可以以不同的数量出现两次。可以,但这取决于hashCode()和equals()的实现,因为这些方法确定HashSet的标识。