比较和排序hashmap java中值的arraylist

时间:2014-11-14 04:55:29

标签: java arraylist hashmap key-value

我有一个hashmap

Map<String, ArrayList<String>> games = new hashmap<String, Arraylist<String>>();

它包含名称作为键和游戏列表作为值。 像这样:

A= [ cricket, soccer, tennis, baseball]
B= [soccer, badminton, rugby]
C= [cricket, badminton]
D= [rugby, cricket, soccer]
E= [cricket, baseball]
F= [tennis, rugby]

现在我想计算每个值(criket,soccer等)的出现次数 根据值的出现仅为键分配一个值。 在这里,板球发生了4次,足球发生了3次,依此类推。 并比较每个键的值并检查发生了多少次的值。 然后我想分配具有最大出现的值。 那就是我想要一个像这样的hashmap:

A= [ cricket] // because among its 4 values, cricket has occured maximum times.
B= [soccer] // among its 3 values, soccer has occured maximum times.
C= [cricket]
D= [cricket]
E= [cricket]
F= [rugby] // among its 2 values, rugby has occured maximum times which is 3.

依旧...... 我不清楚如何解决这种情况。所以,请帮我解决这个问题 提供详细信息。 谢谢

3 个答案:

答案 0 :(得分:0)

假设此算法可以帮助您。

public void testAlgorithm(){
        Map<String, List<String>> map = this.generateDatasource();
        Map<String, Integer> numberMap = this.calculateHappeds(map);
        Map<String, String> rstMap = new HashMap<String, String>();
        for (String key : map.keySet()) {
            List<String> sports = map.get(key);
            Integer index = 0;
            for (int i = 0; i < sports.size(); i++) {
                if (numberMap.get(sports.get(i)) > numberMap.get(sports.get(index))) {
                    index = i;
                }
            }
            rstMap.put(key, sports.get(index));
        }
        for (String key : rstMap.keySet()) {
            System.out.println(key + " = " + " [ " + rstMap.get(key) + " ]");
        }
    }

    private Map<String, Integer> calculateHappeds(Map<String, List<String>> map) {
        Map<String, Integer> calculate = new HashMap<String, Integer>();
        for (String key : map.keySet()) {
            List<String> sports = map.get(key);
            for (String sport : sports) {
                if (calculate.containsKey(sport)) {
                    Integer count = calculate.get(sport);
                    count += 1;
                    calculate.put(sport, count);
                }else {
                    calculate.put(sport,1);
                }
            }
        }
        return calculate;
    }

    private Map<String, List<String>> generateDatasource() {
        Map<String, List<String>> map = new HashMap<String, List<String>>();
        List<String> a = new ArrayList<String>();
        a.add("cricket");
        a.add("soccer");
        a.add("tennis");
        a.add("baseball");
        map.put("A", a);

        List<String> b = new ArrayList<String>();
        b.add("soccer");
        b.add("badminton");
        b.add("rugby");
        map.put("B", b);

        List<String> c = new ArrayList<String>();
        c.add("cricket");
        c.add("badminton");
        map.put("C", c);

        List<String> d = new ArrayList<String>();
        d.add("rugby");
        d.add("cricket");
        d.add("soccer");
        map.put("D", d);

        List<String> e = new ArrayList<String>();
        e.add("cricket");
        e.add("baseball");
        map.put("E", e);

        List<String> f = new ArrayList<String>();
        f.add("tennis");
        f.add("rugby");
        map.put("F", f);
        return map;
    }

因为rstMap是map,所以输出是

D =  [ cricket ]
E =  [ cricket ]
F =  [ rugby ]
A =  [ cricket ]
B =  [ soccer ]
C =  [ cricket ]

答案 1 :(得分:0)

我说最简单的方法是创建一个扩展targetMap实现的类(在本例中为HashMap)。在新类中,为每个值选项保留第二个Map到一个Integer,它保持ArrayList中值的计数。然后覆盖#clear(),#output(),#utput()和#remove()方法来管理第二个映射。对于Ex:

