我有不同的类型化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和香蕉 - > $ 2在
proPrice
- > [1,2,2]价格香蕉两次。)。
所以在这种情况下,我怎么能把这些数据放在我的List<MyProduct>
?
为什么你把数据放在第一集?你不能通过索引来访问一个元素,所以定期循环是不可能的。您可能想要使用列表,除非您有理由尝试阻止重复。然后可以使用索引循环来访问列表中的每个元素。 –
是的,我有一些重复的元素。那就是为什么使用集合而不是列表。 –
集合是无序的,所以如果你的四个集合应该是并行的,你需要使用列表或有序集合(例如'LinkedHashSet') – khelwood