CrypticLuxury
CrypticLuxury

Reputation: 1

Splitting key values from stringlist

okay so i have strings in a list like so: - String, boolean I basically want to grab a whole heap of these from a long string list (progressing downward) and throw them into a hashmap like the following so i can simply get the key (the string) and get the boolean value from the key.

The hashmap: public HashMap<String, Boolean> keyValues = new HashMap<String, Boolean>();

thanks in advance folks.

PS: first time using stackoverflow, lets see how we go!

Upvotes: 0

Views: 34

Answers (2)

user6904265
user6904265

Reputation: 1938

If you want to do it in one-line:

Pattern.compile("-")
    .splitAsStream(s)
        .map(string -> string.split(","))
            .collect(Collectors.toMap(k -> k[0], v -> Boolean.valueOf(v[1])));

Where s is a string like this:

SIGN_COLOUR, false - SIGN_FORMAT, false - SIGN_ASHOP, false - SIGN_PSHOP, false

Upvotes: 1

Serg M Ten
Serg M Ten

Reputation: 5606

That should be easy, isn't it?

public static Map<String,Boolean> toMap(List<String> l) {
    HashMap<String,Boolean> m = new HashMap<String,Boolean>();
    l.forEach((s) -> { String[] t=s.split(","); m.put(t[0], new Boolean(t[1])); });
    return m;
}

Upvotes: 0

Related Questions