flatmap

Am I using flatMap correctly to merge results from multiple API calls?

点点圈 提交于 2021-01-28 08:19:04
问题 I want to make multiple API calls (with three different queries) and merge the results and then display them in onNext() . It is working but I am concerned that flatMap is not ideal for this. @GET("www.examle.com/api/data/") Observable<WebResultsResponse> getWebResults(@Query("param1") String query); ----- private List<WebResult> resultsList; private void requestWebResults(String query) { resultsList.clear(); final Observable<List<WebResult>> observable = MainApplication.apiProvider

Is there any way to convert a 2D List to 1D List using only `map` not using `flatMap`?

你。 提交于 2020-12-30 08:34:11
问题 I'm a Java beginner, I just got learn about map and flatMap . When 2d List should be converted to 1d List, it was implemented like below. List<List<Integer>> list_2d = List.of(List.of(1, 2), List.of(3, 4)); List<Integer> lst1 = list_2d .stream() .flatMap(arr -> arr.stream()) .collect(Collectors.toList()); printAll(lst1); // [1, 2, 3, 4] But, I think it looks that it can be implemented not using flatMap . Are there any way to make the code with same logic, just using map , not using flatMap ?

Use Java 8 Optional in existing Java 7 code

会有一股神秘感。 提交于 2020-12-25 04:39:45
问题 I have an assignment in which I need to convert the following pre-Java 8 code to Java 8 code. Below is just one method which is giving me hard time to finish up: public static List<VehicleMake> loadMatching(Region region, String nameStartsWith, VehicleLoader loader) { if ((nameStartsWith == null) || (region == null) || (loader == null)) { throw new IllegalArgumentException("The VehicleLoader and both region and nameStartsWith are required when loading VehicleMake matches"); } List<VehicleMake

Use Java 8 Optional in existing Java 7 code

十年热恋 提交于 2020-12-25 04:39:05
问题 I have an assignment in which I need to convert the following pre-Java 8 code to Java 8 code. Below is just one method which is giving me hard time to finish up: public static List<VehicleMake> loadMatching(Region region, String nameStartsWith, VehicleLoader loader) { if ((nameStartsWith == null) || (region == null) || (loader == null)) { throw new IllegalArgumentException("The VehicleLoader and both region and nameStartsWith are required when loading VehicleMake matches"); } List<VehicleMake

Java 8 Optional and flatMap - what is wrong?

◇◆丶佛笑我妖孽 提交于 2020-12-03 17:59:29
问题 Some piece of code: public class Player { Team team; String name; } public class Team { List<Player> players; } public class Demo { @Inject TeamDAO teamDAO; @Inject PlayerDAO playerDAO; List<String> findTeamMatesNames(String playerName) { Optional<Player> player = Optional.ofNullable(playerDAO.get(playerName)); return player.flatMap(p -> teamDAO.findPlayers(p.team)) .map(p -> p.name) .orElse(Collections.emptyList()); } } Why am I not able to do this? In flatMap method I am getting error "Type

Java 8 Optional and flatMap - what is wrong?

安稳与你 提交于 2020-12-03 17:58:08
问题 Some piece of code: public class Player { Team team; String name; } public class Team { List<Player> players; } public class Demo { @Inject TeamDAO teamDAO; @Inject PlayerDAO playerDAO; List<String> findTeamMatesNames(String playerName) { Optional<Player> player = Optional.ofNullable(playerDAO.get(playerName)); return player.flatMap(p -> teamDAO.findPlayers(p.team)) .map(p -> p.name) .orElse(Collections.emptyList()); } } Why am I not able to do this? In flatMap method I am getting error "Type

Java 8 Optional and flatMap - what is wrong?

烈酒焚心 提交于 2020-12-03 17:57:57
问题 Some piece of code: public class Player { Team team; String name; } public class Team { List<Player> players; } public class Demo { @Inject TeamDAO teamDAO; @Inject PlayerDAO playerDAO; List<String> findTeamMatesNames(String playerName) { Optional<Player> player = Optional.ofNullable(playerDAO.get(playerName)); return player.flatMap(p -> teamDAO.findPlayers(p.team)) .map(p -> p.name) .orElse(Collections.emptyList()); } } Why am I not able to do this? In flatMap method I am getting error "Type