Sunday, 25 December 2016

Find Non Repetitive Character in String using java

package it.com.shift.plugin.issue;

import java.util.HashMap;
import java.util.LinkedHashMap;

public class FindNonRepetitvieCharecter {

public static void main(String[] args) {
// TODO Auto-generated method stub
        String name ="krishknaraois";
        System.out.println(printFirstNonRepetingCharacterIterative(name));    
        System.out.println(printFirstNonRepetingCharacterUsingJava(name));
        System.out.println(printFirstNonRepetingCharacterUsingJavaSinglePass(name));
}

private static char printFirstNonRepetingCharacterUsingJavaSinglePass(String name) {
for(char value : name.toCharArray()){
if(name.indexOf(value)==name.lastIndexOf(value)){
return value;
}
     }
return ' ';
}

//Minimum complexity n and maximum complexity n^2
private static char printFirstNonRepetingCharacterUsingJava(String name) {
HashMap<Character,Integer> characters = new LinkedHashMap<Character,Integer>();
for (char literal : name.toCharArray()) {
characters.put(literal, characters.containsKey(literal)?characters.get(literal)+1:1);
}
   for (Character value : characters.keySet()) {
if(characters.get(value)==1){
return value;
}
}
return ' ';
}

//Minimum complexity n and maximum complexity n^2
private static char printFirstNonRepetingCharacterIterative(String name) {
// TODO Auto-generated method stub
for(int i=0;i<name.length();i++){
        for(int j=i+1;j<name.length();j++){
        if(name.charAt(i)== name.charAt(j)){
        break;
        }else if(j+1==name.length()){
        return name.charAt(i);
        }
        }
       }
return ' ';
}

}

Thursday, 22 December 2016

Jquery color box to display help information

<html>
<head>
<title>Page Title</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/themes/dot-luv/jquery-ui.css">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.colorbox/1.6.4/jquery.colorbox.js"></script>
<script type="text/javascript">
$( document ).ready(function() {
$("#helpButton").click(function(){
//Initiate color box
 $('#help').colorbox({
    inline : true,
    overlayClose : false,
    maxWidth : '520px',
    maxHeight : '400px',
    width : '95%',
    height : '95%',
    open:true,
    href: "#help"
   });
  });
  });
</script>
</head>
<body>
<a href="#" id="helpButton" class="fa fa-info-circle" >Color Box demo</a>
 
  <div style="display: none;">
       <div id="help">
        <p>
   <span style="font-size: 20px; float: left;">Help</span>
  </p>
    </div>
</div>

</body>
</html>

Output :- 



Dynamic html rendering using handler bars

<!DOCTYPE html>
<html>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/2.0.0/handlebars.js"></script>

//Declare the template here 
<script id="movieTemplate" type="text/x-handlebars-template">
<table style="width:100%">
  <tr>
    <th>ReleaseYear</th>
    <th>Director</th>
  </tr>
  {{#each this.movies}}
  <tr>
    <td>{{ReleaseYear}}</td>
    <td>{{Director}}</td>
  </tr>
  {{/each}}
</table>
</script>

<body>
 //Final data will be rendered here
<div id="results"></div>
</body>
<script type="text/javascript">
var data = {}
var movies = [
{ ReleaseYear: "1998", Director: "Francois Girard" },
{ ReleaseYear: "1999", Director: "Stanley Kubrick" },
{ ReleaseYear: "1976", Director: "Mauro Bolognini" }
];
data.movies=movies;
 // Grab the template script
var html = $( "#movieTemplate" ).html();

// Compile the template
var theTemplate = Handlebars.compile(html);

// Pass our data to the template
var theCompiledHtml = theTemplate(data);

//Assign to html div
$('#results').html(theCompiledHtml);
</script>
</html>

Wednesday, 14 December 2016

Java8 new features :- Consumer interface and start of lambda expression

Consumer  interface :- Below you can observe different ways of implementing interface and how consumer interface is used to implement lambda expressions.

interface A {

public void show();
}

class B implements A {

@Override
public void show() {
System.out.println("Typical way of show");
}

}

public class LambdaDemo {

public static void main(String[] args) {
//Approach1 of implementing interface
B b = new B();
b.show();

//Approach2 of implementing interface
new A() {
public void show() {
System.out.println("smart way of show :-"+(1*0.1==0.1));
}
}.show();

//Approach3 of implementing using consumer interface
A obj;
obj = () -> {
System.out.println("show lambda");
};
obj.show();

A obj1;
obj1 = () -> System.out.println("show lambda without braces");
obj1.show();

}

}

Jquery template to dynamicly render html

Jquery Template :- Is utilized to construct html dynamically by passing json

<!DOCTYPE html>
<html>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js"></script>
//Declare the template here 
<script id="movieTemplate" type="text/x-jquery-tmpl">
<table style="width:100%">
  <tr>
    <th>ReleaseYear</th>
    <th>Director</th>
  </tr>
  {{each data.movie}}
  <tr>
    <td>${ReleaseYear}</td>
    <td>${Director}</td>
  </tr>
  {{/each}}
</table>
</script>

<body>
 //Final data will be rendered here
<div id="results"></div>
</body>
<script type="text/javascript">
var data = {}
var movies = [
{ ReleaseYear: "1998", Director: "Francois Girard" },
{ ReleaseYear: "1999", Director: "Stanley Kubrick" },
{ ReleaseYear: "1976", Director: "Mauro Bolognini" }
];
data.movie=movies;
//Invocation of template
var html = $( "#movieTemplate" ).tmpl( data ).html();
$('#results').html(html);
</script>
</html>

Custom single threaded java server

 package com.diffengine.csv; import java.io.*; import java.net.*; import java.util.Date; public class Server { public static void main(Str...