HTML

How to use flatMap() in Java 8 - Stream Example Tutorial

In order to understand the flatMap() method, you first need to understand the map() function of Java 8. The map() function is declared in  the java.util.stream.Stream class and uses to transform one Stream into another, for example, it can convert a stream of integer numbers into another stream of ints where each element is the square of the corresponding element in the source stream. In the map() operation, a function is applied to each element of the source Stream and return values are inserted into a new Stream which is returned to the caller. The key point to note here is that the function used by map() operation returns a single value.

Top 20 Essential Java Interview Questions with Answers for Freshers with 1 to 2 years Experienced

If you are grad looking for an internship position in a company which uses Java, or a fresher, just out from college and looking out for Java development position, then this post is for you. I have attended, taken, and participated in a different level of Java interviews. I can say there is a vast difference between them, which is not surprising because you obviously want to ask different level questions based upon whether the candidate is fresher or experienced. Similarly, the problem at phone interview, written test, and face to face interviews, keeping this in mind, and give you always have a limited amount of time before going for an interview, It's essential to prepare the right set of questions.

Merge Sort in Java - Algorithm Example and Tutorial

The merge sort algorithm is a divide and conquers algorithm. In the divide and conquer paradigm, a problem is broken into smaller problems where each small problem still retains all the properties of the larger problem -- except its size. To solve the original problem, each piece is solved individually; then the pieces are merged back together. For example, imagine you have to sort an array of 200 elements using the bubble sort algorithm. Since selection sort takes O(n^2) time, it would take about 40,000-time units to sort the array. Now imagine splitting the array into ten equal pieces and sorting each piece individually still using selection sort. Now it would take 400-time units to sort each piece; for a grand total of 10*400 = 4000.

Counting Sort in Java - Example

The Counting sort algorithm, like Radix sort and Bucket sort, is an integer based algorithm (i.e. the values of the input array are assumed to be integers), non-comparison and linear sorting algorithm. Hence counting sort is among the fastest sorting algorithms around, in theory. It is also one of the few linear sorting algorithms or O(n) sorting algorithm. It's quite common in Java interviews nowadays to ask, whether do you know any O(n) sorting algorithm or not. If you face this question in future, you can mention Radix sort, Bucket sort, or Counting sort algorithms.  How does the counting sort algorithm works? Well, counting sort creates a bucket for each value and keep a counter in each bucket. Then each time a value is encountered in the input collection,  the appropriate counter is incremented.

Top 10 Java 8 Stream and Functional Programming Interview Questions Answers

The JDK 8 release has changed the way we write Java. With new functional programming idioms and a powerful Stream API, most of the new Java code is written in functional style. This also means that Stream and Functional programming related questions are increasing on Java interviews. If you are not familiar with Java 8 changes then it's difficult to crack a Java interview nowadays. Though it's not stated anywhere most of the companies, particularly Investment banks like Barclays, Citi, and Goldman Sachs now expect Java developers to know at least Java 8, which is also good right? Java 11 is already out and we are looking forward to Java 12 in a couple of months, it makes sense to know at least Java 8 changes.

Top 75 Programming Interview Questions Answers to Crack Any Coding Job Interview

Hello guys, if you are preparing for your next Programming Job interview and looking for some frequently asked Coding or Programming questions to practice then you have come to the right place. In this article, I am going to share some of the most commonly asked Coding questions from Programming Job interviews. In order to do well on the Coding interview you need practice, you just can't go there and try to solve the coding problems in limited time, that's actually one of the most common reasons to fail your programming Job interviews.  Sometimes, the interviewer also asks little bit easier coding questions on a telephonic interview like revering array in place or reversing a string in place.

java.sql.BatchUpdateException: Error converting data type float to numeric - Java + SQL Server

This error can come if you are inserting or updating a NUMERIC column in Microsoft SQL Server database from a Java Program using executeUpdate()method, I mean executing a  batch update query. It could also happen if you are calling a stored procedure and passing a float value corresponding to a NUMERIC column, and value happened to be out-of-range like generating "Arithmetic overflow error converting numeric to data type numeric" on the SQL Server end. For example, if your column is defined as NUMERIC (6,2) the maximum value it can represent is 9999.99, not 999999.99

