Java Map Invert invert(Map map, String prefix)

Here you can find the source of invert(Map map, String prefix)

Description

invert

License

Open Source License

Declaration

public static List<Map<String, Object>> invert(Map<String, Object> map, String prefix) 

Method Source Code

//package com.java2s;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    public static List<Map<String, Object>> invert(Map<String, Object> map, String prefix) {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if (entry.getKey().startsWith(prefix)) {
                String key = entry.getKey().substring(prefix.length());
                if (Iterable.class.isAssignableFrom(entry.getValue().getClass())) {
                    int i = 0;
                    for (Object e : ((Iterable) entry.getValue())) {
                        for (int j = list.size(); j <= i; j++) {
                            list.add(new HashMap<String, Object>());
                        }/*from   ww  w .  j  a  v a2s . com*/
                        list.get(i).put(key, e);
                        i++;
                    }
                } else {
                    if (list.isEmpty()) {
                        list.add(new HashMap<String, Object>());
                    }
                    list.get(0).put(key, entry.getValue());
                }
            }
        }
        return list;
    }
}

Related

  1. inverse(Map map)
  2. inverseGet(Map map, Object value)
  3. invert(Map map)
  4. invert(Map in)
  5. invert(Map map)
  6. invert(Map map)
  7. invertedMapFrom(Map source)
  8. invertMap(final Map map)
  9. invertMap(final Map map)