2

这是我在 Stackoverflow 中的第一个问题。我来发现一个问题,其中一个问题是我的同事提出的,并让我对它进行一些研究。

我的问题是我有课

   Class Function{
    String func;
   String funcname;
   boolean log;
   }

我创建了一些对象:

 obj1 : ("a" ,"b",true)- //these values come from either DB or UI
obj2 : ("c" ,"x",true)
obj3 : ("a" ,"z",true)

我有一个清单:

      List<function> flist;

现在我想在地图中拥有该列表并想放入地图中

     Map<String, List<function>> funcMap

然后显示以下输出:

    a:[obj1 obj3]
    b:[obj2]

如果我有列表但如何根据需要找到上述输出

4

5 回答 5

1

嗯..我认为这是一种以嵌套循环方式解析列表的情况。这是伪代码:

public void listToMap(List<Function> list)
{
    Map<String, List<Function>> map := new Map
    for every function in the list.
    {
        if(is the current function's func value does not exist in the map)
        {
             func := current functions func value
             List matchingFunctions := new list of Functions.
             for(every function in the list.)
             {
                 // Every Function with the same key get's added to a list.
                 if(function has the same func value as func)
                 {
                     add to matchingFunctions.
                 }
             }

             // That list and key get put into the HashMap.
             map.put(func, matchingFunctions).
        }
    }
}

关于代码设计的注释

Java 约定规定您应该将成员对象包装在gettersandsetters中,并且这些成员应该是私有的。

于 2013-10-10T08:19:18.693 回答
1

尝试这个,

将所有对象添加到flist.

初始化地图

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

key根据对象将添加到值列表的 func 值将对象添加到相关对象。

  for (Function functionValue : flist)
    {
        List<Function> functionList = funcMap.get(functionValue.getFunc());
        if (functionList != null && !functionList.isEmpty())
        {
            functionList.add(functionValue);
        }
        else
        {
            functionList = new ArrayList<Function>();
            functionList.add(functionValue);
            funcMap.put(functionValue.getFunc(), functionList);
        }
    }

Atlast 打印 funcMap

    for (Map.Entry< String, List<Function>> entry : funcMap.entrySet())
    {
        System.out.println("Key :  " + entry.getKey() + "Values : "+entry.getValue());
    }
于 2013-10-10T08:37:26.490 回答
0
public class Stackoverflow {

    public static void main(String[] args) {
        Function obj1 = new Function("a" ,"b",true);
        Function obj2 = new Function("c" ,"x",true);
        Function obj3 = new Function("a" ,"z",true);

        List<Function> functionsList1 = new ArrayList<Function>();
        functionsList1.add(obj1);
        functionsList1.add(obj3);

        List<Function> functionsList2 = new ArrayList<Function>();
        functionsList2.add(obj2);

        Map<String, List<Function>> funcMap = new LinkedHashMap<String, List<Function>>();
        funcMap.put("a", functionsList1);
        funcMap.put("b", functionsList2);

        Set<Entry<String,List<Function>>> entrySet = funcMap.entrySet();
        for (Entry<String, List<Function>> entry : entrySet) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
    }

}

class Function {
    String func;
    String funcname;
    boolean log;

    public Function(String func, String funcname, boolean log) {
        super();
        this.func = func;
        this.funcname = funcname;
        this.log = log;
    }

    @Override
    public String toString() {
        return "Function [func=" + func + ", funcname=" + funcname + ", log="
                + log + "]";
    }


}
于 2013-10-10T08:32:38.337 回答
0

Write your own map. Pass the list to map, let map will decide what portion of list to keep as value. I have added put method here, like the same, have to Override other methods.

class MyHashMap<K,V> extends HashMap<K,V>{
    @SuppressWarnings("unchecked")
    public V put(K k, V v) {
        String key  = (String)k; 
        List<Function> list = (List<Function>) v;
        List<Function> list2 = new ArrayList<Function>();
        for (Function function : list) {
            if(key.equalsIgnoreCase(function.func)){
                list2.add(function);
            }
        }
        return (V) list2;
    };

    @Override
    public boolean equals(Object o) {
        // Your own code
       return true;
    }
    // other methods goes here..

}
于 2013-10-10T09:01:59.780 回答
0

关于什么:

public class FuncTest {

    public static void main(String[] args) {
        new FuncTest().start();

    }

    private void start() {
        List<Function> flist = new ArrayList<Function>();
        flist.add(new Function("a", "b", true));
        flist.add(new Function("c", "x", true));
        flist.add(new Function("a", "z", true));

        Map<String, List<Function>> funcMap = new HashMap<String, List<Function>>();
        for (Function func : flist) {
            this.add(func.func, func, funcMap);
            this.add(func.funcname, func, funcMap);
        }
    }

    private void add(String field, Function func, Map<String, List<Function>> funcMap) {
        List<Function> subList = funcMap.get(field);
        if (subList == null) {
            subList = new ArrayList<Function>();
            funcMap.put(field, subList);
        }

        subList.add(func);
    }
}

笔记

正如 Chris 已经提到的,您应该考虑您的代码设计。使用getterssetters..

于 2013-10-10T08:26:46.087 回答