How to Remove Duplicates from Unsorted Array in Java? [Solved]

This is one of the common technical interview questions which are asked to entry-level programmers and software engineers. There are also a lot of variants of how do you remove duplicates from an array in Java like sometime array is sorted and other times it's not-sorted or unsorted. Sometimes, Interviewer just spends more than half of the interview on this question by progressively making it more difficult by imposing new constraints like removing duplicate elements in place or without using any additional data structure, etc. Btw, If you are allowed to use Java's Collection framework then this is quite easy to solve, but if you are not allowed to use Set, Iterator, and other Java utility classes then it suddenly becomes a tricky algorithm question.

How to check if two String are Anagram of each other - Coding Problem

Here is a Java program to check if two String is an anagram of each other or not.  Two String is said to be an anagram of each other, if they contain exactly the same characters but in a different order. For example "army" and "mary" is an anagram of each other because they contain exact same characters 'a', 'r', 'm' and  'y'. For the sake of simplicity, you can also assume that your solution need not be case-sensitive, which means both ARMY and mary can be matched as an anagram of each other.


Difference between @SpringBootApplication vs @EnableAutoConfiguration annotations in Spring Boot

Even though both @SpringBootApplication and @EnableAutoConfiguration can be used to enable the auto-configuration feature of Spring Boot there is a subtle difference between them. The @SpringBootApplication does much more than what @EnableAutoConfiguration do. It's actually a combination of three annotations: @Configuration which is used in Java-based configuration on Spring framework, @ComponentScan to enable component scanning of components you write e.g. @Controller classes, and @EnableAutoConfgiuration itself, which is used to enable auto-configuration in Spring Boot application. Spring Boot designers realize that these three annotations are frequently used together so they bundled them into @SpringBootApplicaiton. Now, instead of three annotations you just need to specify one annotation on your Main class.

How to Compare Two Arrays in Java to check if they are equal - String, Integer Array Example

Hello guys, one of the common Programming, the day-to-date task is to compare two arrays in Java and see if they are equal to each other or not. Of course, you can't compare a String array to an int array, which means two arrays are said to be equal if they are of the same type, has the same length, contains same elements and in the same order. Now, you can write your own method for checking array equality or take advantage of Java's rich Collection API. Similar to what you have seen while printing array values in Java, java.util.Arrays class provides convenient methods for comparing array values.

What is Default or Defender Methods of Java 8 - Tutorial Example

Whenever someone talks about Java 8, the first thing they speak about is lambda expression and how lambda expression has changed the way you use Collections API today. In truth, lambda expression would not be that useful had language not been enhanced to support default methods on Java Interface. Also known as a virtual extension or defender methods, they allow you to declare a non-abstract method inside Java interface. Which means, finally you can add new methods without breaking all classes, which implements a certain interface. This opens a new path for enhancing and evolving existing Collection API to take advantage of lambda expressions. For example, now you can iterate over all elements of Collection in just one line, as opposed to four lines it requires you to do prior to Java 8.

How to Remove an Element from an Array in Java?

Hello guys, In the last article, you have learned how to reverse an array in place in Java and today I have come back with another array based coding interview question. It's also one of the frequently asked coding questions, not as popular as the previous one but still has been asked a lot of times on various Programming Job interviews, particularly to beginners. In this problem, you are asked to write a program to remove a given number from the given array. It may seem easy but the trick is that because an array is a fixed data structure and you cannot change the length of the array once created. So, you need to clarify with the Interviewer, what is the meaning of removing an element from the array here. Does he want to return an array without that element or just set that index to a null or empty string?

How to implement linked list data structure in Java using Generics

The linked list is a popular data structure for writing programs and lots of questions from a linked list is asked in various Programming Job interviews. Though Java API or JDK provides a sound implementation of the linked list data structure as java.util.LinkedList, a doubly linked list, you don't really need to implement a linked list of your own for writing production code, but all these interview questions require you to code a linked list in Java for solving coding problems. If you are not comfortable to create your own a linked list, it would be really difficult to solve questions like reversing a linked list or finding middle element of a linked list in one pass.

3 Examples of Stream + Collect() method of in Java 8

