Posts

Showing posts with the label java-8

Compare the List of different Objects and build the new list of Object in java8 using Stream

Compare the List of different Objects and build the new list of Object in java8 using Stream Could someone help me converting the below code to java 8 standards using streams. Iterate CustomerRelationship list and customer list,compare customerRelationShip firstName with customer firstName. if it matches then construct the BusinessCustomer object using customerRelationShip and customer object and add it to businessCustomerList. if no matches then construct BusinessCustomer using customerRelationShip and add it to businessCustomerList. List<BusinessCustomer> businessCustomers = new ArrayList<BusinessCustomer>(); List<CustomerRelationship> customerRelationshipList = new ArrayList<CustomerRelationship>(); List<Customer> customerList = new ArrayList<Customer>(); for (CustomerRelationship customerRelationship: customerRelationshipList) { int temp = 0; for (Customer customer:customerList){ if(customer...

Collect the result with only a stream (without external loop use)

Collect the result with only a stream (without external loop use) Is there a way to do the following code only with lambdas ? // translate someList1 to someList3 // .. get sublist List<String> someList2 = someList1.stream() .map(i -> i.getField()) .collect(Collectors.toList()); // .. create new (target) list List<SomeClass> someList3 = new ArrayList<>(); for (String item : someList2) { SomeClass someObj = new SomeClass(); someObj.setField(item); someList3.add(someObj); } 3 Answers 3 Just one collect is required : List<SomeClass> someList2 = someList1.stream() .map(i -> { SomeClass someObj = new SomeClass(); someObj.setField(i.getField()); return someObj; } ) .collect(Collectors.toList()); But note that with a constructor...

Is it thread-safe to set value to class member property in CompletableFuture callback?

Is it thread-safe to set value to class member property in CompletableFuture callback? I am new to CompletableFuture in Java 8 and I wonder whether the following code snippet is thread-safe when I set the result to class member properties in a callback, and try to read them after the allOf().get() call, and why? CompletableFuture allOf().get() public void newInit() throws ExecutionException, InterruptedException { CompletableFuture cf1 = CompletableFuture.supplyAsync(() -> { return 1L; }).thenAccept(result -> { this.result1 = result; }); CompletableFuture cf2 = CompletableFuture.supplyAsync(() -> { return 2L; }).thenAccept(result -> { this.result2 = result; }); CompletableFuture.allOf(cf1, cf2).get(); } @DidierL I will read the values after CompletableFuture.allOf method, is it thread-safe? – Chuck Jun 28 at 6:25 ...

Stream grouping by sum of determinate objects

Stream grouping by sum of determinate objects I have a Request class like this : public class Request { String name,destName; int nSeats; //Example : requestOne,Paris,3 ... } I want to group the request in a Map |Integer,List of String| where the keys are the sum of the request with the same destName and the values are the destinations' names. Here is my code: public TreeMap<Integer, List<String>> destinationsPerNSeats() { return requests. stream(). collect(Collectors.groupingBy(Request::getnSeats, TreeMap::new, Collectors.mapping(Request::getDestName, Collectors.toList()))). } Input : TreeMap<Integer, List<String>> map = mgr.destinationsPerNSeats(); print(map); Output : {4=[Paris], 3=[London, Berlin, Berlin], 2=[Paris]} Output expected : {6=[Berlin, Paris], 3=[London]} How can I solve this? Thanks! What did you use as Input? – Glains 8 mins ago ...

Why is the base type not returned when no generic type specified? [duplicate]

Why is the base type not returned when no generic type specified? [duplicate] This question already has an answer here: I have a code like this: public class A<T extends String> { T field; List<T> fields; public T getField() { return field; } public List<T> getFields() { return fields; } public static void test(){ A a = new A(); String s = a.getField(); List<String> ss = a.getFields(); } } Why when creating A with no generic type getFiled returns String but getFileds returns List<Object> ? A getFiled String getFileds List<Object> I have to define A as A<String> a = new A<>() for this to work properly. A A<String> a = new A<>() Thanks, This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. By not specifying any generic type, you are decl...