Wednesday, 31 July 2019

java completable promise example

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.concurrent.*;
class Main {
public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<String> whatsYourNameFuture = CompletableFuture.supplyAsync(()-> {
         
           // public String get() {
              System.out.println("in promise");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new IllegalArgumentException(e);
                }
                return "Rajeev";
           // }
        });


        CompletableFuture<String> promiseExample = CompletableFuture.supplyAsync(()-> {
            //public String get() {
              System.out.println("in promise");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new IllegalArgumentException(e);
                }
                return "Kumar";
           // }
        });

      CompletableFuture<String> result = whatsYourNameFuture.thenCombine(promiseExample, (str1, str2) -> {
        String name=str1 + " " + str2;
        return name;
        });
        System.out.println("Value- " + result.get());
        //System.out.println(welcomeText.get());
    }
}

completable feature example

String length differnce in php for utf-8 char.

<?php
$name="Si vous n'arrivez pas à récupérer";
echo strlen($name);
echo mb_strlen($name,'UTF-8');
?>

Monday, 15 July 2019

java stream api manupalation for arraylist of integer to comma sepertaed

import java.util.*;
import java.util.stream.Collectors;
class Main {
  public static void main(String[] args) {
   List<String> strings = new LinkedList<>();
     strings.add("Java");strings.add("is");
     strings.add("cool");
     String message = String.join(", ", strings);
     System.out.println(message);

      List<Integer> num = new LinkedList<>();
     num.add(2);
     num.add(5);
     num.add(7);
     String message1 =num.stream().map((gameId)-> gameId.toString()).collect(Collectors.joining(","));
     System.out.println(message1);
     //message returned is: "Java is cool"

  }
}
https://www.baeldung.com/java-string-with-separator-to-list