Java 8 Coding Interview Answers
Java 8 Coding Interview Answers
System.out.println(Arrays.stream(list.stream().collect(Collectors.joining()).split(
"")).
collect(Collectors.groupingBy(Function.identity(),Collectors.counting())));
}
3. Sort list of string increasing order by length
public statis void sortStringByLength(String s){
System.out.println(Arrays.stream(s.split(" ")).
sorted(Comparator.comparing(String::length)).collect(Collectors.toList()));
}
4. Reverse each string in a sentence.
public static void reverseEachWord(String s){
String s2= Arrays.stream(s.split(" ")).map(e->new StringBuffer(e).reverse()).
collect(Collectors.joining(" "));
}
5. Remove duplicates from a list.
public static void removeDuplicates(List<String> list){
list.stream().collect(Collectors.toSet());
}
6. Remove duplicates from two lists.
public static void removeDuplicates(List<String> l1, List<String> l2){
Stream.concate(l1.stream(),l2.stream()).collect(Collectors.toSet());
}
7. Verify given Strings are Anagram.
public static void anagramTest(String s1, String s2){
String s3=Arrays.stream(s1.toLowerCase().split("")).sorted().
collect(Collectors.joining());
String s4= Arrays.stream(s2.toLowerCase().split("")).sorted().
collect(Collectors.joining());
if(s3.equals(s4)) {
System.out.println("its Anagram.");
} else{
System.out.println("Given String are not anagram.");
}
}
8. Validate same chars in a given array list of strings.
//Validate same chars in a given array list of strings.
//Or group by same chars in a array list of strings
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Setter@Getter
@ToString
@EqualsAndHashCode
@Builder
public class Employee {
private String empName;
private int empId;
private int age;
private String gender;
private int salary;
private String address1;
private String address2;
private String zipCode;
private String dept;
}
//This code is used for Auto generate 10 Random employees with different data.
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
}
private static Employee getNewFEmp() {
return
Employee.builder().empId(randomInt(5)).empName(random()).salary(randomInt(6)).gende
r("F").age(randomInt(2)).
address1(random()).address2(random()).zipCode(random()).dept(getDeptId()).build();
}
static final String AB = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
return saltStr;
}
private static int randomInt() {
//Generate random number
String SALTCHARS = "123456789";
StringBuilder salt = new StringBuilder();
Random rnd = new Random();
while (salt.length() < 4) { // length of the random string.
int index = rnd.nextInt(SALTCHARS.length());
salt.append(SALTCHARS.charAt(index));
}
String saltStr = salt.toString();
return Integer.parseInt(saltStr);
}
private static int randomInt(int s) {//Generate random int
String SALTCHARS = "123456789";
StringBuilder salt = new StringBuilder();
Random rnd = new Random();
while (salt.length() < s) {
int index = rnd.nextInt(SALTCHARS.length());
salt.append(SALTCHARS.charAt(index));
}
String saltStr = salt.toString();
return Integer.parseInt(saltStr);
}
private static String getDeptId() {//Generate Random Deptid
int i= randomInt(9);
if(i%3==0) {
return "A";
} else if(i%3==1) {
return"B";
} else {
return "C";
}
}
}
1. Find all male and female employees.
public static void findAllMaleFemaleEmployees(){
List<Employee> empL = ModelHelper.getEmployeeList();
System.out.println(empL);
System.out.println("Find number of M and F employees");
System.out.println(empL.stream().collect(Collectors.groupingBy(e-
>e.getGender(),Collectors.counting())));
System.out.println(empL.stream().collect(Collectors.groupingBy(e-
>e.getGender())));
System.out.println(empL.stream().collect(Collectors.groupingBy(e-
>e.getGender(),Collectors.toList())));
System.out.println("\n\n");
}
2. Find all employees in each department.
public static void findNumberOFEmpsFromEachDept() {
List <Employee> empL = ModelHelper.getEmployeeList();
System.out.println(empL);
System.out.println("number of employees in each department");
System.out.println(empL.stream().collect(Collectors.groupingBy(e-
>e.getDept(),Collectors.counting())));
System.out.println("\n\n");
}
3. Find Youngest and Oldest employee.
public static void findYongestEmp() {
List <Employee> empL = ModelHelper.getEmployeeList();
System.out.println(empL);
System.out.println("Find Youngest employee from from org");
System.out.println(empL.stream().min(Comparator.comparingInt(Employee::getAge)));
System.out.println("\n\n");
}
System.out.println(empL.stream().sorted(Comparator.comparing(Employee::getAge).then
Comparing(Employee::getEmpName)).collect(Collectors.toList()));
System.out.println("\n\n");
}