If I have a collection, such as Collection<String> strs, how can I get the first item out? I could just call an Iterator, take its first next(), then throw the Iterator away. Is there a less wasteful way to do it? Answer Iterables.get(yourC, indexYouWant) Because really, if you’re using Collections, you should be using Google Collections.
Tag: collections
Initialization of an ArrayList in one line
I wanted to create a list of options for testing purposes. At first, I did this: Then, I refactored the code as follows: Is there a better way to do this? Answer Actually, probably the “best” way to initialize the ArrayList is the method you wrote, as it does not need to create a new List in any way: The
What are Reified Generics? How do they solve Type Erasure problems and why can’t they be added without major changes?
I’ve read Neal Gafter’s blog on the subject and am still unclear on a number of points. Why is it not possible to create implementations of the Collections API that preserve type information given the current state of Java, the JVM and existing collections API? Couldn’t these replace the existing implementations in a future version of Java in a way
How to make a new List in Java
We create a Set as: How do we create a List in Java? Answer or with generics (Java 7 or later) or with generics (Old java versions)
Why doesn’t java.util.Set have get(int index)?
I’m sure there’s a good reason, but could someone please explain why the java.util.Set interface lacks get(int Index), or any similar get() method? It seems that sets are great for putting things into, but I can’t find an elegant way of retrieving a single item from it. If I know I want the first item, I can use set.iterator().next(), but
How to clone ArrayList and also clone its contents?
How can I clone an ArrayList and also clone its items in Java? For example I have: And I would expect that objects in clonedList are not the same as in dogs list. Answer You will need to iterate on the items, and clone them one by one, putting the clones in your result array as you go. For that
Why is a ConcurrentModificationException thrown and how to debug it
I am using a Collection (a HashMap used indirectly by the JPA, it so happens), but apparently randomly the code throws a ConcurrentModificationException. What is causing it and how do I fix this problem? By using some synchronization, perhaps? Here is the full stack-trace: Answer This is not a synchronization problem. This will occur if the underlying collection that is
When to use LinkedList over ArrayList in Java?
I’ve always been one to simply use: I use the interface as the type name for portability, so that when I ask questions such as this, I can rework my code. When should LinkedList be used over ArrayList and vice-versa? Answer Summary ArrayList with ArrayDeque are preferable in many more use-cases than LinkedList. If you’re not sure — just start with
Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop
We all know you can’t do the following because of ConcurrentModificationException: But this apparently works sometimes, but not always. Here’s some specific code: This, of course, results in: Even though multiple threads aren’t doing it. Anyway. What’s the best solution to this problem? How can I remove an item from the collection in a loop without throwing this exception? I’m
Rule of thumb for choosing an implementation of a Java Collection?
Anyone have a good rule of thumb for choosing between different implementations of Java Collection interfaces like List, Map, or Set? For example, generally why or in what cases would I prefer to use …