java 8使用list实例更改列表以进行映射

ric*_*chs 7 java java-8 java-stream

我正尝试使用该Collectors.toMap调用将列表转换为地图.该列表由ActivityReconcile对象组成.我想将列表中每个条目的实例传递给toMap调用.

代码在下面,我需要实例的地方用??表示.

final List<ActivityReconcile> activePostedList = loader.loadActivePosted(accessToken);
Map<AccountTransactionKey, ActivityReconcile> postedActiveMap = 
activePostedList.stream().collect(
 Collectors.toMap(
 AccountTransactionKey.createNewAccountTransactionKeyFromActivityReconcileRecord(??),??));
Run Code Online (Sandbox Code Playgroud)

And*_*lko 4

如果我理解正确的话,你将需要类似的东西

Map<AccountTransactionKey, ActivityReconcile> result = choices
              .stream()
              .collect(Collectors.toMap(
                      AccountTransactionKey::generate,
                      Function.identity()));
Run Code Online (Sandbox Code Playgroud)

方法(在AccountTransactionKey类中)看起来像

public static AccountTransactionKey generate(ActivityReconcile reconcile) {...}
Run Code Online (Sandbox Code Playgroud)

我已替换createNewAccountTransactionKeyFromActivityReconcileRecgenerate以使答案更具可读性和易于理解。