将 LinkedHashset 内容复制到新的 ArrayList?

Copying LinkedHashset content to new ArrayList?

我有一个 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());           
    }

Price.class

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

下一行只是将所有元素插入到arraylist中,从第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.

A Set 只会确保其 own 元素是唯一的。您不能期望 ArrayList 排除重复项,除非 整个 集合通过集合进行过滤。例如:

...
p.addAll(0,lhm);
p = new ArrayList<String>(new HashSet<String>(p));

查看此完整解决方案可能对您有所帮助

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
public class LinkedHashSetToList {

        public static void main(String[] args) {
        Set<String> lhs = new LinkedHashSet<String>();
        lhs.add("mumbai");
        lhs.add("delhi");
        lhs.add("kolkata");
        lhs.add("chandigarh");
        lhs.add("dehradun");
        //print linkedhashset
        System.out.println("lhs is = "+lhs);
        List<String> list = new ArrayList<String>(lhs);
        // print arraylist
        System.out.println("ArrayList Is = "+list);

    }

}

输出:

lhs is = [mumbai, delhi, kolkata, chandigarh, dehradun]
ArrayList Is = [mumbai, delhi, kolkata, chandigarh, dehradun]

参考:How to Convert a LinkedHashSet to ArrayList