Tuesday, 21 April 2015

Loop Optimization in java

Loop Optimization I found it very interesting thing that will increase the java looping performance
===============================================================
public loopA()
 {
     String str = “abcdefghijklmnopqurstuvwxyz”; 
    
       for (int j = 0; j < str.length(); j++) {
       }
  }
====================================================
public loopB()
 {
  String str = “abcdefghijklmnopqurstuvwxyz”;
  int len = str.length();
  for (int j = 0; j < len; j++)
   {
//This is because the length of the string is not calculated for each iteration
//through the loop. 
  }
}
====================================================
LooB is 200% faster than loopA.


This is because the length of the string is not calculated for each iteration
through the loop. The method call to str.length() is avoided by setting the results to an integer prior to entering the loop.

Thursday, 26 March 2015

How hash map works if it has same key of strings

it was great discussion with my friend over working of hash-map with the same key.

The question was.
Does hash map store the same key? if yes then

What happens when a duplicate key is put into a HashMap?

Here is example of store ing duplicate key guess the output?

import java.util.*;
public class MyHashMap {
 public static void main(String args[]){
 
  HashMap<String,String> hm=new HashMap<String,String>();

  hm.put("Vipul","1000");
  hm.put("Vipul","2000");
  hm.put("Vipul","3000");

 
  System.out.println(hm.get("Vipul"));
  for(Map.Entry m:hm.entrySet()){
   System.out.println(m.getKey()+" "+m.getValue());
  }
 }
}

ANS Is

Vipul 3000

--------------------------------------------------------------------------------------------
now it is getting more complex....?

What will be size of hash-map in this case?
Ans: ONE
--------------------------------------------------------------------------------------------
import java.util.*;
public class MyTestHashMap {
 public static void main(String args[]){
 
  HashMap<String,String> hm=new HashMap<String,String>();

  hm.put("Vipul","1000");
  hm.put("Vipul","2000");
  hm.put("Vipul","3000");

 
  System.out.println(hm.get("Vipul"));
  for(Map.Entry m:hm.entrySet()){
   System.out.println(hm.size+" "+m.getKey()+" "+m.getValue());
  }
 }
}

There are some more code as well I will add soon
In case your finding is different please comment.


Friday, 30 January 2015

Onclick get image tag value in html

<html>
<head>
<title>img</title>
 <script>
 function myFunction() {
     var catImage = document.getElementById("catImage"),
    catImageValue = document.getElementById("catImageValue");
//alert("I am an catImageValue box!"+ catImageValue);

catImageValue.innerHTML = "Value = " + catImage.getAttribute("value");
   
}

 </script>
 </head>
  Click on this
 <img id="catImage" src="http://i.imgur.com/Mmfl8.png" width="100px" value="1" />
<div id="catImageValue"></div>
<button onclick="myFunction()">Try it</button>
 </body>
</html>

VIEW DEMO HERE MY Fiddle

http://jsfiddle.net/vipulg/povnd0t2/
DEMO

sybase add column

Begin

ALTER TABLE Order_def ADD shared char(3) DEFAULT 'N' NOT NULL CONSTRAINT CHK_shared CHECK (shared IN ("Yes","No","-") )
End
GO


Wednesday, 28 January 2015

How do I remove objects from an array in Java

How do I remove objects from an array in Java


I have array of object type how to remove the object from it

i have class StockExchange 
     and i have array of the same class

  StockExchange[] t= new StockExchange[]{TSE,HKSE,NYSE};

now i want to remove the element from this array 

Here is full example how i removed.
-----------------------------------------------------------------------------
class StockExchange{
    public boolean isClosed() {
return false;
}
--------------------------------------------------------------------------------
public class ObjectRemovalTest {
  
    public static void main(String args[])
    {
    StockExchange TSE = new StockExchange(){
            
            @Override
            public boolean isClosed() {
                return true;
            }         
       };
     
       StockExchange HKSE = new StockExchange(){

            @Override
            public boolean isClosed() {
                return true;
            }         
       };
      
       StockExchange NYSE = new StockExchange(){

            @Override
            public boolean isClosed() {
                return false;
            }         
       };
      

       
    StockExchange[] t= new StockExchange[]{TSE,HKSE,NYSE};
        System.out.println("T"+Arrays.toString(t));
        Set<StockExchange> asSet = new HashSet<StockExchange>(Arrays.asList(t));                   asSet.remove(TSE);
        t = asSet.toArray(new StockExchange[] {});
        System.out.println(Arrays.toString(t));
   
    }

Wednesday, 21 January 2015

convert int to string in java

convert int to string in java

Use String.valueOf(id);

int id =2;

String name = String.valueOf(id);

Monday, 19 January 2015

field xxxxx doesn have a default value

field xxxxxx doesn have a default value

drop the table schema or update the schema using hibernate of jpa option

<property name="hibernate.hbm2ddl.auto" value="update" />