9

I want a class that will count the the number of objects I have - that sounds more efficient that gathering all the objects and then grouping them.

Python has an ideal structure in collections.Counter, does Java or Scala have a similar type?

1
  • Java doesn't, but you can easily implement one of your own. Jan 31, 2015 at 17:32

7 Answers 7

13

From the documentation that you linked:

The Counter class is similar to bags or multisets in other languages.

Java does not have a Multiset class, or an analogue. Guava has a MultiSet collection, that does exactly what you want.

In pure Java, you can use a Map<T, Integer> and the new merge method:

final Map<String, Integer> counts = new HashMap<>();

counts.merge("Test", 1, Integer::sum);
counts.merge("Test", 1, Integer::sum);
counts.merge("Other", 1, Integer::sum);
counts.merge("Other", 1, Integer::sum);
counts.merge("Other", 1, Integer::sum);

System.out.println(counts.getOrDefault("Test", 0));
System.out.println(counts.getOrDefault("Other", 0));
System.out.println(counts.getOrDefault("Another", 0));

Output:

2
3
0

You can wrap this behaviour in a class in a few lines of code:

public class Counter<T> {
    final Map<T, Integer> counts = new HashMap<>();

    public void add(T t) {
        counts.merge(t, 1, Integer::sum);
    }

    public int count(T t) {
        return counts.getOrDefault(t, 0);
    }
}

Use like this:

final Counter<String> counts = new Counter<>();

counts.add("Test");
counts.add("Test");
counts.add("Other");
counts.add("Other");
counts.add("Other");

System.out.println(counts.count("Test"));
System.out.println(counts.count("Other"));
System.out.println(counts.count("Another"));

Output:

2
3
0
1
  • Merge just made my day. Excellent.
    – dantiston
    Apr 10, 2015 at 21:34
9

Not as far as I know. But scala is very expressive, allowing you to cook something like it yourself:

def counts[T](s: Seq[T]) = s.groupBy(x => x).mapValues(_.length)

Edit: Even more concise with:

def counts[T](s: Seq[T]) = s.groupBy(identity).mapValues(_.length)
2
  • But the OP wanted something other than "gathering all the objects and then grouping them"? Jan 31, 2015 at 18:02
  • @Paul He also wanted something like collections.Counter. Now have a look at the python source, it looks like python does the same thing as the scala snippet I wrote. svn.python.org/projects/python/trunk/Lib/collections.py
    – Dave
    Jan 31, 2015 at 18:11
7

Another scala version, doing it in one pass and avoiding .groupBy

val l = List("a", "b", "b", "c", "b", "c", "b", "d")

l.foldLeft(Map[String, Int]() withDefaultValue (0))
          { (m, el) => m updated (el, m(el)+1)}
//> res1: Map(a -> 1, b -> 4, c -> 2, d -> 1)

or if you don't want a map with default value zero

l.foldLeft(Map[String, Int]()) { (m, el) => m updated (el, m.getOrElse(el,0)+1)}
2

Mostly you should be good with basic operations chained together. Like:

val s = Seq("apple", "oranges", "apple", "banana", "apple", "oranges", "oranges")
s.groupBy(l => l).map(t => (t._1, t._2.length)) //1
s.count(_ == "apple") //2

With as result :

Map(banana -> 1, oranges -> 3, apple -> 3) //1 - result
3 //2 - result
0

Guava MultiSet has a count method

http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Multiset.html#count(java.lang.Object)

1
  • Brilliant, I love this library! Jan 31, 2015 at 17:43
0

Many years after I originally asked this question I realized just how trivial it was. My ultra-basic Scala solution is:

import scala.collection.mutable

/**
  * Created by salim on 3/10/2017.
  */
case class Counter[T]() {
  lazy val state:mutable.Map[T,Int] = mutable.HashMap[T,Int]()
  def apply(i:T):Int = state.getOrElse(i,0)
  def count(i:T):Unit = {
    val newCount = 1 + this(i)
    state += (i -> newCount)
  }
}
0

Here is my tail recursive Scala implementation using a mutable map

def counter[T](s: Seq[T]) = {
  import scala.collection.mutable.Map
  def counter_iter[T](s: Seq[T], m: Map[T, Int]): Map[T, Int]= {
    if (s.isEmpty) m
    else {
      m(s.head) += 1
      counter_iter(s.tail, m)
    }
  }
  counter_iter(s, Map[T, Int]().withDefaultValue(0))
}

to use:

scala> counter(List(1,1,2,2,2,3,4))
res34: scala.collection.mutable.Map[Int,Int] = Map(2 -> 3, 4 -> 1, 1 -> 2, 3 -> 1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.