Playing Around With Frequently Changing String Tokenizer

Playing Around With Frequently Changing String Tokenizer

Recently I was working on one of my Project Module. I had to use some data from the other module and they were sending this in the form of String Tokens, like "element_1;element_2;element_3;element_4;". So, as normal I did the normal tokenize code and break this tokens to use the values. Now they changed the token order, so I would have to do all the rework.
After that I came up with some solutions so that my code rework can be minimised even if the  token orders are changed. So I did the following:
1) Add all the tokens in the List. Let’s say, in the tokenizer parser method, just add all the token in the List.
2) Do not write any core logic in the token parser method.
3) Now create a enum or any such structure which will hold the token position. In my case I use the enum.
public enum ElementPositionEnum {
       ELEMENT_1(0), ELEMENT_2(1), ELEMENT_3(2), ELEMENT_4(3);
       private int position;
       ElementPositionEnum(int position) {
              this.position = position;
       public int getPosition() {
              return this.position;
4) Now while creating the object or setting the value to my object, just fetch the elements from the List based upon the Enum position.
                E.g. If I have to fetch the value,  I will do it like this:
                String myVal = List.get(MyElementPositionEnum.ELEMENT_1.getPosition());
5) So, even if the tokens sequence is changed, then  we only need to change the enum's value. Rest of the code will be same.
Java Code for Same :
public class TokenizerParser {
       public TokenElementTO parseToken(String token)
              List listOfTOken = new ArrayList();
              String[] tokens = token.split(";");
              for(String val : tokens)
              return createTokenElementObject(listOfTOken);
        private TokenElementTO createTokenElementObject(List listOfTOken)           {
              TokenElementTO to = new TokenElementTO();
              if(listOfTOken.size() > ElementPositionEnum.ELEMENT_1.getPosition())
              if(listOfTOken.size() > ElementPositionEnum.ELEMENT_2.getPosition())
              if(listOfTOken.size() > ElementPositionEnum.ELEMENT_3.getPosition())
              if(listOfTOken.size() > ElementPositionEnum.ELEMENT_4.getPosition())
               return to;
       public static void main(String[] args)
              TokenizerParser main = new TokenizerParser();
              String tokens = "AAA;BBB;CCC;DDDD";
public class TokenElementTO {
       private String element_1;
       private String element_2;
       private String element_3;
       private String element_4;
       public String getElement_1() {
              return element_1;
       public void setElement_1(String element_1) {
              this.element_1 = element_1;
       public String getElement_2() {
              return element_2;
       public void setElement_2(String element_2) {
              this.element_2 = element_2;
       public String getElement_3() {
              return element_3;
       public void setElement_3(String element_3) {
              this.element_3 = element_3;
       public String getElement_4() {
              return element_4;
       public void setElement_4(String element_4) {
              this.element_4 = element_4;

Create JavaOutOfMemory Crash Dump

Create JavaOutOfMemory Crash Dump:

Sometime Java Application crashes due to out of memory exception. If we don't have the proper logging mechanism, we won't be able to find out why the application has crashed. We can use the Jconsole or Jprofiler to debug the memory leak. But if our application crashes after running for 3-4 hours, it will be difficult to debug without having proper dump.
Java provides some useful arguments, using which we can create the core dump using very few arguments.
We can add below arguments in the JVM to get the dump:
                java -ms32m -mx512m -XX:+HeapDumpOnOutOfMemoryError.
By adding the above argument, when the application crashes, It will create the dump with name : java_pid.hprof


Implement an algorithm to determine if a string has all unique characters. With and without using extra additional data structures

* Implement an algorithm to determine if a string has all unique characters.
* With and without using extra additional data structures?
@author Sonu Mishra
public class StringContainsUniqueCharacter {
* Without using extra DS.
*  Ignoring int variable. Time complexity is
* O(n), where n is the length of the string, and space complexity is O(n).
* We can reduce our space usage a little bit by using a bit vector.We will
* assume, in the below code, that the string is only lower case ‘a’ through
* ‘z’.This will allow us to use just a single int
@param str: String to check
@return : true/false

Not Accessible Due To Restriction On Required Library

Not Accessible Due To Restriction On Required Library

Today, I was working on some java project in my favourite Eclipse editor, when I encountered some unknown problem. It was throwing some error that the calss cannot be accessed due to restriction on required library. This type of error occurs when some classes cannot be loaded into the projects since some restriction rules are being imposed on those classes. Similarly, I got the error like this:

How To solve this Exception:

As this error is coming because of some setting where we are excluding some files. So we need to change that settings. For that, click on the 'Windows' --> 'Preference' and then go to 'Java'-->Compiler-->'Error/Warnings' tab. You will find 'Forbidden reference (access rules):' and its value has been set to 'Error'. So we only need to do is, to change the 'Error' to 'Warning', and all is done.

Swapping the Variables Without Using Third Variables

Swapping the Variables Without Using Third Variables Using Java

In this post, we will see, how to swap the variables. There are different ways to swap the variables. The major swap algorithms are:
1) Swapping using the third varaibles.
2) Swapping using the Bitwise Operator. This is only for the Integer. This doesn't use the third variables.
3) Swapping using the Arithmetic Operations. This is also, only for the Integer. This also, doesn't use the
    third variables.
4) Swapping the Strings without using the third variables. This is little bit tricky. The logic is similar to the
    above one, i.e. Arithmetic Operations. But the Strings are immutable in the Java, so, externally we are not
    using the third variables, but internally, this uses the third variables.

Below, we have the Java Program to discuss all the above methods in details.

* This class will reverse the two variables.
* Here we will see how to swap the variables using or without using the third variables.
* @author Sonu Mishra
public class SwapVariables {
* Swapping using the third variable and without the third variables.
* Swap two variables using the temporary variables or the third variable.
* @param a The first variable.
* @param b The Second variable.

Basic TreeNode Structure in Java using Generics

Basic TreeNode Structure Using Generics

This is the basic block for creating the binary tree. We generally call this block or the structure as a Node. So this is how a single node is created. This in itself is a tree and when we pile the number of nodes, it become a big tree.

Here is the code, to create the TreeNode.

* Basic Structure of the Tree. Or the Single unit of a tree.
* @author Sonu Mishra
* @param Type of the Node for the tree. Like. String, Integer
TreeNode< E extends Comparable<? super E>>
public TreeNode< E > left;
public TreeNode< E > right;
E value;
public TreeNode(TreeNode< E > left, TreeNode< E > right, E value) {
this.left = left;
this.right = right;
this.value = value;
* If a node doesn't have left and right child, it is leaf node.
* @param node check whether this node is leaf node or not.
* @return either true or false