Search This Blog

Loading...

Tuesday, September 1, 2015

Top 10 RESTful Web Service Interview Questions for Java Developers

REST is an architectural style of developing web services which has become immensely popular in last couple of years and consequently gained lot of importance in core Java and Java EE interviews. If you are a Java web developer then you are most likely see couple of questions from web services every time you go for a Java web developer interview. One of the most frequently one is difference between REST and SOAP web services, which I have recently answered there, but there are lot many other good questions I have collected from friends and my readers, which I have not yet published. In this article, I am sharing those questions, mainly based upon REST style web services for your practice and preparation. Some of them are easy to answer and you will find them either in my blog or by doing Google but couple of them is really interesting and challenging and required real solid experience in Java web service domain to answer. I leave it to you guys for now and will probably update this post with answers in near future. If you don't find answer of any REST interview question then I suggest you to take a look at these two books, Java Programming Interview Exposed and RESTful Web Services, you will most likely find your answer there.

Monday, August 31, 2015

How to convert list to array in Java and vice-versa

There is no easy way to convert an array to list in Java, but you can easily convert a list into array by calling toArray() method, which List inherit from Collection interface. If you solely rely on core JDK, then only way to convert an array to list is looping over array and populating list one element at a time. But if you can use open source libraries like Google Guava or Apache Commons lang then there is many utility classes to convert list to array and vice-versa, as shown in this tutorial. If you are working on Java application, you will often need to convert between list and array. A list is nothing but a dynamic array which knows how to re-size itself when it gets full or get close to full. List uses load factor to decide when to re-size, default value of its is 0.75. When they re-size, list usually double their slots e.g. goes from 16 to 32 etc. You can find these nifty details in their implementation classes e.g. ArrayList is one of the popular list in Java which provides order and random access. BTW, if you want to truly master Java Collection framework, then you must read Java Generics and Collection book, written by Maurice Naftaline and one of the must read book to become expert on Java Collections framework.

Saturday, August 29, 2015

How to randomize elements in List in Java using shuffle method

java.util.Collections class provides shuffle() method which can be used to randomize object stored in a List in Java. Since List is an ordered collection and maintains the order on which objects are inserted into it, you may need to randomize elements if you need them in different order. Collections.shuffle() method uses default randomness to randomize element but you also has an overloaded version of shuffle() to provide an instance of  java.util.Random object, which can be used to randomize elements. Since this method except a List, you can also pass it to LinkedList, VectorCopyOnWriteArrayList and others, which doesn't implement RandomAccess method. In such cases, this method convert list to array before shuffling to avoid quadratic performance by shuffling sequential access list. Once shuffling is done it also converts back array to list. Shuffling has many usage e.g. shuffling deck of cards in a poker game simulation. You can also use shuffling to roll dice if you are developing any board game which requires dice e.g. Ludo.