java - 将 LinkedHashset 内容复制到新的 ArrayList?

标签 java android arraylist linkedhashset

我有一个最初包含一些内容的 listView。如果得到的内容相同,我通过 linkedhashset 删除了重复项。现在,我想复制 linkedhashset 内容,即不复制内容到新的 ArrayList

我试过复制

p.addAll(0,lhm);  // P is the instance of  ArrayList and lhm is linkedHashset instance

但是,ArrayList 也包含重复内容。

示例:

 ArrayList<Price> p = new ArrayList<Price>();

     p.add(new Price("Banana", 60));
     p.add(new Price("Apple", 80));

    LinkedHashSet<Price> lhm = new LinkedHashSet<Price>(p); 
    lhm.add(new Price("Banana", 20)); 
    lhm.add(new Price("Apple", 40));
    lhm.add(new Price("Orange", 30)); 
    for(Price pr:lhm)
    {
        System.out.println(pr);
    } 
    Price duplicate = new Price("Banana", 20);
    System.out.println("inserting duplicate object..."); 
    lhm.add(duplicate);
    lhm.add(new Price("Apple", 40));
    p.addAll(0,lhm);
    System.out.println("After insertion:"); 
    for(Price pr:lhm)
    {
        System.out.println(pr);
    }

    for (int i = 0; i < p.size(); i++) {

        System.out.println(p.get(i).getItem() +"-" +p.get(i).getPrice());           
    }

价格等级

class Price
{
    private String item; 
    private int price; 
    public Price(String itm, int pr)
    {
        this.item = itm; 
        this.price = pr; 
        }
    public int hashCode()
    { 
        System.out.println("In hashcode");
        int hashcode = 0; 
        hashcode = price;
        //System.out.println(hashcode);

        hashcode+= item.hashCode(); 
    //  System.out.println(hashcode);

        return hashcode;  
        }

    public boolean equals(Object obj)
    {
        System.out.println("In equals"); 
        if (obj instanceof Price) 
        {
            Price pp = (Price) obj; 
            return (pp.item.equals(this.item) && pp.price == this.price); 
            }
        else 
        { 
            return false;
            }
        }

    public String getItem()
    {
        return item; 
    }

    public void setItem(String item) 
    { 
        this.item = item; 
        }

    public int getPrice() 

    {
        return price;
        }
    public void setPrice(int price) 
    {
        this.price = price; 
        }
    public String toString()
    {
        return "item: "+item+" price: "+price; 
        }
    }

输出:

In hashcode
In hashcode
In hashcode
In hashcode
In hashcode
item: Banana price: 60
item: Apple price: 80
item: Banana price: 20
item: Apple price: 40
item: Orange price: 30
inserting duplicate object...
In hashcode
In equals
In hashcode
In equals
//iterating linkedhasset content

After insertion:
item: Banana price: 60
item: Apple price: 80
item: Banana price: 20
item: Apple price: 40
item: Orange price: 30

// iterating ArrayList p content

Banana-60
Apple-80
Banana-20
Apple-40
Orange-30
Banana-60
Apple-80 <-- duplicate

最佳答案

下一行只是将所有元素插入到数组列表中,从第 0 个索引开始

p.addAll(0,lhm);

而且,使用这些行添加的元素仍然存在于数组列表中:

p.add(new Price("Banana", 60));
p.add(new Price("Apple", 80));

因此,您应该在从 linkedhashset 添加项目之前清除数组列表,以防您不想要重复项。即

p.clear();
p.addAll(lhm); // and, at this point you don't need the index.

关于java - 将 LinkedHashset 内容复制到新的 ArrayList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28935962/

相关文章:

java - 墙壁上的 Pong 反射

java - 重写方法的注释反射

android - 当我的手机屏幕在 android 中关闭时无法检测到摇动事件

Java鼠标监听器

android - 替换包含布局

android - 如何在模拟器中正确显示泰卢固字体

java - 编辑另一个 LinkedHashMap 内 ArrayList 内 LinkedHashMap 中键的值

java - 检查一个 ArrayList 的元素是否存在于另一个

java - 检查 Arraylist 中是否存在用户名

java - 如何从 thymeleaf 检索输入文本?