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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28935962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:25:21  来源:igfitidea点击:

Copying LinkedHashset content to new ArrayList?

javaandroidarraylistlinkedhashset

提问by user3289108

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 linkedhashsetcontents i.e without duplication contents to new ArrayList.

我有一个 listView 最初有一些内容。如果它获得相同的内容,我通过linkedhashset. 现在,我想将linkedhashset内容复制到 new ,即没有重复的内容ArrayList

I tried to copy through

我试图复制

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

But, the ArrayListincludes the duplication content too.

但是,也ArrayList包括重复的内容。

Example :

例子 :

 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; 
        }
    }

Output :

输出 :

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

回答by AKS

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

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

p.addAll(0,lhm);

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.

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

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

回答by Anuj Dhiman

See this full solution may be help you

看到这个完整的解决方案可能对你有帮助

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);

    }

}

Output:

输出:

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

Reference : How to Convert a LinkedHashSet to ArrayList

参考:如何将 LinkedHashSet 转换为 ArrayList

回答by shmosel

A Setwill only ensure that its ownelements are unique. You can't expect ArrayListto exclude duplicates unless the entirecollection is filtered through a set. For example:

ASet只会确保它自己的元素是唯一的。ArrayList除非整个集合都通过一个集合过滤,否则您不能期望排除重复项。例如:

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