fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.stream.Collectors;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. List<Package> packageList = new ArrayList<>(List.of(
  14. new Package(13L, "Normal", new ArrayList<>(List.of(new Service(100L, "China")))),
  15. new Package(10L, "Full", new ArrayList<>(List.of(new Service(100L, "Spain")))),
  16. new Package(5L, "Full", new ArrayList<>(List.of(new Service(100L, "Japan"))))
  17. ));
  18.  
  19. HashMap<String, Object> stringObjectHashMap = new HashMap<>();
  20. boolean isFound = packageList.stream()
  21. .filter(pack -> "Full".equalsIgnoreCase(pack.getPackageName()))
  22. .map(pack -> {
  23. stringObjectHashMap.put("packageId", pack.getPackageId());
  24. return pack;
  25. })
  26. .flatMap(pack -> pack.getServiceList().stream())
  27. .filter(service -> service.getLocation().equalsIgnoreCase("Japan"))
  28. .map(service -> {
  29. stringObjectHashMap.put("serviceId", service.getServiceId());
  30. return service;
  31. })
  32. .anyMatch(service -> service.getLocation().equalsIgnoreCase("Japan"));
  33. System.out.println("isFound: " + isFound);
  34.  
  35. Map<String, Long> mapRes = packageList.stream()
  36. .filter(pack -> pack.getPackageName().equalsIgnoreCase("Full") && pack.getServiceList().stream().anyMatch(s -> s.getLocation().equalsIgnoreCase("Japan")))
  37. .collect(Collectors.teeing(
  38. Collectors.toMap(p -> "packageId", Package::getPackageId, (id1, id2) -> id1),
  39. Collectors.flatMapping(pack -> pack.getServiceList().stream(), Collectors.toMap(s -> "serviceId", Service::getServiceId, (id1, id2) -> id1)),
  40. (map1, map2) -> {
  41. map1.putAll(map2);
  42. return map1;
  43. }));
  44. boolean isFound2 = mapRes.entrySet().size() == 2;
  45. System.out.println("\nMap: " + mapRes);
  46. System.out.println("isFound: " + isFound2);
  47. }
  48.  
  49. static class Package {
  50. Long packageId;
  51. String packageName;
  52. List<Service> serviceList;
  53.  
  54. public Package(Long packageId, String packageName, List<Service> serviceList) {
  55. this.packageId = packageId;
  56. this.packageName = packageName;
  57. this.serviceList = serviceList;
  58. }
  59.  
  60. public Long getPackageId() {
  61. return packageId;
  62. }
  63.  
  64. public String getPackageName() {
  65. return packageName;
  66. }
  67.  
  68. public List<Service> getServiceList() {
  69. return serviceList;
  70. }
  71. }
  72.  
  73. static class Service {
  74. Long serviceId;
  75. String location;
  76.  
  77. public Service(Long serviceId, String location) {
  78. this.serviceId = serviceId;
  79. this.location = location;
  80. }
  81.  
  82. public Long getServiceId() {
  83. return serviceId;
  84. }
  85.  
  86. public String getLocation() {
  87. return location;
  88. }
  89. }
  90. }
Success #stdin #stdout 0.2s 55576KB
stdin
Standard input is empty
stdout
isFound: true

Map: {packageId=5, serviceId=100}
isFound: true