Posted on 2011-10-31 16:53
云云 阅读(4656)
评论(0) 编辑 收藏
List <Integer> list = new ArrayList <Integer>();
Map <Integer,Integer> map = new HashMap <Integer,Integer>();

for(Integer i :list)
{
map.put(i, map.get(i)+1);
}把list集合中的数据 循环放进map中
如果当前元素已经在map中了 就加1
第二种方法:

public class Test2
{

public static void main(String [] args)
{
List<Integer> myList=Arrays.asList(1,1,2,2,3,3,3,4,4);
HashSet<Integer> hs=new HashSet<Integer>(myList);

for(Integer i:hs)
{
System.out.println(i+" 在List中存在" + Collections.frequency(myList,i) + "个");
}
}
}