Tuesday 11 August 2015

How To Store UTF-8 Character in MySql


What is UTF-8 ?



  • UTF-8 & UTF 16 are Unicode Character Sets.
  • UTF-8 is the preferred encoding for e-mail and web pages. 
  • UTF-8 is variable-length and uses 8-bit code units.
  • UTF-8 is backwards compatible with ASCII.


How to handle with mysql?

First of all check character set and collation of  your database,to execute following query :

SHOW VARIABLES LIKE "character_set_database";

Variable_name                 Value
-----------------------------------------------    
character_set_database      latin1


SHOW VARIABLES LIKE "collation_database";

Variable_name                     Value          
-----------------------------------------------------------
collation_database            latin1_swedish_ci

OR you can also execute this query : SHOW VARIABLES LIKE  'char%';

As a result you get result like this:




  • So here default character set is UTF 8 so no big bloom..if not then whenever you create table set CHARSET=utf8 and Collation = utf8_general_ci

Let us create one table :

CREATE TABLE product (
  id bigint(20) NOT NULL AUTO_INCREMENT,
  name varchar(50) NOT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8


If you have already created table then execute following alter query

ALTER TABLE product 
MODIFY `name` VARCHAR(255) CHARACTER SET utf8 NOT NULL DEFAULT ''

Now you can insert or update utf-8 character easily..enjoy..lots..!!!!



Tuesday 4 August 2015

Java Collection Set : Difference between Hashset and Treeset

Java Collection

  • A collection is a group of data manipulate as a single object. 
  • Collections are primarily defined through a set of interfaces.
  • Interfaces are used of flexibility reasons
    • Programs that uses an interface is not tightened to a specific implementation of a collection.
    • It is easy to change or replace the underlying collection class with another (more efficient) class that implements the same interface.
  • HashSet and TreeSet implement the interface Set.
  • HashSet is much faster than TreeSet (constant-time versus log-time for most operations like add, remove and contains) but offers no ordering guarantees like TreeSet.
  • HashSet
    • Class offers constant time performance for the basic operations (add, remove, contains and size).
    • It does not guarantee that the order of elements will remain constant over time.
    • Iteration performance depends on the initial capacity and the load factor of the HashSet.
    • It's quite safe to accept default load factor but you may want to specify an initial capacity that's about twice the size to which you expect the set to grow.
  • TreeSet
    • Guarantees log(n) time cost for the basic operations (add, remove and contains) .
    • Guarantees that elements of set will be sorted (ascending, natural, or the one specified by you via its constructor) (implements SortedSet).
    • Doesn't offer any tuning parameters for iteration performance
    • Offers a few handy methods to deal with the ordered set like first(), last(), headSet(), and tailSet() etc.
  • Important points:
    •  Both guarantee duplicate-free collection of elements.
    •  It is generally faster to add elements to the HashSet and then convert the collection to a TreeSet for a duplicate-free sorted traversal.
    •  None of these implementation are synchronized. That is if multiple threads access a set concurrently, and at least one of the threads modifies the set, it must be synchronized externally.
    • LinkedHashSet is in some sense intermediate between HashSet and TreeSet. Implemented as a hash table with a linked list running through it, however it provides insertion-ordered iteration which is not same as sorted traversal guaranteed by TreeSet.
  • So choice of usage depends entirely on your needs but I feel that even if you need an ordered collection then you should still prefer HashSet to create the Set and then convert it into TreeSet.
  • e.g. SortedSet<String> s = new TreeSet<String>(hashSet);

Monday 3 August 2015

Big Oh notation

  • For run time complexity analysis we use big Oh notation extensively so it is vital that you are familiar with the general concepts to determine which is the best algorithm for you in certain scenarios. 
  • We have chosen to use big Oh notation for a few reasons, the most important of which is that it provides an abstract measurement by which we can judge the performance of algorithms without using mathematical proofs.
The following list explains some of the most common big Oh notations : 



  • O(1) constant: the operation doesn't depend on the size of its input, e.g. adding a node to the tail of a linked list where we always maintain a pointer to the tail node.

  •  O(n) linear: the run time complexity is proportionate to the size of n

  •  O(log nlogarithmic: normally associated with algorithms that break the problem into smaller chunks per each invocation, e.g. searching a binary search tree.

  • O(n log njust n log n : usually associated with an algorithm that breaks the problem into smaller chunks per each invocation, and then takes the results of these smaller chunks and stitches them back together, e.g. quick sort.
  • (n^2quadratic: e.g. bubble sort.
  • O(n^3) cubic: very rare.
  • O(2n) exponential: incredibly rare.



  • If you encounter either of the latter two items (cubic and exponential) this is really a signal for you to review the design of your algorithm. 
  • While prototyping algorithm designs you may just have the intention of solving the problem irrespective of how fast it works. We would strongly advise that you always review your algorithm design and optimize where possible|particularly loops recursive calls|so that you can get the most efficient run times for your algorithms.
  • Taking a quantitative approach for many software development properties will make you a far superior programmer - measuring one's work is critical to success.

Thursday 16 July 2015

In Java to encode Base64 in Java


  • Base64 is a straight forward encoding for binary data into readable characters.
  • The Java class is the javax.xml.bind.DatatypeConverter, which is part of the XML package (JAXB) and has a method called printBase64Binary() that takes in a byte array and returns a encoded BASE64 string.
  • The following is a code snippet that encoding the user name and password for BASIC Authentication for Web applications.
  • It combines a user name and corresponding password with “:” as delimiter, and converts it to an byte array that can be feed into the printBase64Binary(). Once the encoded string is ready, just set it as value to the Authorization header. 

The following code is how to do the encoding and set it as a HTTP header in request to server:
     
import javax.xml.bind.DatatypeConverter;

String encoding = 
DatatypeConverter.printBase64Binary((username + ":" + password).getBytes("UTF-8"));
  
urlConn.setRequestProperty("Authorization", "Basic " + encoding);


Depending the client library, your API to set the header could be different. For example, if you use Apache Thrift’s HTTP transport, the code may look like the following:

import org.apache.thrift.transport.THttpClient;
 
transport.setCustomHeader("Authorization", "Basic " + encoding);

Sunday 28 June 2015

How to calculate difference of date and time in Java


To calculate date and time difference with manual calculation...

  • Very first step to convert date into milliseconds (ms)
    • String date1 = "01-02-2015 09:15:20";
    • SimpleDateFormat format = new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      • HH convert into 24 hours format (0-23)
      • hh convert into 12 hours format
    •  Date d1 = format.parse(date1);
    • long ms = d1.getTime();


Thursday 25 June 2015

Angular JS Directice in IE8


  • The most powerful feature Directive in Angular JS. 
  • Allow you to create custom reusable components. 
  • Few exception are there...keep in mind..!!!
  • With IE8 Angular Js should work consistent like other browser.
  • Four ways to create directive
    1. Angular JS viz. Attribute  (Common One)
    2. Element (Common One)
    3. Class
    4. Comment
  •  Let take one example :
    • Your directive is like : <div my-js-control>....</div>
    • Now use this directive in IE8
    • Copy following syntax and paste it in your HTML/JSP page.
<Html>
    <head>

   <!--[if lte IE 8]>
      <script>
        document.createElement('div-my-js-control');
      </script>
    <![endif]-->

   </head>
</html>


  • This document.createElement() creates an element called 'my-js-control' which can use in application.
  • If you create more than one directive in your system then you need to create that many new elements for older browser
  • Long story short : It is preferable to use Angular JS directive in IE8 as attributes.




Friday 3 April 2015

public class A {
   public static void main(String[] args) {
      

     Integer i1=100; Integer i2=100;
     System.out.println(i1==i2);//true
    

     Integer i3=200; Integer i4=200;
    System.out.println(i3==i4);//false ??
   }
}


Conclusion :

  • The Integer wrapper class interns the objects from -127 to +128. So all Integers with a value less than 128 are the same as they are interned. The values greater than 128 are not the same because they are not interned.
  • The JVM will create a cache of objects(something similar to the string constant pool) for the above range and assigns the same object(if it already exists) when ever a new object with the same value is created. So in your case, the two Integers for 100 will use the same object and the ones for 200 won't be the same.
  • you assign a integral literal to a Integer reference, it will invoke the Integer.valueOf(..) method.
  • This method uses the famous Fly-weigh pattern. That is that the -128~127 integers are cached. So the "i1==i2" will be true. Of course, you can change the cache range by setting IntegerCache.low or IntegerCache.high which are static variables.
  • Always compare wrapper classes with equals(e).