Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.ArrayList;

import java.util.Collection;

import java.util.HashMap;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Main {
    public static Map groupMap(Collection collection, String keyName) {
        Map map = new HashMap();
        if (collection == null || collection.isEmpty()) {
            return map;
        }

        Map eachMap = null;
        Object key = null;
        List groupList = null;

        Iterator iter = collection.iterator();

        while (iter.hasNext()) {
            eachMap = (Map) iter.next();
            key = eachMap.get(keyName);
            if (key == null) {
                continue;
            }
            if (map.containsKey(key)) {
                groupList = (List) map.get(key);
                groupList.add(eachMap);
            } else {
                groupList = new ArrayList();
                groupList.add(eachMap);
                map.put(key, groupList);
            }
        }
        return map;
    }
}