Size-limited queue that holds last N elements in Java

A very simple & quick question on Java libraries: is there a ready-made class that implements a Queue with a fixed maximum size – i.e. it always allows addition of elements, but it will silently remove head elements to accomodate space for newly added elements. Of course, it’s trivial to implement it manually: import java.util.LinkedList; … Read more

Google Guava vs. Apache Commons [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for … Read more

How to count the number of occurrences of an element in a List

I have an ArrayList, a Collection class of Java, as follows: ArrayList<String> animals = new ArrayList<String>(); animals.add(“bat”); animals.add(“owl”); animals.add(“bat”); animals.add(“bat”); As you can see, the animals ArrayList consists of 3 bat elements and one owl element. I was wondering if there is any API in the Collection framework that returns the number of bat occurrences … Read more

Kotlin’s List missing “add”, “remove”, Map missing “put”, etc?

In Java we could do the following public class TempClass { List<Integer> myList = null; void doSomething() { myList = new ArrayList<>(); myList.add(10); myList.remove(10); } } But if we rewrite it to Kotlin directly as below class TempClass { var myList: List<Int>? = null fun doSomething() { myList = ArrayList<Int>() myList!!.add(10) myList!!.remove(10) } } I … Read more