Difference between Stack and Queue Data Structure in Java? Example

Stack and Queue are two of the important data structures in the programming world and have a variety of usage. As opposed to the array and linked list, which are considered as a primary data structures, they are secondary data structures that can build using an array or linked list. You can use Stack to solve recursive problems and Queue can be used for ordered processing. The difference between Stack and Queue Data structure is also one of the common questions not only in Java interviews but also in C, C++, and other programming job interviews.

How Garbage Collection works in Java

I have read many articles on Garbage Collection in Java, some of them are too complex to understand and some of them don’t contain enough information required to understand garbage collection in Java. Then I decided to write my own experience as an article. You can call it a tutorial about garbage collection in simple words, which would be easy to understand and have sufficient information to understand how garbage collection works in Java. Garbage collection works by employing several GC algorithms e.g. Mark and Sweep. There are different kinds of garbage collectors available in Java to collect different areas of heap memory e.g. you have serial, parallel, and concurrent garbage collectors in Java.

5 ways to Convert Java 8 Stream to List - Example, Tutorial

One of the common problems while working with Stream API in Java 8 is how to convert a Stream to List in Java because there is no toList() method present in Stream class. When you are processing a List using Stream's map and filter method, you ideally want your result in some collection so that you can pass it to other parts of the program. Though java.util.stream.Stream class has toArray() method to convert Stream to Array, but there is no similar method to convert Stream to List or Set. Java has a design philosophy of providing conversion method between new and old API classes e.g. when they introduced Path class in JDK 7, which is similar to java.io.File, they provided a toPath() method to File class.

2 Examples of Streams with Collections in Java 8

Finally, Java 8 is here, after more than 2 years of JDK 7, we have a much expected Java 8 with lots of interesting features. Though Lambda expression is the most talked item of the coming Java 8 release, it wouldn't have been this much popular, if Collections were not improved and Stream API was not introduced. Java 8 is bringing on new Streams API java.util.stream package, which allows you to process elements of Java Collections in parallel. Java is inheritably sequential and there are no direct means to introduce parallel processing at the library level, stream API is going to fill that gap. By using this, you can filter elements of collection on a given criterion e.g. if you have a list of orders, you can filter buy orders with sell orders, filter orders based upon there quantity and price, and so on.

Difference between First and Second Level Cache in Hibernate

If you have used Hibernate in past then you know that one of the strongest points of the Hibernate framework is caching, which can drastically improve the performance of Java application's persistence layer if configured and used correctly. Hibernate provides caching at many levels e.g. first level cache at Session level, second-level cache at the SessionFactory level, and query cache to cache frequently executed SQL queries. The first level cache minimizes database access for the same object. For example, if you call the get() method to access Employee object with id = 1 from one session, it will go the database and load the object into memory, but it will also cache the object in the first-level cache.

How to Convert an Array to Comma Separated String in Java

The simplest way to convert an array to comma separated String is to create a StringBuilder, iterate through the array, and add each element of the array into StringBuilder after appending comma. You just need Java 1.5 for that, even if you are not running on Java 5, you can use StringBuffer in place of StringBuilder. The joining of String has got even easier in JDK 8, where you have got the join() method right in the String class itself. The join() method takes a delimiter and a source, which can be array or collection and returns a String where each element is joined by a delimiter.

10 Hibernate Interview Questions and Answers for Java Developers

Hibernate Interview Questions are asked on Java J2EE Interviews, mostly for web-based enterprise application development role. Success and acceptability of Hibernate framework on Java world have made it one of the most popular Object Relational Mapping (ORM) solution in the Java technology stack. Hibernate frees you from database-specific coding and allows you to focus more on utilizing powerful object-oriented design principles to implement core business logic. By using Hibernate you can switch between database rather easily and also take advantage of out of box caching facilities provided by Hibernate, in terms of second-level cache and query cache.

How to Clone Collection in Java - Deep copy of ArrayList and HashSet

Programmers often mistook copy constructors provided by various collection classes, as a means to clone Collection e.g. List, Set, ArrayList, HashSet, or any other implementation. What is worth remembering is that copy constructor of Collection in Java only provides shallow copy and not a deep copy, which means objects stored in both original List and cloned List will be same and point to the same memory location in Java heap. One thing, which adds into this misconception is shallow copy of Collections with Immutable Objects. Since Immutable can't be changed, It's Ok even if two collections are pointing to same object. This is exactly the case of String contained in pool, update on one will not affect other.

Java 8 Date Time - 20 Examples of LocalDate, LocalTime, LocalDateTime

Along with lambda expressions, streams, and several minor goodies, Java 8 has also introduced brand new Date and Time API, and in this tutorial, we will learn how to use Java 8 Date Time API with simple how-to-do task examples. Java's handling of Date, Calendar, and Time is long been criticized by the community, which is not helped by Java's decision of making java.util.Date mutable and SimpleDateFormat not thread-safe. It seems, Java has realized a need for better Date and time support, which is good for a community which already used to of Joda Date and Time API.

Top 50 Java Thread Interview Questions Answers for Experienced

You go to any Java interview, senior or junior, experience or freshers,  you are bound to see a couple of questions from the thread, concurrency, and multi-threading. In fact, this built-in concurrency support is one of the strongest points of Java programming language and helped it to gain popularity among the enterprise world and programmers equally. Most of the lucrative Java developer position demands excellent core Java multi-threading skills and experience in developing, debugging, and tuning high-performance low latency concurrent Java applications. This is the reason, it is one of the most sought after skills in Java interviews. The multithreading and concurrency are also hard to master the concept and only good developers with solid experience can effectively deal with concurrency issues.

How to Implement Stack in Java Using Array and Generics - Example

The stack is one of the popular data structure which supports LIFO (Last In First OUT) operation. Due to the LIFO advantage, you can use a stack data structure to convert a recursive algorithm to an iterative one. The stack data structure is very easy to implement using an array or linked list, but you don't have to implement it on your own because Java already provides a Stack implementation in java.util.Stack class. This class is a subclass of the Vector class and you should use it whenever you need Stack for your production code, there is no point in inventing the wheel again when the focus is on developing your application.

Difference between map() and flatMap() in Java 8 Stream

The map() and flatmap() are two important operations in new functional Java 8. Both represent functional operation and they are also methods in java.util.stream.Stream class. The key difference between map() and flatmap() function is that when you use a map(), it applies a function on each element of stream and stores the value returned by the function into a new Stream. This way one stream is transformed into another e.g. a Stream of String is transformed into a Stream of Integer where each element is the length of the corresponding Stream.

130+ Java Interview Questions Answers for 2 to 7 Year Experienced Programmers

Last Updated Date: 2nd March 2018
Time is changing and so is Java interviews. Gone are the days, when knowing the difference between String and StringBuffer can help you to go through the second round of interview, questions are becoming more advanced and interviewers are asking more deep questions. When I started my career, questions like Vector vs Array and HashMap vs Hashtable were the most popular ones and just memorizing them gives you a good chance to do well in interviews, but not anymore. Nowadays, you will get questions from the areas where not many Java programmer looks e.g. NIO, patterns, sophisticated unit testing or those which are hard to master e.g. concurrency, algorithms, data structures and coding.