将不同类型的集合迭代到用户定义的类型列表中

Iterate different typed Sets into a User defined type List

我有不同类型的 Sets,如下所示:

        Set<String> imgs = new LinkedHashSet<String>(this.getImages());

        Set<String> proNames = new LinkedHashSet<String>(this.getProductNames());

        Set<Integer> proQty = new LinkedHashSet<Integer>(this.getQty());

        Set<Double> proPrice = new LinkedHashSet<Double>(this.getPrices());

我需要将上述集合中的所有数据插入到一个用户定义的类型 ArrayList 中,如下所示:

List<MyProduct> pList = new ArrayList<MyProduct>();
        for (Iterator<Double> iterator = proPrice.iterator(); iterator.hasNext();) {
            Double next = iterator.next();
            pList.add(new MyProduct(?, ?, ?, next));
        }

考虑所有集合的大小都相同。(所有 4 个集合包含相同数量的数据)

MyProduct class:

public class MyProduct {

    private String image;
    private String proName;
    private int qty;
    private double price;


    public MyProduct(String image, String proName, int qty, double price) {
        this.image = image;
        this.proName = proName;
        this.qty = qty;
        this.price = price;
    }
      //getters and setters
       ...

提前致谢。

更新:

假设我有 4 ArrayLists 而不是 Sets :

public ArrayList<String> proNames = new ArrayList();
public ArrayList<Double> proPrice = new ArrayList();
public ArrayList<Integer> proQty = new ArrayList();
public ArrayList<String> imgs = new ArrayList<String>();

但在这些列表中,重复项可能 include.Which 表示 proNames 有两个产品 AppleBanana。但是在 proPrice 中,我们得到了 AppleBanana 的价格,但有重复的价格。

(Ex: Lets assume Apple--> and Banana--> . In proPrice-->[1,2,2] price for banana two times.).

所以在这种情况下,我怎样才能将这些数据放入我的 List<MyProduct> ??

不知道你当初为什么要分开^_^

但无论如何,正如你所说,它们的长度都相同,那么只需要一个 for 语句就足够了。

List<MyProduct> pList = new List<MyProduct>();
for(int i = 0; i < imgs.Count; i++)
{
    pList.Add(new new MyProduct(imgs[i],proNames[i], proQty[i], proPrice[i]))
}

Update :抱歉,我用 c# 编写了示例,但没有注意到您想要 Java,但我猜对了

通过在不包含有关哪些元素相关的信息的各种组件集中组合组件的相关片段来构建 MyProductList 是不可能的。你怎么知道哪些组件与其他组件一起使用?

理想的解决方案是根本不要将组件(imageproNameqtyprice)放入 Set 中,所以你可以避免破坏他们之间的关系。这当然会引出一个问题,即您首先是如何知道它们之间的关系的。假设你有这样的机制(如果你没有那么这将无法解决),我会通过假设你可以编写像 getImage(enumerator) 这样的函数来抽象它,其中 enumerator 就像一个索引成一堆列表或一个键成一堆 Maps。我将进一步假设您可以在 Collection enumerators 中枚举所有可能的标识符。最后,如果您有重复项,我假设您可以使用产品的其中一个字段(可能是 proName)来确定某些内容是否重复。有了所有这些假设,您可以构造一个唯一的 Set of MyProduct,如下所示:

Map<String,MyProduct> uniqueProducts = new HashMap<>();
for(TypeOfEnumerator enumerator : enumerators){

    if(!uniqueProducts.contains(getProName(enumerator))){
        //You've stated a requirement that you avoid duplication
        //before you even create a duplicate MyProduct. This if
        //before the construction meets that requirement.
        uniqueProducts.put(getProName(enumerator),
            new MyProduct(getImg(enumerator),getProName(enumerator),getQty(enumerator),getPrice(enumerator)));
    }
}

然后,如果您想要 Set 而不是 Map 的最终结果:

Set<MyProduct> products = new HashSet<MyProduct>(uniqueProducts.values());

注意: 非常清楚,只要组件在 [=31= 中,您将无法创建像 getImage(enumerator) 这样的方法].我的解决方案假设您在将 imageqty 等以任何形式保留 之前将它们放入集合中以删除重复项 。它使用 Mapcontains 检查来避免重复。


更新:你已经编辑了你的问题,说你现在在 Lists 而不是 Sets 中持有你的组件。那样的话,TypeOfEnumerator就是int,我们可以把enumerator重命名为标准的i,方法getImage(enumerator)就变成了imgs.get(i)。这使得上面的代码变成:

Map<String,MyProduct> uniqueProducts = new HashMap<>();
for(int i=0; i<proNames.size(); i++){

    if(!uniqueProducts.contains(proNames.get(i))){
        //You've stated a requirement that you avoid duplication
        //before you even create a duplicate MyProduct. This if
        //before the construction meets that requirement.
        uniqueProducts.put(proNames.get(i),
            new MyProduct(imgs.get(i),proNames.get(i),proQty.get(i),proPrice.get(i)));
    }
}

Set<MyProduct> products = new HashSet<MyProduct>(uniqueProducts.values());