Java Remove Element from ArrayList

To remove some elements from an ArrayList while iterating over the ArrayList, we need to use Iterator. Integer[] arr = {1,2,3,4,5,6}; ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(arr)); System.out.println(list);   Iterator<Integer> iter = list.iterator(); while(iter.hasNext()){ int i = iter.next(); if(i==5) iter.remove(); }   System.out.println(list);Integer[] arr = {1,2,3,4,5,6}; ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(arr)); System.out.println(list); Iterator<Integer> iter = list.iterator(); … Read more

Deep Understanding of ArrayList.iterator()

iterator often cause problems, because developers often do not know how it works. The following code is from source code of ArrayList. One common problem is throwing java.util.ConcurrentModificationException. This exception is actually throw in the remove method. remove() has to be called after next(). if remove() is called before next(), the size of arraylist changes, … Read more

java.util.ConcurrentModificationException

This post shows show to solve the problem of java.util.ConcurrentModificationException for ArrayList. The error message looks like the following: Exception in thread “main” java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(Unknown Source) at java.util.ArrayList$Itr.next(Unknown Source) … … The Problem You may want to iterate through an ArrayList, and delete some element under some certain condition. For example, the following code … Read more

Efficient Counter in Java

You may often need a counter to understand the frequency of something (e.g., words) from a database or text file. A counter can be easily implemented by using a HashMap in Java. This article compares different approaches to implement a counter. Finally, an efficient one will be concluded. UPDATE: Check out Java 8 counter, writing … Read more

Top 9 questions about Java Maps

In general, Map is a data structure consisting of a set of key-value pairs, and each key can only appears once in the map. This post summarizes Top 9 FAQ of how to use Java Map and its implemented classes. For sake of simplicity, I will use generics in examples. Therefore, I will just write Map instead of specific Map. But you can always assume that both the K and V are comparable, which means K extends Comparable and V extends Comparable.

Read more

Top 10 questions about Java Collections

The following are the most popular questions of Java collections asked and discussed on Stackoverflow. Before you look at those questions, it’s a good idea to see the class hierarchy diagram. 1. When to use LinkedList over ArrayList? ArrayList is essentially an array. Its elements can be accessed directly by index. But if the array … Read more

Frequently Used Methods of Java HashMap

HashMap is very useful when a counter is required.

HashMap<String, Integer> countMap = new HashMap<String, Integer>();
 
//.... a lot of a's like the following
if(countMap.keySet().contains(a)){
	countMap.put(a, countMap.get(a)+1);
}else{
	countMap.put(a, 1);
}

Read more

How to Convert Array to ArrayList in Java?

This article analyzes answers for a top-voted questions on Stack Overflow. The person who asked this question got a lot of reputation points, which could grant him permissions to do a lot of things on Stack Overflow. This does not make sense to me, but let’s take a look at the question first.

The question is “how to convert the following array to an ArrayList?”.

Read more

Java – Sort Map By Value

In Java, we can use the TreeMap class to sort a map by its keys. This class is very handy to use. However, sometimes we need to sort a map by its values. How to sort a map by its values is a most frequently asked question by Java programmers. In this post, I will … Read more

HashMap vs. TreeMap vs. Hashtable vs. LinkedHashMap

Map is one of the most important data structures in Java. In this post, I will illustrate how to use different types of maps, such as HashMap, TreeMap, HashTable and LinkedHashMap. 1. Map Overview There are 4 commonly used implementations of Map in Java SE – HashMap, TreeMap, Hashtable and LinkedHashMap. If we use only … Read more

HashSet vs. TreeSet vs. LinkedHashSet

A Set contains no duplicate elements. That is one of the major reasons to use a set. There are 3 commonly used implementations of Set: HashSet, TreeSet and LinkedHashSet. When and which to use is an important question. In brief, if you need a fast set, you should use HashSet; if you need a sorted … Read more

Sort LinkedList of User-Defined Objects in Java

For sorting a list in Java, you can use sort(List<T> list) method. This method can sort a list in which all elements must implement the Comparable interface. In the example below, the House class is user-defined. To make it comparable, it implements the Comparable interface. By using the sort(List<T> list) method, it can be sorted … Read more

Loop an Iterable in Java

If you use some Java API and it returns an Iterable, such as Iterable, you may want to loop this iterable. The method is pretty simple as follows: Iterable<String> result = … //result returned from some method. for(String s: result){ System.out.println(s); }Iterable<String> result = … //result returned from some method. for(String s: result){ System.out.println(s); } … Read more