2015-12-19 43 views
1

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

预先感谢。

UPDATE:

会认为我有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已经有两个产品苹果香蕉。但在proPrice我们得到了价格均为苹果香蕉但重复。

(例如:让我们假设苹果 - > $ 1香蕉 - > $ 2proPrice - > [1,2,2]价格香蕉两次。)。

所以在这种情况下,我怎么能把这些数据放在我的List<MyProduct>

+1

为什么你把数据放在第一集?你不能通过索引来访问一个元素,所以定期循环是不可能的。您可能想要使用列表,除非您有理由尝试阻止重复。然后可以使用索引循环来访问列表中的每个元素。 –

+0

是的,我有一些重复的元素。那就是为什么使用集合而不是列表。 –

+0

集合是无序的,所以如果你的四个集合应该是并行的,你需要使用列表或有序集合(例如'LinkedHashSet') – khelwood

回答

0

通过合并构建MyProductList一个S不包含关于哪些元素相关的信息的各种组件集中的相关联的组件是不可能的。你如何知道哪些组件与哪些其他组件一起使用?

理想的解决方案是不把组件(imageproNameqtyprice)为Set s的一切,这样就可以避免破坏它们之间的关系。这当然会引发你如何首先了解他们之间的关系的问题。假设你有这样一种机制(如果你不这样做,那么这将是不可能解决的),我会通过假设你可以编写像getImage(enumerator)这样的函数来将它抽象出来,其中enumerator就像索引到一堆列表或一个键中成一堆Map s。我会进一步假设你可以枚举所有可能的标识符Collectionenumerators。最后,如果你有重复,我会假设你可以通过使用产品的某个字段来识别是否有重复,可能是proName。有了所有这些假设,你可以构造MyProductSet一个独特的S如下:

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

备注:只要清楚起见,您将无法创建像getImage(enumerator)这样的方法,只要组件位于Sets 。我的解决方案假定您在将它们放入集合中以删除重复项之前,以任何形式(如)离开image s,qty s等。它使用Mapcontains检查管理避免重复。


更新:您已编辑你的问题说,你现在拿着你的组件条List真是让人不是Set秒。在这种情况下,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()); 
0

我不知道你为什么摆在首位^ _^

它们分开但无论如何,就像你说的,他们都是一样的长度则只有我一个说法应该是足够的。

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之一,但我想这是明确

+0

什么是“计数”在这里。 'imgs,proNames,proQty,proPrice'在这里设置。 –

+0

它是Java中的'yourSet.size()' –