为什么我在Java编程中被设置为数组列表?

为什么我在Java编程中被设置为数组列表?,java,set,Java,Set,各位程序员好 因此,我正在创建一个程序,允许用户指定他们想要的保险类型,作为保险单的一部分 因为他们只有4种类型的封面可供选择,所以我想使用集合或散列集合来跟踪提供给用户的封面类型 问题是,is似乎没有消除重复项,而是将其视为arraylist,当我打印出集合的内容时,我会得到集合中不应出现的重复项 这是我用来将创建的保险类型对象传递给保险单类的代码 CoverType theInsuranceType = new CoverType("Fire",250); theInsurancePolic

各位程序员好

因此,我正在创建一个程序,允许用户指定他们想要的保险类型,作为保险单的一部分

因为他们只有4种类型的封面可供选择,所以我想使用集合或散列集合来跟踪提供给用户的封面类型

问题是,is似乎没有消除重复项,而是将其视为arraylist,当我打印出集合的内容时,我会得到集合中不应出现的重复项

这是我用来将创建的保险类型对象传递给保险单类的代码

CoverType theInsuranceType = new CoverType("Fire",250);
theInsurancePolicy.includeCover(theInsuranceType);
这是我在保险单类中使用的代码,用于跟踪用户购买的保险类型

    import java.util.*;
    // Class:   InsurancePolicy
    // Purpose: To represent a particular customer's insurance agreement
    public class InsurancePolicy {


            //ArrayList<Product> orderList = new ArrayList<Product>();
        private static int totalPolicyCost;

        private static String name = null;

        private static int price = 0;

        private int amount = 0;

        private static int total = 0, claims = 0;


        private static Set<CoverType> TheCoverType = new HashSet<CoverType>();


        public static boolean includeCover(CoverType which)
        {

            TheCoverType.add(which);
            System.out.println(which.getName()+" Has ben added to your insurance    policy");
            System.out.println(" ");
            System.out.println(" ");
            System.out.println("-----------------------");  
            return true;

        }
import java.util.*;
//类别:保险单
//目的:代表特定客户的保险协议
公共类保险单{
//ArrayList orderList=新建ArrayList();
私人静态成本;
私有静态字符串名称=null;
私有静态整数价格=0;
私人整数金额=0;
私有静态整数总计=0,索赔=0;
私有静态集合TheCoverType=newHashSet();
公共静态布尔includeCover(CoverType which)
{
添加(哪一个);
System.out.println(ben将其添加到您的保险单中的.getName()+);
System.out.println(“”);
System.out.println(“”);
System.out.println(“--------------------------”;
返回true;
}

作为保险单类的一部分,我这样做是为了迭代集合,为用户提取并打印每个对象的值,这就是我获取副本的地方

    Iterator<CoverType> iter = TheCoverType.iterator();
        int cash =0;
        while (iter.hasNext())
        {
            CoverType type = iter.next();
             cash =  type.getPrice();
            amount = amount + cash;
            System.out.println("This one Cost.  "+ cash);
                        // int cost = TheCoverType.getPrice().next();
//          if theCover     


        }
        amount = amount+ 100;
        String message = "So all up total cost of the policy is. "+cash; 
        return amount;
Iterator iter=TheCoverType.Iterator();
int cash=0;
while(iter.hasNext())
{
CoverType类型=iter.next();
cash=type.getPrice();
金额=金额+现金;
System.out.println(“这一个成本。”+现金);
//int cost=TheCoverType.getPrice().next();
//如果这件事结束了
}
金额=金额+100;
String message=“所以保单的总成本为。”+现金;
退货金额;

任何帮助都将不胜感激。

您想要放入集合的对象需要实现和


即使所有字段都是“相等”的,在java中也不会得到对象相等(
.equals()
),除非重写这些方法。

我假设集合所持有的类型是“CoverType”,对吗?如果是,是否重载equals和hashCode()对于这个类?

您为您的CoverType类实现了.equals和.hashCode吗?

我想知道我们中有多少人会得出相同的答案?大多数IDE都支持为您的类生成hashCode和equals。(省去您编写它)是的,为什么我会重载equals和hashCode()@Binyomin:smart alec answer:这样你们就不会遇到问题了。更好的答案是:因为这就是Hashset用来识别唯一对象从而删除重复项的方法。我在保险单类中这样做是为了迭代集合以提取信息迭代器iter=TheCoverType.Iterator();int cash=0;while(iter.hasNext()){CoverType type=iter.next();cash=type.getPrice();amount=amount+cash;System.out.println(“这一成本。”+cash);//int Cost=TheCoverType.getPrice().next();//如果theCover}amount=amount+100;String message=“所以保单的总成本为。”+现金;返回金额;哎哟。请将您的代码作为对原始问题的编辑发布,因为代码在评论中很难阅读。@Binyomin:但我仍然不知道您在哪里实现了我们三人提出的建议,以使CoverType具有相当好的等号和哈希代码覆盖。既然我们三人都不能rong?检查Google看看如何做好这一点。StackOverflow有很多不错的例子。不知道我为什么要这么做??我不熟悉使用set(非常新!!)你需要重写equals和hashcode方法,因为这些方法将用于查找一个对象是否等于另一个对象,然后检索该对象(HashSet在内部使用HashMap,所以记住,哈希、bucket等等)