我输入了不同的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
有两种产品Apple和Banana。但是在 proPrice
我们有 Apple 和 Banana 的价格,但有重复。
(Ex: Lets assume Apple--> $1 and Banana--> $2. In
proPrice
-->[1,2,2] price for banana two times.).
那么在这种情况下我该如何将这些数据放入我的 List<MyProduct>
中??
最佳答案
通过组合各种组件集中的相关组件来构建 MyProduct
的 List
是不可能的,因为这些组件集中不包含有关哪些元素相关的信息。您如何知道哪些组件与其他组件搭配使用?
理想的解决方案是根本不将组件(image
、proName
、qty
和 price
)放入 Set
中,这样就可以避免破坏它们之间的关系。这当然引出了一个问题:你首先如何知道它们之间的关系。假设您有这样的机制(如果没有,那么这将无法解决),我将通过假设您可以编写像 getImage(enumerator)
这样的函数来抽象它,其中 enumerator
类似于一堆列表的索引或一堆 Map
的键。我将进一步假设您可以枚举 Collection
enumerators
中所有可能的标识符。最后,如果您有重复项,我假设您可以通过使用产品的字段之一(可能是 proName
)来识别某些内容是否重复。有了所有这些假设,您就可以构建一个唯一的 MyProduct
Set
,如下所示:
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)
之类的方法。我的解决方案假设您将image
、qty
等保留为任何形式,然后再将它们放入集合中以删除重复项。它使用 Map
和 contains
检查来避免重复。
更新:您已编辑问题,表示您现在将组件保存在 List
中,而不是 Set
中。在这种情况下,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());
关于java - 将不同类型的集合迭代到用户定义的类型列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34374531/