【问题标题】:Iterate different typed Sets into a User defined type List将不同类型的 Set 迭代到用户定义的类型列表中
【发布时间】:2015-12-19 19:30:35
【问题描述】:

我有不同类型的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类:

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

但在这些列表中可能包含重复项。这意味着 proNames 有两种产品 AppleBanana。但在 proPrice 我们有两种产品的价格 Apple 和 Banana 但有重复项。

(例如:假设 Apple--> $1Banana--> $2。在 proPrice-->[1,2,2]香蕉的两倍价格。)。

那么在这种情况下,我怎样才能将这些数据放在我的List&lt;MyProduct&gt; 中??

【问题讨论】:

  • 一开始为什么要把数据放到集合中呢?您不能通过集合按索引访问元素,因此不可能使用常规循环。您可能想要使用列表,除非您有理由尝试防止重复。然后,您可以使用索引循环访问列表中的每个元素。
  • 是的。我有一些重复的元素。这就是为什么使用集合而不是列表的原因。
  • 集合是无序的,所以如果你的四个集合应该是并行的,你需要使用列表或有序集合(例如LinkedHashSet
  • 所有的副本都一样吗?这似乎是一个可怕的想法。列表确实是这里的方法,然后在将它们组合到 MyProducts 后处理任何重复的解决方案。
  • @LouisWasserman 在将它们合并到myProducts 之前,我必须删除重复项。

标签: java list generics set


【解决方案1】:

通过组合不包含有关哪些元素相关信息的各种组件集中的相关组件片段来构造MyProducts 的List 是不可能的。你怎么知道哪些组件与其他组件搭配?

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

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

注意: 非常清楚,只要组件在Sets 中,您将无法创建getImage(enumerator) 之类的方法。我的解决方案假定您将images、qtys 等以任何形式保留,然后再将它们放入集合中以删除重复项。它使用Mapcontains 检查来避免重复。


更新:您已编辑您的问题,表明您现在将组件保存在Lists 而不是Sets。在这种情况下,TypeOfEnumeratorint,我们可以将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());

【讨论】:

    【解决方案2】:

    我不知道你为什么首先将它们分开^_^

    但无论如何,正如您所说,它们的长度都相同,那么只需一个 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]))
    }
    

    更新:对不起,我用 c# 写了这个例子,没有注意到你想要 Java 的,但我想这很清楚

    【讨论】:

    • 这里的Count 是什么。 imgs,proNames,proQty,proPrice 是这里的集合。
    • Java 中的yourSet.size()
    猜你喜欢
    • 2011-05-20
    • 2016-03-22
    • 1970-01-01
    • 1970-01-01
    • 2012-11-18
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多