Frequently Asked Tricky Java Interview Coding Questions
Please go through below Java interview questions to get an idea how interviewer test you Core Java knowledge along with coding skill in Java 8 and later versions.
What will occur if we put the same key object in a HashMap again?
Answer –
HashMap doesn’t permit copy keys, so putting a similar key again will overwrite the existing one. A similar key will produce the equivalent hashcode and will wind up at a similar bucket position. Each bucket has map linked list.
Java compares the existing entry key object to the new one using equals method. If it returns true, the existing key object will be replaced by a new key value.
Java program to retrieve integer numbers starts with 5 in Java 8 (using Stream API)?
Answer –
List<Integer> intList = Arrays.asList(500,64,3,55,43);
myList.stream()
.map(x -> x + "")
.filter(x -> x.startsWith("5"))
.forEach(System.out::println);
Output is 500, 55
Write a Java program using Java 8 to get duplicates from the given integer List?
Java 8 program –
List<Integer> intList = Arrays.asList(3,6,2,3,2,5,7); Set<Integer> intSet = new HashSet(); intList.stream() .filter(x -> !intSet.add(x)) .forEach(System.out::println);
Output is 3, 2