Hello guys, you may know that Java 8 brought Stream API which supports a lot of functional programming operations like filtermapflatMap, reduce, and collect. In this article, you will learn about the collect() method. The collect() method of Stream class can be used to accumulate elements of any Stream into a Collection. In Java 8, you will often write code which converts a Collection like a List or Set to Stream and then applies some logic using functional programming methods like the filter, map, flatMap and then converts the result back to the Collection like a ListSetMap, or ConcurrentMap in Java. In this last part, the collect() method of Stream helps. It allows you to accumulate the result into choice fo container you want like a list, set, or a map.

How to get current Day, Month, Year from Date in Java 8 and before? LocalDate vs java.util.Date

In this article, I'll show you how to get the current day, month, year, and dayOfWeek in Java 8 and earlier version like Java 6 and JDK 1.7. Prior to Java 8, you can use the Calendar class to get the various attribute from java.util.Date in Java. The Calendar class provides a get() method which accepts an integer field corresponding to the attribute you want to extract and return the value of the field from given Date, as shown here. You might be wondering, why not use the getMonth() and getYear() method of java.util.Date itself, well, they are deprecated and can be removed in the future version, hence it is not advised to use them.

10 Examples of forEach() method in Java 8

From Java 8 onward, you can iterate over a List or any Collection without using any loop in Java. The new Stream class provides a forEach() method, which can be used to loop over all or selected elements of list and map. forEach() method provides several advantages over traditional for loop e.g. you can execute it in parallel by just using a parallel Stream instead of regular stream. Since you are operating on stream, it also allows you to filter and map elements. Once you are done with filtering and mapping, you can use forEach() to operate over them. You can even use the method reference and lambda expression inside forEach() method, resulting in more clear and concise code.

How to sort HashMap by values in Java 8 using Lambdas and Stream - Example Tutorial

In the past, I have shown you how to sort a HashMap by values in Java, but that was using traditional techniques of pre-Java 8 world. Now the time has changed and Java has evolved into a programming language which can also do functional programming. How can you, a Java Programmer take advantage of that fact to do your day to day task better like how do you sort a Map by values in Java using lambda expressions and Stream API. That's what you are going to learn in this article. It will serve two purposes, first, it will tell you a new way to sort a Map by values in Java, and, second and more important it will introduce you to essential Java 8 features like Lambda Expression and Streams, which every Java Programmer should learn.

Difference between Abstract class and Interface in Java 8

Ever since JDK 8 has allowed concrete (non-abstract) methods on the interface like default and static methods, many of my readers have asked me how should they answer the classical abstract class vs interface questions. Earlier, an interface cannot have any concrete methods and that was the main difference between abstract class and interface but now that is not the case. In this post, I'll revisit this hugely popular Java interview question in light of Java 8 changes. As I said, before JDK 8, the level of abstraction was the clear-cut difference between abstract class and interface like interface was the purest form of Abstraction which only defines what interface is supposed to do without specifying how they should be implemented, in other words, it only declares API methods and leaves implementation to its subclasses.

Linear Search Algorithm in Java? Example tutorial

In the last article about searching and sorting, you have learned the binary search algorithm and today I'll teach you another fundamental searching algorithm called Linear search. Linear search is nothing but iterating over the array and comparing each element with target element to see if they are equal since we search the array sequential from start to end, this is also known as sequential search or linear search. It is very slow as compared to binary search because you have to compare each element with every other element and definitely not suitable for a large array. It's practically useful only in case of the small array up to 10 to 15 numbers. In the worst case, you need to check all elements to confirm if the target element exists in an array or not.

How to implement Binary Tree PreOrder Traversal in Java without Recursion - Iterative Example

This is my second article on how to implement binary tree pre-order traversal in Java. In the first part, I have shown how to traverse a binary tree with a pre-order traversal algorithm using Recursion, and in this article, you will learn how to implement pre-order traversal without using Recursion. You might be thinking that why do you need to learn the iterative solution if a recursive solution is possible and easy to write? Well, this type of question is mostly asked on Programming Job interviews and Interviewers like to see how comfortable a candidate is with both Recursion as well as using other data structures and iteration.