Java8 generate a map, it has key from each item in a list and it has value from a pair for that list

Advertisements public enum MyEnum { A(1, Arrays.asList(0, 1, 2, 3, 100, 101)), B(2, Arrays.asList(4)) private Integer code, private List<Integer> types; MyEnum(Integer code, List<Integer> types) { this.code = code; this.types = types; } } I want to generate a Map<Integer, Integer> map based on the above enum, which has key from each element in types and… Read More Java8 generate a map, it has key from each item in a list and it has value from a pair for that list

Is using lambda expression in Java makes our code efficient?

Advertisements Does using lambda expression in Java makes our code efficient? Can anybody explain Lambda expressions improve code readability and do not require interpretation. But other than that, are there other benefits of using lambda expression in Java in our code? >Solution : Lambdas allows you to write concise code. It encourages the use of… Read More Is using lambda expression in Java makes our code efficient?

We must compute the data for every hour using a list of dates

Advertisements I want the Map<String hours, count > from the list of dates I also need to put the 1 hour transaction between dates Ex In DB Value looks like this 2023-07-18 10:10:04.000 2023-07-18 10:20:04.000 2023-07-18 11:10:04.000 2023-07-18 11:30:04.000 2023-07-18 11:35:04.000 2023-07-18 12:10:04.000 2023-07-18 12:30:04.000 My expected result should be as follow as should calculate… Read More We must compute the data for every hour using a list of dates

Lambda to Double colon method reference

Advertisements here I’m trying to print values using lambda. Is it possible to make it in double colon way? public class Occurance { static void occuranceUsingListSet() { ArrayList<String> listColors = new ArrayList<>(Arrays.asList("Red","blue","green","green")); Set<String> setVals = new LinkedHashSet<>(listColors); setVals.stream().forEach(s -> System.out.println(s + " : " + Collections.frequency(listColors, s))); } } setVals.stream().forEach(s -> System.out.println(s + " :… Read More Lambda to Double colon method reference

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

Advertisements 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… 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

Advertisements 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());… Read More Return Optional.empty instead of Optional[null] in Optional.map

How to group and map string via lambda

Advertisements 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]}… Read More How to group and map string via lambda

grouping list object by attribute using java 8

Advertisements 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 :… Read More grouping list object by attribute using java 8