Initialization of an ArrayList in one line I want to create a list of options for testing purposes. At first, I did this: ArrayList<String> places = new ArrayList<String>(); places.add("Buenos Aires"); places.add("Córdoba"); places.add("La Plata"); Then I refactored the code as follows: ArrayList<String> places = new ArrayList<String>( Arrays.asList("Buenos Aires", "Córdoba", "La Plata")); 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: ArrayList<String>
undefined
How to efficiently iterate over each Entry in a Map? If I have an object implementing the Map interface in Java and I wish to iterate over every pair contained within it, what is the most efficient way of going through the map? Will the ordering of elements depend on the specific map implementation that I have for the interface? Solution: Map<String, String> map = ... for (Map.Entry<String, String> entry : map.entrySet()) { System.out.println(entry.getKey() + "/" + entry.getValue()); } http://stackoverflow.com/questions/46898/how-to-efficiently-iterate-over-each-entry-in-a-map
undefined
When to use LinkedList over ArrayList? I've always been one to simply use: List<String> names = new ArrayList<String>(); I use the interface as the type name for portability, so that when I ask questions such as these I can rework my code. When should LinkedList be used over ArrayList and vice-versa? Answer: TL;DR ArrayList with ArrayDeque are preferable in much more use-cases than LinkedList. Not sure — just start with ArrayList. LinkedList and ArrayList are two different implementations of the List interface. LinkedListimplements it with a doubly-linked list. ArrayList implements it with a dynamically re-sizing array. As with standard linked list and array