简体   繁体   中英

How do convert Multiple If Else ladder in java 8

I am having Array of Strings in the form of

[field1, Expected: 60,got: 70,field2, Expected: 80,got: 90, field3, Expected: 90,got: 70]

From this array i am constructing it into List<HashMap<String,String>> Here is what i tried so far

   List<HashMap<String,String>> getErroMap(String [] err){
    List<HashMap<String,String>> listErrorMap = new ArrayList<HashMap<String,String>>();
    HashMap<String,String> errorMap=null;
    byte count = 0;
    for (String arr : err) {
        if(count == 0 ) {
             errorMap = new HashMap<String, String>();
             errorMap.put("object",arr.replaceAll(".import", ""));
        }else if (count >0) {
            String key = count ==1?"Expected":"Found";
            if(arr.contains(":")) {
                 String[] tokens = arr.split(":");
                 errorMap.put(key, tokens[1]);
            }else {
                 errorMap.put(key, arr);
            }
        }
        if(count >= 2) {
            listErrorMap.add(errorMap);
            count = 0 ;
        }else {
            count++;
        }
    }
    return listErrorMap;
}

And getting desired output

 [{'object':field1,"Expected":60,"Found":70},{'object':'field2','Expected':80,'Found':90  ..and so on ]

SInce i am new to Java 8 i guess i am doing a lot of code to achieve simple thing. How do improve this using java 8 .

To get your desired result, you would need to return List<List<HashMap<String,String>>> , which is not ideally clean.

It's better to make a custom class as suggested, and create List<CustomClass> . For example:

public class myClass{
    public String obj;
    public int expected;
    public int found;

    public myClass(String obj, int expected, int found){
        this.obj = obj;
        this.expected = expected;
        this.found = found;
    }
}

Then, you can create a loop:

private List<myClass> convertToMap(String[] info){
    ArrayList<myClass> newList = new ArrayList<myClass>();
    for(int i = 0; i < info.length; i += 3){
        if(i < info.length){
          newList.add(new myClass(info[i],
                      Integer.parseInt(info[i+1]), 
                      Integer.parseInt(info[i+2])));
        }
    }
    return newList;
}

The below is what you want?

private List<Map<String, String>> getErrorMap(String[] inputs) {
    if (inputs.length % 3 != 0) {
        return new ArrayList<>();
    }
    final AtomicInteger counter = new AtomicInteger(0);
    return Arrays.stream(inputs)
        .collect(Collectors.groupingBy(s -> counter.getAndIncrement() / 3))
        .values()
        .stream()
        .map(strings -> {
            Map<String, String> map = new HashMap<>();
            map.put("object", strings.get(0));
            map.put("Expected", strings.get(1).split(":")[1]);
            map.put("Found", strings.get(2).split(":")[1]);
            return map;
        }).collect(Collectors.toList());
}

Input:

{"field1", "Expected: 60", "got: 70", "field2", "Expected: 80", "got: 90", "field3", "Expected: 90", "got: 70"}

Output:

[{Expected= 60, Found= 70, object=field1}, {Expected= 80, Found= 90, object=field2}, {Expected= 90, Found= 70, object=field3}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM