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

标签 java list generics set

我输入了不同的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我们有 AppleBanana 的价格,但有重复。

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

那么在这种情况下我该如何将这些数据放入我的 List<MyProduct> 中??

最佳答案

通过组合各种组件集中的相关组件来构建 MyProductList 是不可能的,因为这些组件集中不包含有关哪些元素相关的信息。您如何知道哪些组件与其他组件搭配使用?

理想的解决方案是根本不将组件(imageproNameqtyprice)放入 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) 之类的方法。我的解决方案假设您将imageqty等保留为任何形式,然后再将它们放入集合中以删除重复项。它使用 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());

关于java - 将不同类型的集合迭代到用户定义的类型列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34374531/

相关文章:

java - 我需要将信息传递给一个类,然后返回到Java中的主类

python - 如何将字符串列表转换为 python 中的字典列表?

c# - C# 中的 Java 泛型

java - 在 Eclipse 中使用 Composites with Shell for java

java - 如何在Oracle(长数据)中将长html、图像和长字符串作为单列插入?

database - 使用Flutter从数据库中获取构建列表

python - 如何将 python 中的字典应用于字符串而不是单个字母

java - 通用参数可重用性

java - java.util.Arrays.copyOf(U[], int, Class<? extends T[]>) 中通用通配符的优点

java - 多线程 OptimisticLockException