如何将此流方法修改为 return 对象而不是列表 <Object>?
How can I amend this streams method to return the Object rather than a List<Object>?
我有下面的代码,我想在 buildAccounts()
中的 List.of
中包含 getNames
方法,但是当我尝试这个时,我得到一个错误提示所需类型 List<Account>
与提供的类型 List <Object>
不匹配。我尝试通过添加 findAny().orElse(null)
将 getNames()
的 return 类型更改为 Account
,其中 return 是对象而不是列表,但是此方法没有longer 以 List
格式生成正确的输出。我的问题是,我需要做哪些更改才能允许 getNames()
在 buildAccounts List.of
中使用而不更改 getNames()
的输出?
public List<Account> buildAccounts(MetaData metaData){
return List.of(
createAccount(metaData.getAccountName(), metaData.getaccountType()), getNames(metaData, metaData.getaccountType()));
}
public List<Account> getNames(MetaData metadata, AccountType type){
return metaData.getNames().stream()
.map(n -> createAccount(n, type))
.collect(Collectors.toList());
}
public Account createAccount(String name, AccountType accountType){
....
}
你的问题是 List.of 不能那样使用。如果 getNames 的输出无法更改,您将需要更改 buildAccounts 而不是在此处使用 List.of。
由于您已将此问题标记为流并在其他函数中使用了流,因此一种方法是在 buildAccounts 中创建两个流并将它们合并。
public List<Account> buildAccounts(MetaData metaData){
Stream firstStream =
Stream.of(createAccount(metaData.getAccountName(), metaData.getaccountType()));
Stream secondStream =
getNames(metaData, metaData.getaccountType()).stream();
return Stream.concat(firstStream, secondStream).collect(Collectors.toList());
}
在我看来,您尝试将 Account
(由 createAccount(...)
返回)添加到 Account
的 List
(由 getNames(...)
返回) ).
但它不是那样工作的。 List.of
取任意数量的相同类型的元素并组成 List
个。
您将需要这样的东西:
public List<Account> buildAccounts(MetaData metaData){
List<Account> list = new ArrayList<>(getNames(metaData, metaData.getAccountType()));
list.add(0, createAccount(metaData.getAccountName(), metaData.getAccountType()));
return list;
}
在我看来,下面的内容与您要实现的目标相同吗?
public List<Account> buildAccounts(MetaData metaData){
List<Account> accounts = getNames(metaData, metaData.getAccountType());
accounts.add(createAccount(metaData.getAccountName(), metaData.getaccountType()));
return accounts;
}
我有下面的代码,我想在 buildAccounts()
中的 List.of
中包含 getNames
方法,但是当我尝试这个时,我得到一个错误提示所需类型 List<Account>
与提供的类型 List <Object>
不匹配。我尝试通过添加 findAny().orElse(null)
将 getNames()
的 return 类型更改为 Account
,其中 return 是对象而不是列表,但是此方法没有longer 以 List
格式生成正确的输出。我的问题是,我需要做哪些更改才能允许 getNames()
在 buildAccounts List.of
中使用而不更改 getNames()
的输出?
public List<Account> buildAccounts(MetaData metaData){
return List.of(
createAccount(metaData.getAccountName(), metaData.getaccountType()), getNames(metaData, metaData.getaccountType()));
}
public List<Account> getNames(MetaData metadata, AccountType type){
return metaData.getNames().stream()
.map(n -> createAccount(n, type))
.collect(Collectors.toList());
}
public Account createAccount(String name, AccountType accountType){
....
}
你的问题是 List.of 不能那样使用。如果 getNames 的输出无法更改,您将需要更改 buildAccounts 而不是在此处使用 List.of。
由于您已将此问题标记为流并在其他函数中使用了流,因此一种方法是在 buildAccounts 中创建两个流并将它们合并。
public List<Account> buildAccounts(MetaData metaData){
Stream firstStream =
Stream.of(createAccount(metaData.getAccountName(), metaData.getaccountType()));
Stream secondStream =
getNames(metaData, metaData.getaccountType()).stream();
return Stream.concat(firstStream, secondStream).collect(Collectors.toList());
}
在我看来,您尝试将 Account
(由 createAccount(...)
返回)添加到 Account
的 List
(由 getNames(...)
返回) ).
但它不是那样工作的。 List.of
取任意数量的相同类型的元素并组成 List
个。
您将需要这样的东西:
public List<Account> buildAccounts(MetaData metaData){
List<Account> list = new ArrayList<>(getNames(metaData, metaData.getAccountType()));
list.add(0, createAccount(metaData.getAccountName(), metaData.getAccountType()));
return list;
}
在我看来,下面的内容与您要实现的目标相同吗?
public List<Account> buildAccounts(MetaData metaData){
List<Account> accounts = getNames(metaData, metaData.getAccountType());
accounts.add(createAccount(metaData.getAccountName(), metaData.getaccountType()));
return accounts;
}