Search This Blog

Loading...

Saturday, January 31, 2015

Java 8 Map Function Examples

Map is a well known functional programming concept which is incorporated into Java 8. Map is a function defined in java.util.stream.Streams class, which is used to transform each element of stream. Because of this property you can use Map in Java 8 to transform a Collection, List, Set or Map. For example, if you have a list of String and you want to convert all of them into upper case, how will you do this? Prior to Java 8, there is no function to do this. You had to iterate through List using a for loop or foreach loop and transform each element. In Java 8, you get stream, which allow you to apply many functional programming operator like map, reduce and filter. By using Map function you can apply any function to every element of Collection. It can be any predefined function or a user defined function. You not only can use lambda expression but also you can also use method references. Some examples of Map in Java 8 is to convert a list of integers and then square of each number. Map function is also an intermediate operation and it return a stream of transformed element. Stream API also provides methods like mapToDouble(), mapToInt(), and mapToLong() which returns DoubleStream, IntStream and LongStream, which are specialized stream for double, int and long data types. You can collect the result of transformation by using Collectors class, which provides several method to collect the result of transformation into List, Set or any Collection.

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.

Java67 Headline Animator