How to use Optional.of() or Stream.of() approach to reduce code

I recently made it through to the final round interview for a company in which they dissected the week-long take-home project they assigned me (Angular Spring Boot "Rock, Paper, Scissors" app). The details of the project don’t matter, my question is very specific. At one point in the interview they asked me to demonstrate my… Read More How to use Optional.of() or Stream.of() approach to reduce code

How to get all keys whose values are null in Java 8 using Map

I was going through How to remove a key from HashMap while iterating over it?, but my requirement is bit different. class Main { public static void main(String[] args) { Map<String, String> hashMap = new HashMap<>(); hashMap.put("RED", "#FF0000"); hashMap.put("BLACK", null); hashMap.put("BLUE", "#0000FF"); hashMap.put("GREEN", "#008000"); hashMap.put("WHITE", null); // I wan’t result like below – get All… Read More How to get all keys whose values are null in Java 8 using Map

How to write expression in java8?

I’m trying to write this function with java 8 , but i didn’t succeeded. This is my code : private LocalTime getAbsenceParJour(List<test> tests) { LocalTime absenceParJourTime = null; Duration sum = Duration.ZERO; for (test perPrestation : tests) { if (perPrestation.getTypeContactLibre().equalsIgnoreCase("absence")) { Duration duration = Duration.between(perPrestation.getHeureDebut(), perPrestation.getHeureFin()); sum = sum.plus(duration); } } if (sum != null)… Read More How to write expression in java8?

How to extract the instance value in the string efficently

I have a string "/p4products/nemis2/filehandlerU/encdv2/work/10098325" which is coming as input from CMD. In this string the "encdv" is the environment name and "2" is the instance ID. Environment name can be either "encdv1","encdv2","encpr1","encpr2" and so on but Instance ID can be "1" or "2" only. I want to extract the instance ID whenever I encounter… Read More How to extract the instance value in the string efficently

Java Method receive generic list and returns <capture of ?>

I’m trying to filter a List of objects that implements a Interface. And I trying to create a generic method for all the classes. Something like: interface SomeInterface { String getFlag(); } class SomeObject implements SomeInterface { public String getFlag() { return "X"; } } List<SomeObject> someObjectList = new ArrayList<>(); // Compilation error here List<SomeObject>… Read More Java Method receive generic list and returns <capture of ?>

How to filter and get list using java 8 in java?

i have simple java 8 example using filter. When i try to filter , getting empty value. class Per { String id; Per(String id) { this.id = id; } public String getId() { return id; } public static void main(String[] args) { List<String> allIds = new ArrayList<>(); allIds.add("1"); allIds.add("2"); allIds.add("3"); allIds.add("4"); List<Per> unavailableItems = new… Read More How to filter and get list using java 8 in java?

segregate duplicates and non duplicates from a list of objects based on property on multiple property in java 8

I want to segregate duplicates and non duplicates from a list of objects based on property customerId and customerRegistration using java 8 segregate meaning if I have 3 record with same customerId and customerRegistration, first record should add into nonduplicate list and 2 nd 3 record should be add in to duplicate list. I have… Read More segregate duplicates and non duplicates from a list of objects based on property on multiple property in java 8