本文介绍了将LinkedHashset内容复制到新的ArrayList吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've a listView that has some content initially. If the same content it gets, i removed the duplication through linkedhashset. Now, i want copy the linkedhashset contents i.e without duplication contents to new ArrayList.

我尝试通过

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

但是,ArrayList也包括重复内容.

But, the ArrayList includes the duplication content too.

示例:

 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

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

推荐答案

下面的代码行将所有元素从第0个索引处插入到数组列表中

The following line just inserts all the elements into the arraylist starting from the 0th index

p.addAll(0,lhm);

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

And, the elements which were added using these lines were still present in the arraylist:

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

因此,如果您不希望重复,则应在添加来自链接哈希集的项之前清除数组列表.即

So, you should clear the array list before adding the items from the linkedhashset, in case you don't want the duplicates. i.e.

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

这篇关于将LinkedHashset内容复制到新的ArrayList吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 21:05
查看更多