Arjs
Arjs

Reputation: 45

Adding list of methods to class map in java

I have a list of values which concatenates the method name with the class name.

Ex: method1#class1

Now, I want to create a map where the key is the class name and values is a list of method names. The sample code is as follows.

Map<String, List<String>> map = new HashMap<>();

List<String> name = new ArrayList<>();
name.add("method1#class1");
name.add("method2#class1");
name.add("method3#class2");
name.add("method4#class2");

So, based on the above example, I need to create a map that should contain {class1 : [method1,method2]} {class2 : [method3,method4]}

Can someone help to iterate the above list and add to the map?

Upvotes: 1

Views: 541

Answers (4)

Przemysław Moskal
Przemysław Moskal

Reputation: 3609

I made all explanations in comments to the code:

    Map<String, List<String>> map = new HashMap<>();

    List<String> name = new ArrayList<>();
    name.add("method1#class1");
    name.add("method2#class1");
    name.add("method3#class2");
    name.add("method4#class2");

    for(String nm : name) { // for each String from name list...
        String[] splitted = nm.split("#"); // split this string on the '#' character
        if(map.containsKey(splitted[1])) { // if result map contains class name as the key...
            map.get(splitted[1]).add(splitted[0]); // get this key, and add this String to list of values associated with this key
        } else { // if result map doesn't contain that class name as key...
            map.put(splitted[1], new ArrayList<String>()); // put to map class name as key, initialize associated ArrayList...
            map.get(splitted[1]).add(splitted[0]); // and add method name to ArrayList of values
        }
    }

Upvotes: 0

Chorizzo
Chorizzo

Reputation: 68

Clue about extracting method names from a class:

    Class c = Example.class;
    Method[] m = c.getDeclaredMethods();
    for (Method method: m) {
        System.out.println(method.getName());
    }

Upvotes: 0

Luc&#237;a
Luc&#237;a

Reputation: 143

You could do something like this:

Map<String, List<String>> map = new HashMap<String, List<String>>();
List<String> name = new ArrayList<String>();
name.add("method1#class1");
name.add("method2#class1");
name.add("method3#class2");
name.add("method4#class2");


for (String item : name) {
    String[] split = item.split("#");

    String className = split[1];
    if(!map.containsKey(className))
        map.put(className, new ArrayList<String>());

    map.get(className).add(split[0]);

}

Good luck!

Upvotes: 3

Jorn Vernee
Jorn Vernee

Reputation: 33885

You can use streams in combination with the groupingBy collector:

Map<String, List<String>> result = name.stream()
        .map(s -> s.split("#")) // split string by '#'
        .collect(
            Collectors.groupingBy(arr -> arr[1], // second element is the key
                Collectors.mapping(arr -> arr[0], // first element is the value
                    Collectors.toList()))); // collect values with the same key into a list

System.out.println(result); // {class2=[method3, method4], class1=[method1, method2]}

Upvotes: 3

Related Questions