在java中计算不同

时间:2013-04-30 08:29:33

标签: java

从对象集合到(Object,int)的散列映射的最简单方法是什么,其中每个键包含初始集合的对象,其值等于类似的数量该集合中的元素?

6 个答案:

答案 0 :(得分:2)

我不知道Java中的任何实用方法都可以在一行中完成。您需要覆盖Custom类中的equals()hashCode()方法。然后做一些像这样的编码:

Map<SomeObject,int> frequencymap = new HashMap<SomeObject,int>();
 foreach(SomeObject element in objectList) {
    if(frequencymap.containsKey(element)) {
      frequencymap.put(element , frequencymap.get(element)+1);
  }
 else{ frequencymap.put(element, 1); }
}

编辑:我假设类似你的意思是基于equals()hashCode() 相等的对象方法

答案 1 :(得分:0)

for(Object o : objects) {
    int count = map.get(o); // if it's null, count = 0
    count++;
    map.put(o, count);
}

答案 2 :(得分:0)

List<Foo> all = ... ;
Map<Foo, Integer> dis = new HashMap<Foo, Integer>();
Integer current = null;
for (Foo foo : all) {
    current = dis.get(foo);
    current = current == null ? 1 : current+1;
    dis.put(foo, current);
}

确保您的对象覆盖哈希码,等于正确。

答案 3 :(得分:0)

试试这个

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

public class CountObjects
{
    public static void main(String[] args)
    {
        List<Obj> objList = new ArrayList<Obj>();
        objList.add( new Obj ( "name1", "34" ) );
        objList.add( new Obj ( "name1", "33" ) );
        objList.add( new Obj ( "name2", "34" ) );
        objList.add( new Obj ( "name4", "35" ) );
        objList.add( new Obj ( "name2", "37" ) );

        Map<String, Integer> map = new HashMap<String, Integer>();
        for (Obj obj : objList)
        {
            Integer childObjs = map.get( obj.getName() );
            if( childObjs == null )
            {
                childObjs = 0;
            }
            childObjs++;
            map.put( obj.getName(), childObjs );
        }
        System.out.println( map );
    }
}

class Obj 
{
    private String name;

    private String age;

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getAge()
    {
        return age;
    }

    public void setAge(String age)
    {
        this.age = age;
    }

    public Obj(String name, String age)
    {
        super();
        this.name = name;
        this.age = age;
    }

    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((age == null) ? 0 : age.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Obj other = (Obj) obj;
        if (age == null)
        {
            if (other.age != null)
                return false;
        } else if (!age.equals(other.age))
            return false;
        if (name == null)
        {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

    public String toString()
    {
        final String TAB = "    ";

        String retValue = "";

        retValue = "Obj ( "
            + "name = " + this.name + TAB
            + "age = " + this.age + TAB
            + " )";

        return retValue;
    }


}

答案 4 :(得分:0)

使用Google Guava's Multiset

然后就是

Multiset<Stuff> multiset = HashMultiset.create();
multiset.addAll(collectionOfStuff);

答案 5 :(得分:0)

试试这个:

List<Object> list = new ArrayList<Object>();
//fill your list
Map<Object,Integer> map = new HashMap<Object,Integer>();
int count=0;
for(Object obj:list){
    count=0;
    count = Collections.frequency(list, obj);
    map.put(obj, count);
}