Here you can find the source of count(Iterator
public static <THING> Map<THING, Integer> count(Iterator<THING> thingsToCount)
//package com.java2s; //License from project: Open Source License import java.util.*; public class Main { public static <THING> Map<THING, Integer> count(Iterator<THING> thingsToCount) { Map<THING, Integer> countedThings = new HashMap<>(); while (thingsToCount.hasNext()) { THING thing = thingsToCount.next(); if (countedThings.containsKey(thing)) { int count = countedThings.get(thing); countedThings.put(thing, count + 1); } else { countedThings.put(thing, 1); }//from w w w . jav a 2s .com } return countedThings; } }