SHashMap extends HashMap<String, ArrayList<String>>{
  HashMap<String,Integer> valMap;

  public void put(String key, ArrayList<String> vals){
    for(String s: vals){
        if(valMap.containsKey(s)){
            valMap.put(s,valMap.get(s)+1);
        }else{
            valMap.put(s,1);
        }
    }
    return super.put(key,vals); // Keep the hierarchy accurate
  }
}

然后,只需添加一个简单的getMax(String)方法,该方法检查具有最高计数的valMap:

public String getMax(String key){
    String max = null;
    int countMax =0;
    for(String val:get(key)){
        if(valMap.get(val) > countMax){
            countMax = valMap.get(val);
            max = val;
        }
    }
    return max;
}

实现#clear,#putAll和#remove方法,根据您的需求和使用需要,适当调整valMap中的数据。根据您的使用情况,有多种方法可以提高效率,但这可以帮助您实现目标。

答案 2 :(得分:0)

    public class CountMap {

/*  data
    A= [ cricket, soccer, tennis, baseball]
    B= [soccer, badminton, rugby]
    C= [cricket, badminton]
    D= [rugby, cricket, soccer]
    E= [cricket, baseball]
    F= [tennis, rugby]*/

    public CountMap(){}

    private static Map<String,List<String>> testDdata= new HashMap<>(); 
    private static Map<String,Integer> sportsCoount= new HashMap<>(); 


    public static void main(String[] args){

        //prepare test data
        prepareTestData();

        prepareOutputMap();
    }

    private static void prepareOutputMap() {

        // sportsCoountMap is ready now
        for(String key:testDdata.keySet()){
            List<String> valueList= testDdata.get(key);

            for(String testValue:valueList){
                Integer value=sportsCoount.get(testValue);
                if(value!=null){
                    value++;
                    sportsCoount.put(testValue,value);
                }else{
                    value=1;
                    sportsCoount.put(testValue, value);
                }
            }
        }

        PriorityQueue<Entry<String, Integer>> pq = new PriorityQueue<Map.Entry<String,Integer>>(sportsCoount.size(), new Comparator<Entry<String, Integer>>() {

            @Override
            public int compare(Entry<String, Integer> arg0,
                    Entry<String, Integer> arg1) {
                return arg1.getValue().compareTo(arg0.getValue());
            }
        });
        pq.addAll(sportsCoount.entrySet());
        /*while (!pq.isEmpty()) {
            System.out.println(pq.poll());
        }*/

        Entry<String,Integer> entry;

        List<String> keyList= new ArrayList<String>();
        keyList.addAll(testDdata.keySet());

        Collections.sort(keyList);
        for(String key:keyList){
            entry= pq.poll();
            System.out.println(key+"="+entry.getKey());
        }

    }

    private static void prepareTestData() {
        List<String> data1= new ArrayList<>();
        //cricket, soccer, tennis, baseball
        data1.add("cricket");
        data1.add("soccer");
        data1.add("tennis");
        data1.add("baseball");
        testDdata.put("A", data1);

        List<String> data2= new ArrayList<>();
        //soccer, badminton, rugby
        data2.add("soccer");
        data2.add("badminton");
        data2.add("rugby");
        testDdata.put("B", data2);

        List<String> data3= new ArrayList<>();
        //cricket, badminton
        data3.add("cricket");
        data3.add("badminton");
        testDdata.put("C", data3);

        List<String> data4= new ArrayList<>();
        //rugby, cricket, soccer
        data4.add("rugby");
        data4.add("cricket");
        data4.add("soccer");
        testDdata.put("D", data4);

         List<String> data5= new ArrayList<>();
        //cricket, baseball
        data5.add("cricket");
        data5.add("baseball");
        testDdata.put("E", data5);


    }
}
> OutPut:-
> 
> A=cricket 
> B=soccer 
> C=rugby 
> D=baseball 
> E=badminton