-1

从对象集合到 (Object,int) 的哈希图的最简单方法是什么,其中每个键包含初始集合的一个对象,其值等于该集合中相似元素的数量?

4

6 回答 6

2

我不知道 Java 中有任何实用方法可以在一行中做到这一点。您需要覆盖自定义类中的equals()andhashCode()方法。然后做一些这样的编码:

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); }
}

编辑:我假设您的意思是基于and方法相等的对象。equals()hashCode()

于 2013-04-30T08:32:38.613 回答
0
for(Object o : objects) {
    int count = map.get(o); // if it's null, count = 0
    count++;
    map.put(o, count);
}
于 2013-04-30T08:34:00.920 回答
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);
}

确保您的对象覆盖哈希码,适当地等于。

于 2013-04-30T08:34:07.197 回答
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;
    }


}
于 2013-04-30T08:40:02.040 回答
0

使用Google Guava 的 Multiset

然后就是简单

Multiset<Stuff> multiset = HashMultiset.create();
multiset.addAll(collectionOfStuff);
于 2013-04-30T08:40:43.637 回答
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);
}
于 2013-04-30T08:41:42.423 回答