Max/Min of three or more Integers if null values are allowed

I am wondering if there a better, short and elegant way to achieve this than what i am trying. Let’s say i have 3 Integers(value1, value2, value3) and i want to find max value out of those Integers and null values are allowed for them. I cannot use below code because it could throw NullPointerException:… Read More Max/Min of three or more Integers if null values are allowed

How to check if a String is in valid format or not in Java8

I want to validate if a given String is in below format or not. String validString = "Emp_idIn645745,48576845,8347683,8734682"; some of the invalid Strings are below: "EmpIdIn46374,49587,498576" "Emp_Id=736437" Will it be possible to achieve this in java 8? >Solution : If so, then it’s not a problem with Java 8. But it depends on what exact… Read More How to check if a String is in valid format or not in Java8

Return Optional.empty instead of Optional[null] in Optional.map

In the below code, if customerInfo is not present, it returns Optional[null] and hence customerInfo.get(name).textValue() returns a NPE. Is there a way to make map(data -> data.get("customerInfo")) return Optional.empty() instead of Optional[null]? Here the null within Optional[null] is a NullNode object. Optional<JsonNode> orderData = getOrderData() // API Call orderData.map(data -> data.get("customerInfo")) .map(customerInfo -> customerInfo.get(name).textValue()); >Solution… Read More Return Optional.empty instead of Optional[null] in Optional.map

How to group and map string via lambda

I have a string as below: String data = "010$$fengtai,010$$chaoyang,010$$haidain,027$$wuchang,027$$hongshan,027$$caidan,021$$changnin,021$$xuhui,020$$tianhe"; And I want to convert it into Map<String, List> like below(first split by , and split by $$,value before $$ is key need to be group,and value after $$ needs to put inside a list): {027=[wuchang, hongshan, caidan], 020=[tianhe], 010=[fengtai, chaoyang, haidain], 021=[changnin, xuhui]} I… Read More How to group and map string via lambda

grouping list object by attribute using java 8

i have data like this : [ { "category":"Fruits", "name":"Apple" }, { "category":"Fruits", "name":"Manggo" }, { "category":"Vegetables", "name":"Water Spinach" } ] I want to grouping by java 8, I’ve tried with : Map<String, List<MyData>> myData = list.stream().collect(Collectors.groupingBy(d -> d.getCategory())); But the result is not what I need. Because the result I expected was : Fruits… Read More grouping list object by attribute using java 8

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?