Friday, June 29, 2018

Reverse the number !!

public class MathsSol {
    public static void main(String args[]) {
        int a = 4958;
        reverseNumber(a);
    }
    public static void reverseNumber(int num) {
        int remainder = 0;
        while (num != 0) {
            remainder = remainder * 10 + num % 10;
            num = num / 10;
        }
        System.out.println(remainder);
    }
}

Output:
8594

WAP to convert an array to number !

Problem statement: wap to convert an array to number.
public class ConvertArrayToNumber {
    public static void main(String args[]) {
        int a[] = {5, 7, 3, 8, 1};
        int a1[] = {5, 7, 3, 8, 1, 4, 4};

        //Test cases
        if (Integer.parseInt(convert(a)) == 57381)
            System.out.println("pass");
        else            System.out.println("Fail");
        if (Integer.parseInt(convert(a1)) == 5738144)
            System.out.println("pass");
        else            System.out.println("Fail");
    }
    public static String convert(int a[]) {
        String temp = "";
        for (int i = 0; i < a.length; i++) {
            temp = temp + a[i];
        }
        return temp;
    }
}

Output: 
pass
pass

public class ConvertArrayToNumber {
    public static void main(String args[]) {
        int a[] = {5, 7, 3, 8, 1};
        int a1[] = {5, 7, 3, 8, 1, 4, 4};
        if (convert(a) == 57381)
            System.out.println("pass");
        else            System.out.println("Fail");
        if (convert(a1) == 5738144)
            System.out.println("pass");
        else            System.out.println("Fail");
    }
    public static int convert(int a[]) {
        String str = a[0] + "";
        int total = 0;
        for (int i : a) {
            total = total * 10 + i;
        }
        return total;
    }
}


Thursday, June 28, 2018

IntelliJ IDEA Shortcuts !!

  • Delete whole line in IntelliJ IDE - CTRL + Y
  • Format code in IntelliJ - CTRL + ALT + L

IntelliJ IDEA and Eclipse Shortcuts
EclipseIntelliJ IDEADescription
ctrl+shift+mctrl+alt+Vcreate local variable refactoring
syso ctrl+spacesout ctrj+jSystem.out.println(“”)
alt + up/downctrl + shift + up/downmove lines
ctrl + dctrl + ydelete current line

EclipseIntelliJ IDEADescription
F4ctrl+hshow the type hierarchy
ctrl+alt+gctrl+alt+F7find usages
ctrl+shift+uctrl+f7finds the usages in the same file
alt+shift+rshift+F6rename
ctrl+shift+rctrl+shift+Nfind file / open resource
ctrl+shift+x, jctrl+shift+F10run (java program)
ctrl+shift+octrl+alt+oorganize imports
ctrl+octrl+F12show current file structure / outline
ctrl+shift+mctrl+alt+Vcreate local variable refactoring
syso ctrl+spacesout ctrj+jSystem.out.println(“”)
alt + up/downctrl + shift + up/downmove lines
ctrl + dctrl + ydelete current line
???alt + hshow subversion history
ctrl + hctrl + shift + fsearch (find in path)
“semi” set in window-> preferencesctrl + shift + enterif I want to add the semi-colon at the end of a statement
ctrl + 1 or ctrl + shift + lctrl + alt + vintroduce local variable
alt + shift + salt + insertgenerate getters / setters
ctrl + shift + fctrl + alt + lformat code
ctrl + yctrl + shift + zredo
ctrl + shift + cctrl + /comment out lines (my own IDEA shortcut definition for comment/uncomment on german keyboard layout on laptop: ctrl + shift + y)
ctrl + alt + hctrl + alt + h (same!)show call hierarchy
none ?ctrl + alt + f7to jump to one of the callers of a method
ctrl + shift + ialt + f8evaluate expression (in debugger)
F3ctrl + bgo to declaration (e.g. go to method)
ctrl + lctrl + ggo to line

Fizz Buzz

Problem statement: 
Write a program that outputs the string representation of numbers from 1 to n. But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

