November 28, 2017

Permutation of String - Java code interview questions

package com.belazy.uber;
import java.util.HashSet;
import java.util.Set;

/**
 * Permutations for AAC are:
[AAC, ACA, CAA]
 * @author monica spear
 *
 */
public class PermutationString {
    public static Set<String> permutationFinder(String str) {
        Set<String> perm = new HashSet<String>();
        //Handling error scenarios
        if (str == null) {
            return null;
        } else if (str.length() == 0) {
            perm.add("");
            return perm;
        }
        char initial = str.charAt(0); // first character
        String rem = str.substring(1); // Full string without first character
        Set<String> words = permutationFinder(rem);
        for (String strNew : words) {
            for (int i = 0;i<=strNew.length();i++){
                perm.add(charInsert(strNew, initial, i));
            }
        }
        return perm;
    }

    public static String charInsert(String str, char c, int j) {
        String begin = str.substring(0, j);
        String end = str.substring(j);
        return begin + c + end;
    }

    public static void main(String[] args) {
        String s = "AAC";
        String s1 = "ABC";
        String s2 = "ABCD";
        System.out.println("\nPermutations for " + s + " are: \n" + permutationFinder(s));
       System.out.println("\nPermutations for " + s1 + " are: \n" + permutationFinder(s1));
       System.out.println("\nPermutations for " + s2 + " are: \n" + permutationFinder(s2));
    }
}

November 20, 2017

Rest call to Java Json sample program

Getting JSON Response from Rest Call in java

Description

This is a rest service call in java. The rest service which returns josn as response is saved to Java Object through Object Mapper API. The Object Mapper is very simple to use


/**
 *  Json to Java using Object Mapper
 */
package com.belazy.rest;

import java.io.File;
import java.io.IOException;
import java.net.URL;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author itsection kozhikode
 *
 */
public class JsonMapper {

/**
* @param args
* @throws IOException
* @throws JsonMappingException
* @throws JsonGenerationException
*/
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
// TODO Auto-generated method stub

ObjectMapper objectMapper = new ObjectMapper();
Car car = new Car("bmw","blue");
File resultFile = new File("d://belazy.json");
objectMapper.writeValue(resultFile, car);

ComputerDetails comp = objectMapper.readValue(new URL("http://ip.jsontest.com"), ComputerDetails.class);
System.out.println(comp.getIp());

JsonKey jsonkey = objectMapper.readValue(new URL("http://echo.jsontest.com/key/value/one/two"), JsonKey.class);
System.out.println("jsonkey.getOne() : "+jsonkey.getOne());

}





}


computerdetails.class




/**
 *
 */
package com.belazy.rest;

/**
 * @author belazy
 *
 */
public class ComputerDetails {

String ip;

/**
* @return the ip
*/
public String getIp() {
return ip;
}

/**
* @param ip the ip to set
*/
public void setIp(String ip) {
this.ip = ip;
}


}

jsonkey.class


package com.belazy.rest;
public class JsonKey
{
    private String one;

    private String key;

    public String getOne ()
    {
        return one;
    }

    public void setOne (String one)
    {
        this.one = one;
    }

    public String getKey ()
    {
        return key;
    }

    public void setKey (String key)
    {
        this.key = key;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [one = "+one+", key = "+key+"]";
    }
}

java api needed



jackson annotation jar
jackson core
jackson databind



Output



86.99.219.5
jsonkey.getOne()two

November 19, 2017

How to deal with SSL Handshake exception

/**
 * Consuming a Rest application
 */
package com.belazy.rest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author belazy
 *
 * This program I dedicate to my Tech Arc
 * she wish be a data scientist
 * Hope she will achieve it in the nearest days.
 *
 */
public class RestConsumer {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub

String theUrl = "https://jsonplaceholder.typicode.com/posts";

try {
URL url = new URL(theUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");

System.out.println(conn.getResponseCode());

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}


/**
 * 
 */
package com.belazy.rest;

/**
 * @author belazy
 *
 */
public class Post {
private String userId;
private String id;
private String title;
private String body;

/**
* @return the userId
*/
public String getUserId() {
return userId;
}

/**
* @param userId the userId to set
*/
public void setUserId(String userId) {
this.userId = userId;
}

/**
* @return the id
*/
public String getId() {
return id;
}

/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}

/**
* @return the title
*/
public String getTitle() {
return title;
}

/**
* @param title the title to set
*/
public void setTitle(String title) {
this.title = title;
}

/**
* @return the body
*/
public String getBody() {
return body;
}

/**
* @param body the body to set
*/
public void setBody(String body) {
this.body = body;
}

}





Exception in thread "main" javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Unknown Source)
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.recvAlert(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
at com.belazy.rest.RestConsumer.main(RestConsumer.java:38)

November 08, 2017

Understanding garbage collection in java

Understanding garbage collection in java





https://stackify.com/memory-leaks-java/

http://javabelazy.blogspot.in/

November 04, 2017

Comparing Java Webservices JAX-WS with Spring WS

Conclusion

 Considering contract-first web services, my little experience has driven me to choose JAX-WS in favor of Spring WS: testing benefits do not balance the ease-of-use of the standard. I must admit I was a little surprised by these results since most Spring components are easier to use and configure than their standard counterparts, but results are here.
  Link
 http://javabelazy.blogspot.in/

Facebook comments