Efficiency of Java “Double Brace Initialization”?

Here’s the problem when I get too carried away with anonymous inner classes: 2009/05/27 16:35 1,602 DemoApp2$1.class 2009/05/27 16:35 1,976 DemoApp2$10.class 2009/05/27 16:35 1,919 DemoApp2$11.class 2009/05/27 16:35 2,404 DemoApp2$12.class 2009/05/27 16:35 1,197 DemoApp2$13.class /* snip */ 2009/05/27 16:35 1,953 DemoApp2$30.class 2009/05/27 16:35 1,910 DemoApp2$31.class 2009/05/27 16:35 2,007 DemoApp2$32.class 2009/05/27 16:35 926 DemoApp2$33$1$1.class 2009/05/27 16:35 4,104 … Read more

Difference between

extends The wildcard declaration of List<? extends Number> foo3 means that any of these are legal assignments: List<? extends Number> foo3 = new ArrayList<Number>(); // Number “extends” Number (in this context) List<? extends Number> foo3 = new ArrayList<Integer>(); // Integer extends Number List<? extends Number> foo3 = new ArrayList<Double>(); // Double extends Number Reading – … Read more

Converting ‘ArrayList to ‘String[]’ in Java

List<String> list = ..; String[] array = list.toArray(new String[0]); For example: List<String> list = new ArrayList<String>(); //add some stuff list.add(“android”); list.add(“apple”); String[] stringArray = list.toArray(new String[0]); The toArray() method without passing any argument returns Object[]. So you have to pass an array as an argument, which will be filled with the data from the list, … Read more

Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop

Iterator.remove() is safe, you can use it like this: List<String> list = new ArrayList<>(); // This is a clever way to create the iterator and call iterator.hasNext() like // you would do in a while-loop. It would be the same as doing: // Iterator<String> iterator = list.iterator(); // while (iterator.hasNext()) { for (Iterator<String> iterator = … Read more

How to directly initialize a HashMap (in a literal way)?

All Versions In case you happen to need just a single entry: There is Collections.singletonMap(“key”, “value”). For Java Version 9 or higher: Yes, this is possible now. In Java 9 a couple of factory methods have been added that simplify the creation of maps : // this works for up to 10 elements: Map<String, String> … Read more

Sort a Map by values

Here’s a generic-friendly version: public class MapUtil { public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) { List<Entry<K, V>> list = new ArrayList<>(map.entrySet()); list.sort(Entry.comparingByValue()); Map<K, V> result = new LinkedHashMap<>(); for (Entry<K, V> entry : list) { result.put(entry.getKey(), entry.getValue()); } return result; } }

Initialization of an ArrayList in one line

It would be simpler if you were to just declare it as a List – does it have to be an ArrayList? List<String> places = Arrays.asList(“Buenos Aires”, “Córdoba”, “La Plata”); Or if you have only one element: List<String> places = Collections.singletonList(“Buenos Aires”); This would mean that places is immutable (trying to change it will cause … Read more

When to use LinkedList over ArrayList in Java?

Summary ArrayList with ArrayDeque are preferable in many more use-cases than LinkedList. If you’re not sure — just start with ArrayList. TLDR, in ArrayList accessing an element takes constant time [O(1)] and adding an element takes O(n) time [worst case]. In LinkedList inserting an element takes O(n) time and accessing also takes O(n) time but LinkedList … Read more