Thursday, September 12, 2019

Multipart File upload in java using okHttp and HttpClient

OkHTTP:
There are several ways to upload the file. OKHTTP is a one of a  client to call 
an api to upload file to the server.






HTTPCLIENT:
We can also use HTTP Client to write an api client to upload files to server.







Saturday, June 22, 2019

Basic spring boot setup and running simple hello world page in Intellij Community Edition Spring boot MVC

1) Install Spring Assistant Plugin for intellij.
2) Create Web project  with Spring Web Starter.
3) add dependency in pom file:
     <dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
   <scope>compile</scope>
</dependency>
<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
</dependency>

4) add following in application.properties file in src/main/resources
       spring.mvc.view.prefix: /WEB-INF/jsp/
       spring.mvc.view.suffix: .jsp

5) Create controller class named SpringJava4sController.java


       import org.springframework.stereotype.Controller;
       import org.springframework.ui.Model;
       import org.springframework.web.bind.annotation.*;
       import org.springframework.web.servlet.ModelAndView;

       @RestController       public class SpringJava4sController {

      @RequestMapping(value="/test", method=RequestMethod.GET)
       public ModelAndView showLoginPage(Model model) {
       model.addAttribute("message", "Welcome to Java4s Spring Boot Tutorials");

       return new ModelAndView("welcomePage");
       }
     }


6) Make sure we have class with main as 
     import org.springframework.boot.SpringApplication;
     import org.springframework.boot.autoconfigure.SpringBootApplication;

     @SpringBootApplication     public class DemoApplication {

     public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
    }


7) create webapp/WEB-INF/jsp folder within src/main
8) place jsp file welcomePage inside jsp as:

    <html  xmlns:th="http://www.thymeleaf.org">
     <body>

   
    ${message}

   
    </body>
   </html>


9) go to browsrer and see localhost:8080/test


 

Monday, June 17, 2019

Java program to calculate subset in o(n) time complexity using ArratList


  1. Brutforce:

      1. for(int i = 0; i < len; i++) {  
      2.             //This loop adds the next character every iteration for the subset to form and add it to the array  
      3.             for(int j = i; j < len; j++) {  
      4.                 arr[temp] = str.substring(i, j+1);  
      5.                 temp++;  
      6.             }  
      7.         }  


Subset can be implemented with the concept of decision tree:


How we can implement decision tree in code is:





CODE in JAVA:



Thursday, June 13, 2019

Java Program to check federal holidays in US using ZonedDateTime API java8

This is the program n=in java where it checks whether the given zoneddatetime is holidays or not.
It uses java8 ZonedDateTime to determine the zoned date and time.


Code to check whether a given date falls on weekend or holidays

Wednesday, March 20, 2019

Returning the permutation of the string to find the number just greater number formed by the given digits in java

Problem:

Find the number  just greater than given number  which is formed by the available digits?

eg. if number is 86435, the number just greater than 86435 must be 86453.

Solution1:

public class QuestionB {
    public static ArrayList<String> getPerms(String remainder) {
        int len = remainder.length();
        ArrayList<String> result = new ArrayList<String>();

        /* Base case. */        if (len == 0) {
            result.add(""); // Be sure to return empty string!  
          return result;
        }


        for (int i = 0; i < len; i++) {
            /* Remove char i and find permutations of remaining characters.*/   
         String before = remainder.substring(0, i);
            String after = remainder.substring(i + 1, len);
            ArrayList<String> partials = getPerms(before + after);

            /* Prepend char i to each permutation.*/        
            for (String s : partials) {
                result.add(remainder.charAt(i) + s);
            }
        }

        return result;
    }

    public static void main(String[] args) {
        ArrayList<String> list = getPerms("abc");
        System.out.println("There are " + list.size() + " permutations.");
        for (String s : list) {
            System.out.println(s);
        }
    }

}

Solution2:

public class GeneralizedSolution {
  public static void main(String[] args) {
    String s = "86435";
//    System.out.println("\nString " + s + ":\nPermutations: " + Permutation(s)); 
 Set<Integer> set = Permutation(s).stream().map(a -> Integer.parseInt(a))
.collect(Collectors.toSet());
    List<Integer> list = new ArrayList<>(set);
    Collections.sort(list);
//    System.out.println(list);    int index = list.indexOf(Integer.parseInt(s));
    if (index == list.size() - 1) {
      System.out.println("there is no number greatet than " + s);
    } else {
      System.out.println("The number just greater than " + s + "is :" + list.get(index + 1));

    }
  }

  public static Set<String> Permutation(String str) {
    Set<String> Result = new HashSet<String>();
    if (str == null) {
      return null;
    } else if (str.length() == 0) {
      Result.add("");
      return Result;
    }

    char firstChar = str.charAt(0);
    String rem = str.substring(1);
    Set<String> words = Permutation(rem);
    for (String newString : words) {
      for (int i = 0; i <= newString.length(); i++) {
        Result.add(CharAdd(newString, firstChar, i));
      }
    }
    return Result;
  }

  public static String CharAdd(String str, char c, int j) {
    String first = str.substring(0, j);
    String last = str.substring(j);
    return first + c + last;
  }

}


solution 3rd:
of permutation with finding permutation with making first element constant and
finding permutation of remaining:


class Permutations
{
 // Recursive function to generate all permutations of a String
 private static void permutations(String candidate, String remaining)
 {
  if (remaining.length() == 0) {
   System.out.println(candidate);
  }

  for (int i = 0; i < remaining.length(); i++)
  {
   String newCandidate = candidate + remaining.charAt(i);

   String newRemaining = remaining.substring(0, i) +
          remaining.substring(i + 1);

   permutations(newCandidate, newRemaining);
  }
 }

 // Find Permutations of a String in Java
 public static void main(String[] args)
 {
  String s = "ABC";
  permutations("", s);
 }
}

Sunday, March 17, 2019

Get key with maximum value in hashmap in java

static String findMax(HashMap<String,Integer> hm){
    Map.Entry<String,Integer> maxEntry = null;
    for(Map.Entry<String ,Integer>entry: hm.entrySet()){
        if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) >= 0)
        {
            maxEntry = entry;
        }


    }
    return maxEntry.getKey();