简体   繁体   中英

Is it good practice to pass method in a HashSet?

Is it a good practice to pass method in HashSet like below where i have passed the methods directly inside HashSet?

Set byPrice = new HashSet(getProductsByPriceFilter( lowPrice, highPrice));**

  public Set<Product> filterProducts(BigDecimal lowPrice,
                BigDecimal highPrice, String manufacturer, String category) {

            Set<Product> byPrice = new HashSet<Product>(getProductsByPriceFilter(
                    lowPrice, highPrice));
            Set<Product> byManufacturer = new HashSet<Product>(
                    getProductsByManufacturer(manufacturer));
            Set<Product> byCategory = new HashSet<Product>(
                    getProductsByCategory(category));

            byPrice.retainAll(byManufacturer);
            byPrice.retainAll(byCategory);

            return byPrice;
        }

One thought on this: if your method already returns something that could fill your entire Set - why not have the method return that Set?

Set<Product> byPrice = getProductsByPriceFilter(
                lowPrice, highPrice));

With some

public Set <Product> getProductsByPriceFilter(double low, double high) {

Same then for the other filters.

Or... pass the already filtered set into the next stage. You probably have a loop over all products in getProductsByManufacturer () . If you added the already filtered set as parameter you could reduce this loop to loop over all products that already are in price range and so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM