Posts

Showing posts from April, 2022

Working of HashMap in Java

Image
Working of HashMap in Java What is Hashing It is the process of converting an object into an integer value. The integer value helps in indexing and faster searches. What is HashMap HashMap is a part of the Java collection framework. It uses a technique called Hashing. It implements the map interface. It stores the data in the pair of Key and Value. HashMap contains an array of the nodes, and the node is represented as a class. It uses an array and LinkedList data structure internally for storing Key and Value. There are four fields in HashMap. Before understanding the internal working of HashMap, you must be aware of hashCode() and equals() method. equals():  It checks the equality of two objects. It compares the Key, whether they are equal or not. It is a method of the Object class. It can be overridden. If you override the equals() method, then it is mandatory to override the hashCode() method. hashCode():  This is the method of the object class. It returns the memory refere...

Java 8 Features

  Java 8 Features Oracle released a new version of Java as Java 8 in March 18, 2014. It was a revolutionary release of the Java for software development platform. It includes various upgrades to the Java programming, JVM, Tools and libraries. Java 8 Programming Language Enhancements Java 8 provides following features for Java Programming: Lambda expressions, Method references, Functional interfaces, Stream API, Default methods, Base64 Encode Decode, Static methods in interface, Optional class, Collectors class, ForEach() method, Nashorn JavaScript Engine, Parallel Array Sorting, Type and Repating Annotations, IO Enhancements, Concurrency Enhancements, JDBC Enhancements etc. Lambda Expressions Lambda expression helps us to write our code in functional style. It provides a clear and concise way to implement SAM interface(Single Abstract Method) by using an expression. It is very useful in collection library in which it helps to iterate, filter and extract data. Method References Java...

Postman form-data sending complex object with file

  @PostMapping(value = "/evaluate", produces = "application/json") public ResponseEntity<?> sendEvaluateForm( @RequestParam ( "client" ) String client, @RequestParam(value = "files", required = false) MultipartFile files) throws IOException { ObjectMapper mapper = new ObjectMapper (); Client clientobject = mapper.readValue(client, Client.class); return ResponseEntity.ok().build(); }

CORS issue - Response to preflight request doesn't pass access control check:

  I am using React with Spring JWT faced similar issue as blocked by CORS policy . added  http.cors()  in my SecurityConfigurer class to resolve @EnableWebSecurity public class SecurityConfigurer extends WebSecurityConfigurerAdapter { @Override protected void configure( HttpSecurity http) throws Exception { http.cors().and().csrf().disable()....... } } And Write some lines of inside Spring boot application: @Bean public WebMvcConfigurer configure() { return new WebMvcConfigurer() { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**").allowedOrigins("*").allowedMethods("*").allowedHeaders("Acces-Control-Allow-Origin","*"); } }; }

Java Interview Coding Questions:

1) How To Reverse a String without using reverse method:     public class Reverse {     public static void main ( String [] args ) {         String str = "STRING" ;         char [] chr = str . toCharArray ();         for ( int i = chr . length - 1 ; i >= 0 ; i --) {             System . out . print ( chr [ i ]);         }             } }   2) Swap two numbers without using third variable:  public class Swap {         public static void main ( String [] args ) {         int a = 11 ;         int b = 12 ;         //logic         b = b - a ;         a = a + b ;         b = a - b ;         System . out . println ( "value of a is " + a ); ...