-1

Input

String [] ss = {"-Dmyone=1","-Dmytwo=2","-Dmythree=3"}; 

ExpectedOutput:

Map<String,String> mapOb = {myone = 1, mytwo = 2, mythree = 3}

NOTE: I want to split the List of Strings by "=" and remove the "-D" and replace with empty string, and after splitting string if the length is not greater than 1 and then insert an empty Map.

Below is the snippet of Code I have written , but this causing an issue.

Map<String,String> collectMap = Arrays.stream(ss).flatMap(x -> {
        Map<String,String> expected = new HashMap<>();
        int splitIndex = x.indexOf("=");
        String [] keyValList = x.split("=");

        if(Arrays.stream(keyValList).count() > 1){
            Collectors.toMap(
              key -> x.substring(0,splitIndex).replace("-D", " "), 
              value -> x.substring(splitIndex +  1, x.length()));
        }
        else{
            Collections.<String,String>emptyMap();
        }
    }).collect(Collectors.toMap());
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Sampat Kumar
  • 492
  • 1
  • 6
  • 14
  • For ease I have specified List for input but these List of strings will be fetched from the Java runtime arguments "args". – Sampat Kumar Feb 22 '22 at 05:00
  • 1
    " but this causing an issue" What issue? – tgdavies Feb 22 '22 at 05:04
  • 3
    You're using `flatMap` in the wrong place and with the wrong argument. You really need to revisit your basics of using the stream API. Your code doesn't need to be complex: `Map collectMap=Arrays.stream(ss).map(s -> s.replace("-D", "").split("=")).collect(Collectors.toMap(k -> k[0], v -> v.length > 1 ? v[1] : ""));` – ernest_k Feb 22 '22 at 05:09
  • 1
    Does this answer your question? [How do I parse command line arguments in Java?](https://stackoverflow.com/questions/367706/how-do-i-parse-command-line-arguments-in-java) – aSemy Feb 22 '22 at 05:10
  • @aSemy that's not the same problem. – hfontanez Feb 22 '22 at 13:01
  • @SampatKumar have I answered your question? If so, don't forget to mark it as the selected answer. – hfontanez Feb 22 '22 at 13:02
  • Sure - @hfontanez – Sampat Kumar Feb 28 '22 at 08:07
  • @SampatKumar in that case, click the checkmark to mark it as the selected answer. I will appreciate it. – hfontanez Feb 28 '22 at 12:34

1 Answers1

2

Your stream operation is wrong. And it also didn't compile. The solution is actually quite simple:

  1. Open the stream and split each element using the = as the delimeter.
  2. The splitted elements are collected as follows e[0] is the key and e[1] the value on the map.
  3. The string stored as key need to replace "-D" with empty string "" (or nothing).
  4. The value component is stored unaltered.
public static void main(String[] args) {
    String [] ss = {"-Dmyone=1","-Dmytwo=2","-Dmythree=3"};

    List<String> myList = Arrays.asList(ss);
    Map<String, String> myMap = myList.stream().map(elem -> elem.split("=")).collect(Collectors.toMap(e -> e[0].replace("-D", ""), e -> e[1]));
    myMap.entrySet().forEach(entry -> {
       System.out.println("key: " + entry.getKey() + " - value: " + entry.getValue());
    });
}

This outputs:

key: myone - value: 1
key: mythree - value: 3
key: mytwo - value: 2
hfontanez
  • 5,774
  • 2
  • 25
  • 37