Search This Blog

Loading...

Saturday, January 24, 2015

How to Generate Random Number between 1 to 10 - Java Example

There are many ways to generate random numbers in Java e.g. Math.random() utility function, java.util.Random class or newly introduced ThreadLocalRandom and SecureRandom, added on JDK 1.7. Each has there own pros and cons but if your requirement is simple, you can generate random numbers in Java by using Math.random() method. This method returns a psuedorandom positive double value between 0.0 and 1.0, where 0.0 is inclusive and 1.0 is exclusive. It means Math.random() always return a number greater than or equal to 0.0 and less than 1.0. Internally it uses java.util.Random class. So when you first call this method, it creates instance of Random class and cache it for future use. Any further call is just equivalent of Random.nextDouble(). If your requirement is more sophisticated i.e. you need random numbers between a range or multiple threads needs to generate random number simultaneously, then you should look other random solution available in Java. Since Math.random() method is properly synchronized to ensure correct value is returned when used by multiple thread, it also become bottleneck when multiple thread simultaneously uses it. To solve this problem, JDK 1.7 introduces ThreadLocalRandom class, which allows each thread to keep their own psuedo random number to reduce contention. In a scalable environment, ThreadLocalRandom can improve performance significantly as it keeps the instance of random number generator in a ThreadLocal variable to reduce contention. If security is your concern then you have another option in terms of SecureRandom, which provides a cryptographically strong random number generator.

Thursday, January 22, 2015

How To Convert Map to List in Java with Example

Before converting a Map to a List in Java,  we should be very clear about these data structures which is widely  used in Java. So lets begin with Map. What is Map? Map is an Interface in Java which store key and value object. It's Java representation of popular hash table data structure which allows you to search an existing element in O(1) time, at the same time also makes insertion and removal easier.  We use key object to retrieve the value object by using hashing functionality provided by Map. As we have seen in how get method of HashMap works, In Java, equals() and hashcode() method are integral part of storing and retrieving object from it. Map allows duplicate values but no duplicate keys. Map has its implementation in various classes like HashMap, ConcurrentHashMap and TreeMap. The Map interface also provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings. Now let's understand what is a List in Java, It is also an interface to provide an ordered collection based on index. Elements can be inserted and deleted using positions and duplicate elements are allowed in the list. There are multiple implementation of List is available in Java e.g. ArrayList, which is based in array and LinkedList which is based in linked list data structure. Now let's see a Java program which is used to convert a Map to List.

Friday, January 16, 2015

Second Highest Salary in MySQL and SQL Server - LeetCode Solution

Write a SQL query to get the second highest salary from the Employee table.

+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+

For example, given the above Employee table, the second highest salary is 200. If there is no second highest salary, then the query should return NULL. You can write SQL query in any of your favorite database e.g. MySQL, Microsoft SQL Server or Oracle. You can also use database specific feature e.g. TOP, LIMIT or ROW_NUMBER to write SQL query, but you must also provide a generic solution which should work on all database. In fact, there are several ways to find second highest salary and you must know couple of them e.g. in MySQL without using LIMIT keyword, in SQL Server without using TOP and in Oracle without using RANK and ROWNUM. Once you solve the problem, Interviewer will most likely increase the difficulty level by either moving to Nth salary direction or taking away this buit-in utilities.

Java67 Headline Animator