【问题标题】:Copying LinkedHashset content to new ArrayList?将 LinkedHashset 内容复制到新的 ArrayList?
【发布时间】:2015-03-09 05:55:27
【问题描述】:

我有一个 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

【问题讨论】:

  • 如果您仔细考虑,这不是问题。您创建了一个数组列表并在其中添加了两个元素。并使用该数组列表创建了另一个集合,并在集合上进行了一些插入,然后添加到列表中。那么你期待什么。如果您在其中添加了一些元素,列表如何防止重复(使用您的逻辑)?
  • 由于LinkedHashset只有独特的项目,我想有没有办法将独特的内容复制到新的ArrayList。
  • @user3289108 AKS 的回答正是证明了这一点。您的问题是您添加了对原始列表的相同引用的副本。如果你先清除它,所有的新元素都将是唯一的。

标签: java android arraylist linkedhashset


【解决方案1】:

下面一行只是将所有元素从第 0 个索引开始插入到 arraylist 中

p.addAll(0,lhm);

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

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.

【讨论】:

  • 查看 1,2 是否为 p.如果我添加另一个 2,3,4...最终的数组列表应该显示 1,2,3,4。 (通过防止重复..here 2)。我不需要清除所有项目。它应该出现,并且在添加重复的新项目时不应插入。
  • Arraylist 不保证像 Set 这样的唯一项目,这就是为什么当您在 p 中有项目 1、2 并且稍后添加 2、3、4 时,之后 p 中的项目将是 1、2 , 2, 3, 4.
  • 是的,但是因为我从linkedhashset 复制了内容,它只有唯一的内容,对吗?它不会丢弃旧内容吗?
  • @user3289108 List.addAll() 不会覆盖现有值,它只是添加额外的值,无论它们是否已经包含。
  • 对,我现在明白了。由于 p 已经有 2 个项目,因此添加包含相同项目的 lhm 会使其对新的 arraylist 重复。在将 lhm 添加到 p 之前清除 p 会有所帮助。谢谢。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    Set 只会确保其自己的 元素是唯一的。你不能指望ArrayList 排除重复项,除非 整个 集合通过集合过滤。例如:

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

    【讨论】:

    • 我知道。我想你错过了这条线LinkedHashSet&lt;Price&gt; lhm = new LinkedHashSet&lt;Price&gt;(p);
    • @user3289108 所做的只是确保lhm 只包含唯一的元素。但是没有人检查您随后从lhm 复制回p 的元素是否是p 独有的。
    • 是的,lhm 只包含独特的元素。您可以查看示例。在插入后,不包括Apple-80,因为它是linkedhashset。但是,它会在将 lhm 内容复制到新的 arraylist 时显示。
    • 是的,当然lhm 包含独特的元素,但是您将这些元素复制到已经包含相同元素的列表中。你需要停下来想一想。我们都在说同样的话,但你没有在听。
    • 为什么你只提到Apple-80是重复项,而Banana-60也是重复项。而且,由于每个人都在迭代和重复这些项目已经存在于数组列表中。而且,当您添加更多项目时,它们只是被附加,并不会替换现有项目。
    猜你喜欢
    • 2014-06-15
    • 1970-01-01
    • 2012-01-16
    • 2018-05-25
    • 2012-09-30
    • 2013-05-02
    • 2019-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多