Skip to main content

Posts

Showing posts from February, 2023

Eureka Server and Client

 Server Code @ SpringBootApplication @ EnableEurekaServer public class DiscoveryAppApplication {     public static void main( String [] args) {         SpringApplication.run(DiscoveryAppApplication.class, args);     } } application.properties server.port= 8761 eureka.client.register-with-eureka= false eureka.client.fetch-registry= false Client Code @ SpringBootApplication public class AppApplication {     @ Bean     @ LoadBalanced     public Builder getWebClient() {         return WebClient.builder();     }     public static void main( String [] args) {         SpringApplication.run(AppApplication.class, args);     } } application.properties spring.application.name=data-fetcher server.port= 8081 eureka.client.serviceUrl.defaultZone= ${EUREKA_URI : http : //localhost:8761/eureka} eureka.instance.preferIpAddress : true

Java Multi-Threading

 Callable with Future  Future can be used along with callable to get return from the threads execution. public String getDataWithGET( String url, int i) {         System.out.println( "Stated" + i);         WebClient web = WebClient.create(url);         String x = web.get().exchangeToMono(             t -> t.statusCode().equals(HttpStatusCode.valueOf( 200 )) ? t.bodyToMono(String.class) : t.createError())             .block();     System.out.println(x);     return x; } void getData() {     try {         ExecutorService exe = Executors.newFixedThreadPool( 10 );         ArrayList<Future<String>> listFuture = new ArrayList<Future<String>>();         for (int i = 0 ; i < 20 ; i++) {             int num...

Java 8 Features

Java 8 Features  Interface Default and Static Methods Method References  Functional Interfaces and Lambda Expressions Java Stream API for Bulk Data Operations on Collections Optional Class Java Time API StringJoiner Java Parallel Array Sorting forEach() method in Iterable interface Collection API improvements Concurrency API improvements Java IO improvements Collectors Class Base64 Encoder Interface Default and Static Methods interface Example{         static String sayHello() {         System.out.println( "Hello" );         return "Hello" ;     }     //can be overridden.     default String sayBye() {         return "bye" ;     } } Method References Reference to a Static Method Syntax :-  ContainingClass::methodName. numList.forEach(System.out :: println); Reference to an Instance Method syntax containingInstance::methodName. FuncExample x = s ::...

Stream API

Stream API Count List < Integer > list = Arrays.asList( 10 , 5 , 3 , 9 , 5 , 4 , 6 ); long count = list.stream().count(); Reduce long sum = list.stream().reduce( 0 , (a,b) -> a+b); // Returns the sum int sum  = list.stream().reduce( 0 ,Integer :: sum); long facto = list.stream().reduce( 1 , (a,b) -> a*b); // Returns the factorial long evenSum = list.stream().reduce( 0 , (t, u) -> (u % 2 == 0 ) ? t + u : t); // Sum of even numbers BinaryOperator < Integer > binOp = new BinaryOperator < Integer >() {     @ Override     public Integer apply( Integer previous, Integer current) {     if (current % 2 == 0 ) {         return previous + current;     }         return previous;     } }; long count = list.stream().reduce( 0 , binOp); System.out.println(count); int min = list.stream().reduce(list.get( 0 ),Integer :: min); int max = list.stream().reduce(list.ge...

Collection API

Collection Interface  Included Signatures size() isEmpty() contains(Object) iterator() toArray() toArray( T []) toArray( IntFunction < T []>) Example System.out.println(list); Student [] s = list.toArray( Student [] ::new ); System.out.println(s); for ( Student x : s) {     System.out.println(x); } add(E) remove(Object) containsAll( Collection < ? >) addAll( Collection < ? extends E>) removeAll( Collection < ? >) removeIf( Predicate < ? super E>) retainAll( Collection < ? >) clear() equals(Object) hashCode() spliterator() stream() parallelStream() Collections Class Sorting  Natural Order Sorting  List < Integer > list = Arrays.asList( 4 , 3 , 8 , 6 , 9 , 8 , 10 ); Collections.sort(list); System.out.println(list); Custom Comparator Sorting Comparator < Integer > com = (o1, o2) -> o1 < o2 ? - 1 : 1 ; Collections.sort(list,com); System.out.println(list); Reverse Sorting Collections.reverse(list); System...