如何优化循环?

huangapple go评论82阅读模式
英文:

How can i optimize cycles?

问题

这是要翻译的部分:

Method

@Override
public List<Product> filter(String[] brands, String[] cpus) {
    List<Product> products;
    List<Product> toReturn = new ArrayList<>();
    int i = 0;
    if(brands != null && cpus != null){
        for(String brand: brands){
            for(String cpu: cpus){
                products = productRepo.findAllByBrandAndCpu(brand, cpu);
                toReturn.addAll(products);
                System.out.println(i++);
            }
        }
    }else{
        if (brands != null){
            for (String brand: brands) {
                products = productRepo.findAllByBrand(brand);
                toReturn.addAll(products);
            }
        }else if(cpus != null){
            for (String cpu: cpus) {
                products = productRepo.findAllByCpu(cpu);
                toReturn.addAll(products);
            }
        }else{
            return productRepo.findAll();
        }
    }
    return toReturn;
}

Here

if(brands != null && cpus != null){
    for(String brand: brands){
        for(String cpu: cpus){
            products = productRepo.findAllByBrandAndCpu(brand, cpu);
            toReturn.addAll(products);
            System.out.println(i++);
        }
    }
}

Method findAllByBrandAndCpu

@Query(value = "SELECT p FROM Product p where p.brand.name = ?1 and p.cpu.name = ?2")
List<Product> findAllByBrandAndCpu(String brandName, String cpuName);
英文:

There is a method that takes two parameters of type String [] as input and does filtering depending on what it received.

Method

 @Override
    public List&lt;Product&gt; filter(String[] brands, String[] cpus) {
        List&lt;Product&gt; products;
        List&lt;Product&gt; toReturn = new ArrayList&lt;&gt;();
        int i = 0;
        if(brands != null &amp;&amp; cpus != null){
            for(String brand: brands){
                for(String cpu: cpus){
                    products = productRepo.findAllByBrandAndCpu(brand, cpu);
                    toReturn.addAll(products);
                    System.out.println(i++);
                }
            }
        }else{
            if (brands != null){
                for (String brand: brands) {
                    products = productRepo.findAllByBrand(brand);
                    toReturn.addAll(products);
                }
            }else if(cpus != null){
                for (String cpu: cpus) {
                    products = productRepo.findAllByCpu(cpu);
                    toReturn.addAll(products);
                }
            }else{
                return productRepo.findAll();
            }
        }
        return toReturn;
    }

How can I change this code so that there are not so many iterations here:

Here

if(brands != null &amp;&amp; cpus != null){
            for(String brand: brands){
                for(String cpu: cpus){
                    products = productRepo.findAllByBrandAndCpu(brand, cpu);
                    toReturn.addAll(products);
                    System.out.println(i++);
                }
            }
        } 

Method findAllByBrandAndCpu

@Query(value = &quot;SELECT p FROM Product p where p.brand.name = ?1 and p.cpu.name = ?2&quot;)
List&lt;Product&gt; findAllByBrandAndCpu(String brandName, String cpuName);

答案1

得分: 0

你好 @Kzz,您可以通过使用 IN SQL 运算符来更新您的存储库查询,从而去除两个嵌套循环,具体操作如下:

@Query(value = "SELECT p FROM Product p where p.brand.name IN ?1 and p.cpu.name IN ?2")
List<Product> findAllByBrandAndCpu(List<String> brands, List<String> cpus);

请注意,以上为翻译后的代码部分。

英文:

Hello @Kzz you can remove both nested loop by updating your repository query using the IN SQL operator as such:

@Query(value = &quot;SELECT p FROM Product p where p.brand.name IN ?1 and p.cpu.name IN ?2&quot;)
List&lt;Product&gt; findAllByBrandAndCpu(List&lt;String&gt; brands, List&lt;String&gt; cpus);

huangapple
  • 本文由 发表于 2020年4月5日 23:05:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/61044666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定