import java.util.List;
class Solution {
    public static List<String> fizzBuzz(int n) {
        List<String> str = null;
        for (int i =1; i <= n; i++) {
            if (i % 3 == 0 && i % 5 == 0) {
                System.out.println("FizzBuzz");
            }else  if (i% 3 == 0) {
                System.out.println("Fizz");
            }else  if (i% 5 == 0) {
                System.out.println("Buzz");
            }else{
                System.out.println(i);
            }
            //return str;        }
        return str;
    }
    public static void  main(String args[]){
        fizzBuzz(15);
    }

Output:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz

Java 8 Stream API implementation

  1. import java.util.*;
  2. import java.util.stream.Collectors;

  3. public class TestExe1 {
  4. public static void main(String[] args) {

  5.         List<String> myList1 =
  6.                 Arrays.asList("a1", "a1","a1","a2", "b1", "c2", "c1");

  7.         List<String> teamIndia = Arrays.asList("i1", "i2", "i3");
  8.         List<String> teamAustralia = Arrays.asList("a1", "a2", "a3");
  9.         List<String> teamEngland = Arrays.asList("Alex", "Bell", "Broad");
  10.         List<String> teamNewZeland = Arrays.asList("Kane", "Nathan", "Vettori");
  11.         List<String> teamSouthAfrica = Arrays.asList("AB", "Amla", "Faf");
  12.         List<String> teamWestIndies = Arrays.asList("Sammy", "Gayle", "Narine");
  13.         List<String> teamSriLanka = Arrays.asList("Mahela", "Sanga", "Dilshan");
  14.         List<String> teamPakistan = Arrays.asList("Misbah", "Afridi", "Shehzad");

  15.         List<List<String>> playersInWorldCup2016 = new ArrayList<>();
  16.         playersInWorldCup2016.add(teamIndia);
  17.         playersInWorldCup2016.add(teamAustralia);
  18.         playersInWorldCup2016.add(teamEngland);
  19.         playersInWorldCup2016.add(teamNewZeland);
  20.         playersInWorldCup2016.add(teamSouthAfrica);
  21.         playersInWorldCup2016.add(teamWestIndies);
  22.         playersInWorldCup2016.add(teamSriLanka);
  23.         playersInWorldCup2016.add(teamPakistan);

  24. // Let's print all players before Java 8

  25. List<String> listOfAllPlayers = new ArrayList<>();
  26. for(List<String> team : playersInWorldCup2016){
  27. for(String name : team){
  28.                 listOfAllPlayers.add(name);
  29.             }
  30.         }
  31.         System.out.println("Players playing in world cup 2016");
  32.         System.out.println(listOfAllPlayers);

  33.         List<String> myList =
  34.                 Arrays.asList("a1", "a1","a1","a2", "b1", "c2", "c1");
  35. // Now let's do this in Java 8 using FlatMap

  36. List<String> flatMapList = playersInWorldCup2016.stream()
  37.                 .flatMap(x -> x.stream())
  38.                 .collect(Collectors.toList());

  39.         System.out.println("List of all Players using Java 8"); System.out.println(flatMapList);

  40.         myList
  41.                 .stream()
  42.                 .filter(s1 -> s1.startsWith("c"))
  43.                 .map(String::toUpperCase)
  44.                 .sorted()
  45.                 .skip(1)
  46.                 .forEach(System.out::println);

  47.         System.out.println("Using Java 7: ");

  48. // Count empty strings
  49. List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
  50.         System.out.println("List: " +strings);
  51. long count = getCountEmptyStringUsingJava7(strings);

  52.         System.out.println("Empty Strings: " + count);
  53.         count = getCountLength3UsingJava7(strings);

  54.         System.out.println("Strings of length 3: " + count);

  55. //Eliminate empty string
  56. List<String> filtered = deleteEmptyStringsUsingJava7(strings);
  57.         System.out.println("Filtered List: " + filtered);

  58. //Eliminate empty string and join using comma.
  59. String mergedString = getMergedStringUsingJava7(strings,", ");
  60.         System.out.println("Merged String: " + mergedString);
  61.         List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);

  62. //get list of square of distinct numbers
  63. List<Integer> squaresList = getSquares(numbers);
  64.         System.out.println("Squares List: " + squaresList);
  65.         List<Integer> integers = Arrays.asList(1,2,13,4,15,6,17,8,19);

  66.         System.out.println("List: " +integers);
  67.         System.out.println("Highest number in List : " + getMax(integers));
  68.         System.out.println("Lowest number in List : " + getMin(integers));
  69.         System.out.println("Sum of all numbers : " + getSum(integers));
  70.         System.out.println("Average of all numbers : " + getAverage(integers));
  71.         System.out.println("Random Numbers: ");

  72. //print ten random numbers
  73. Random random = new Random();

  74. for(int i = 0; i < 10; i++) {
  75.             System.out.println(random.nextInt());
  76.         }

  77.         System.out.println("Using Java 8: ");
  78.         System.out.println("List: " +strings);

  79.         count = strings
  80.                 .stream()
  81.                 .filter(v -> !(v.isEmpty()))
  82.                 .count();
  83.         System.out.println("Empty Strings: " + count);

  84.         count = strings.stream().filter(string -> string.length() == 3).count();
  85.         System.out.println("Strings of length 3: " + count);

  86.         filtered = strings.stream().filter(string ->!string.isEmpty()).collect(Collectors.toList());
  87.         System.out.println("Filtered List: " + filtered);

  88.         mergedString = strings.stream().filter(string ->!string.isEmpty()).collect(Collectors.joining(", "));
  89.         System.out.println("Merged String: " + mergedString);

  90.         squaresList = numbers.stream().map( i ->i*i).distinct().collect(Collectors.toList());
  91.         System.out.println("Squares List: " + squaresList);
  92.         System.out.println("List: " +integers);

  93.         IntSummaryStatistics stats = integers.stream().mapToInt((x) ->x).summaryStatistics();

  94.         System.out.println("Highest number in List : " + stats.getMax());
  95.         System.out.println("Lowest number in List : " + stats.getMin());
  96.         System.out.println("Sum of all numbers : " + stats.getSum());
  97.         System.out.println("Average of all numbers : " + stats.getAverage());
  98.         System.out.println("Random Numbers: ");

  99.         random.ints().limit(10).sorted().forEach(System.out::println);

  100. //parallel processing
  101. count = strings.parallelStream().filter(string -> string.isEmpty()).count();
  102.         System.out.println("Empty Strings: " + count);
  103.     }

  104. private static int getCountEmptyStringUsingJava7(List<String> strings) {
  105. int count = 0;

  106. for(String string: strings) {

  107. if(string.isEmpty()) {
  108.                 count++;
  109.             }
  110.         }
  111. return count;
  112.     }

  113. private static int getCountLength3UsingJava7(List<String> strings) {
  114. int count = 0;

  115. for(String string: strings) {

  116. if(string.length() == 3) {
  117.                 count++;
  118.             }
  119.         }
  120. return count;
  121.     }

  122. private static List<String> deleteEmptyStringsUsingJava7(List<String> strings) {
  123.         List<String> filteredList = new ArrayList<String>();

  124. for(String string: strings) {

  125. if(!string.isEmpty()) {
  126.                 filteredList.add(string);
  127.             }
  128.         }
  129. return filteredList;
  130.     }

  131. private static String getMergedStringUsingJava7(List<String> strings, String separator) {
  132.         StringBuilder stringBuilder = new StringBuilder();

  133. for(String string: strings) {

  134. if(!string.isEmpty()) {
  135.                 stringBuilder.append(string);
  136.                 stringBuilder.append(separator);
  137.             }
  138.         }
  139.         String mergedString = stringBuilder.toString();
  140. return mergedString.substring(0, mergedString.length()-2);
  141.     }

  142. private static List<Integer> getSquares(List<Integer> numbers) {
  143.         List<Integer> squaresList = new ArrayList<Integer>();

  144. for(Integer number: numbers) {
  145.             Integer square = new Integer(number.intValue() * number.intValue());

  146. if(!squaresList.contains(square)) {
  147.                 squaresList.add(square);
  148.             }
  149.         }
  150. return squaresList;
  151.     }

  152. private static int getMax(List<Integer> numbers) {
  153. int max = numbers.get(0);

  154. for(int i = 1;i < numbers.size();i++) {

  155.             Integer number = numbers.get(i);

  156. if(number.intValue() > max) {
  157.                 max = number.intValue();
  158.             }
  159.         }
  160. return max;
  161.     }

  162. private static int getMin(List<Integer> numbers) {
  163. int min = numbers.get(0);

  164. for(int i= 1;i < numbers.size();i++) {
  165.             Integer number = numbers.get(i);

  166. if(number.intValue() < min) {
  167.                 min = number.intValue();
  168.             }
  169.         }
  170. return min;
  171.     }

  172. private static int getSum(List numbers) {
  173. int sum = (int)(numbers.get(0));

  174. for(int i = 1;i < numbers.size();i++) {
  175.             sum += (int)numbers.get(i);
  176.         }
  177. return sum;
  178.     }

  179. private static int getAverage(List<Integer> numbers) {
  180. return getSum(numbers) / numbers.size();
  181.     }

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...