Monday, August 8, 2016

H2Database grammer Blog

Insertion Sort and Selection Sort

String to Integer conversion in JavaScript

http://stackoverflow.com/questions/1133770/how-do-i-convert-a-string-into-an-integer-in-javascript
http://jsbin.com/vavotujoge/edit?html,js,output
========================================================
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<div id="rizvi">
  <input type="textbox" id="Tue" />
  <input type="textbox" id="Wed" />
  <input type="textbox" id="Thu" />
  <input type="textbox" id="Sat" />
  </div>
</body>
</html>

=======================================================
var textbox = document.getElementById('Tue');
textbox.value = ("00000000" + "12345678").slice(-8);

var textbox1 = document.getElementById('Wed');
textbox1.value = parseInt("010", 10);

var textbox2 = document.getElementById('Thu');
textbox2.value = parseInt("00010101", 10);

var textbox3 = document.getElementById('Sat');
var str = "000000000000000034566789900233";       // string
var number = str*1;   // number
textbox3.value = number;
=======================================================
Output:
12345678
10
10101
34566789900233

How do I convert a string into an integer in JavaScript?

Try parseInt function:
var number = parseInt("10");
But there is a problem. If you try to convert "010" using parseInt function, it detects as octal number, and will return number 8. So, you need to specify a radix (from 2 to 36). In this case base 10.
parseInt(string, radix)
Example:
var result = parseInt("010", 10) == 10; // Returns true

var result = parseInt("010") == 10; // Returns false

If the radix parameter is omitted, JavaScript assumes the following:
  • If the string begins with "0x", the radix is 16 (hexadecimal)
  • If the string begins with "0", the radix is 8 (octal). This feature is deprecated
  • If the string begins with any other value, the radix is 10 (decimal)
Note: Only the first number in the string is returned!
Note: Leading and trailing spaces are allowed.
Note: If the first character cannot be converted to a number, parseInt() returns NaN.
Note: Older browsers will result parseInt("010") as 8, because older versions of ECMAScript, (older than ECMAScript 5, uses the octal radix (8) as default when the string begins with "0". As of ECMAScript 5, the default is the decimal radix (10).

Resource Link:
  1. How do I convert a string into an integer in JavaScript?
  2. JavaScript parseInt() Function

Wednesday, August 3, 2016

Java HashSet and TreeSet

It says that hashset in java doesn't mantain an order but looking at my program
public static void main(String[] args) {
    HashSet<Integer> io=new HashSet<Integer>();

    Integer io1=new Integer(4);
    Integer io2=new Integer(5);
    Integer io3=new Integer(6);

    io.add(io2);
    io.add(io3);
    io.add(io1);

    System.out.println(io);
}
and execute it it give me an ordered set everytime i run it. Why this is happening?
Another question is: if i implement a treeset (like i did in the previous program but instead of hashset using treeset and intead of Integer using my class) i have to implement compareto ?

Answer by Eran:

HashSet doesn't maintain order, but it has to iterate over the elements in some order when you print them. HashSet is backed by a HashMap, which iterates over the elements in the order of the bins in which they are stored. In your simple example, 4,5,6 are mapped to bins 4,5,6 (since the hashCode of an integer is the value of the integer) so they are printed in ascending order.
If you tried to add 40,50,60 you would see a different order ([50, 40, 60]), since the default initial number of bins is 16, so the hash codes 40,50,60 will be mapped to bins 40%16 (8),50%16 (2) ,60%16 (12), so 50 is the first element iterated, followed by 50 and 60.
As for TreeSet<SomeCostumClass>, you can either implement Comparable<SomeCostumClass> in SomeCostumClass, or pass a Comparator<SomeCostumClass> to the constructor.

When to prefer TreeSet over HashSet

1.  Sorted unique elements are required instead of unique elements.The sorted list given by TreeSet is always in ascending order.

2.   TreeSet has greater locality than HashSet.

If two entries  are near by in the order , then TreeSet places them near each other in data structure and hence in memory, while HashSet spreads the entries all over memory  regardless of the keys they are associated to. 
     
As we know Data reads from the hard drive takes much more latency time than data read from the cache or memory. In case data needs to be read from hard drive than prefer TreeSet as it has greater locality than HashSet.

3. TreeSet uses Red- Black tree algorithm underneath to sort out the elements. When one need to perform read/write operations frequently , then TreeSet is a good choice.