0
0

Java Gold 例題 parallel

Last updated at Posted at 2024-08-13

GroupingByConcurrentExampleクラスのinsert code部分に入力すると、出力結果が得られるコードはどれでしょうか。

class Product {
    private String name;
    private double price;

    public Product(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }

    @Override
    public String toString() {
        return "Product{name='" + name + "', price=" + price + '}';
    }
}
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;

public class GroupingByConcurrentExample {
	public static void main(String[] args) {
		List<Product> products = Arrays.asList(
				new Product("AAAA", 1000.00),
				new Product("BBBB", 500.00),
				new Product("CCCC", 300.00),
				new Product("DDDD", 900.00),
				new Product("EEEE", 125.00),
				new Product("FFFF", 398.00)
		);
		
		ConcurrentMap<String, List<Product>> groupedProducts = 
          products.parallelStream()
                  .collect(Collectors.groupingByConcurrent(/*insert code */));
		
		groupedProducts.forEach((k, v) -> {
			System.out.println("Price Range : " + k);
			v.forEach(System.out::println);
		});
	}

}

出力結果

Price Range : High
Product{name='DDDD', price=900.0}
Product{name='AAAA', price=1000.0}
Price Range : Low
Product{name='CCCC', price=300.0}
Product{name='EEEE', price=125.0}
Product{name='FFFF', price=398.0}
Price Range : Medium
Product{name='BBBB', price=500.0}

A

Product::getPrice;

B

p -> {
    if(p.getPrice() >= 800) return "High";
    else if(p.getPrice() >= 400) return "Medium";
    else return "Low";
}

C

(Product p) -> {
    if(product.getPrice() >= 800) {
        return "High";
    } else if(product.getPrice() >= 400) {
        return "Medium";
    } else {
        return "Low";
    }
}

D

p -> {
    double price = p.getPrice();
    if(price >= 800) "High";
    else if(price >= 400)  "Medium";
    else "Low";
}

E

product -> {
    Predicate<Product> f = {
        double price = product.getPrice();
        if(price >= 800)  "High";
        else if(price >= 400) "Medium";
        else  "Low";
    }
    f.test(product)
}

F

if(product.getPrice() >= 800) return "High";
else if(product.getPrice() >= 400) return "Medium";
else return "Low";

B
Collectors.groupingByConcurrent()に関する問題です。
データをグループ化するためのメソッドで、大規模なデータを扱う場合や、複数のスレッドで作業を分散させたい場合に役立ちます。
並列処理の利点を活かしながら、データを分類・グループ化することができます。
本コードではグループ化された商品を、価格帯ごとに出力します。

groupingByConcurrent
public static <T,K> Collector<T,?,ConcurrentMap<K,List<T>>> groupingByConcurrent(Function<? super T,? extends K> classifier)

groupingByConcurrent(classifier, toList());

型パラメータ:
T - 入力要素の型
K - キーの型
パラメータ:
classifier - 入力要素をキーにマップする分類関数
戻り値:
グループ化操作を実装した、順序付けされていない並行なCollector

分類関数は、要素をあるキーの型Kにマップします。 コレクタによって生成されるConcurrentMap<K, List<T>>のキーは、入力要素に分類関数を適用した結果の値であり、対応する値は、分類関数の下で関連キーにマップされた入力要素を含むListになります。

ConcurrentMap<String, List<Product>> groupedProducts = 
          products.parallelStream()
                  .collect(Collectors.groupingByConcurrent(p -> {
                        if(p.getPrice() >= 800) return "High";
                        else if(p.getPrice() >= 400) return "Medium";
                        else return "Low";
                    }));

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0