Member-only story

Java 8 coding interview questions

Gain Java Knowledge
8 min readMay 4, 2024

--

In this tutorial, I am sharing the some Java 8 interview coding questions with answers. I hope it will be helpful for you guys while preparing for an interview.

  1. How do you remove duplicate elements from a list using Java 8 streams?
package com.gain.java.knowledge;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class RemoveDuplicate {

public static void main(String[] args) {

//Creating a list of strings named listOfNames containing some names.
//The Arrays.asList method is used to convert an array of strings into a List.
List<String> listOfNames = Arrays.asList("sumit", "rohit", "kailash", "lucky", "joney", "rohit");
List<String> uniqueNames = listOfNames.stream()
.distinct()
.collect(Collectors.toList());
System.out.println(uniqueNames);
}
}

Output :
=======
[sumit, rohit, kailash, lucky, joney]

List<String> uniqueNames = listOfNames.stream()
.distinct()
.collect(Collectors.toList());

Step 1 : listOfNames.stream() -The stream() method is called on the listOfNames, which converts the list into a stream.
Step 2 : .distinct() - distinct() method is used to filter out duplicate elements from the…

--

--

Gain Java Knowledge
Gain Java Knowledge

Written by Gain Java Knowledge

The Java programming language is one of the most popular languages today. Stay up to date with news, certifications, free learning resources and much more.

No responses yet