Java Core
    Java - Overview
    What is new in Java 21
    What is new in Java 17
    What is new in Java 11
    Java - JVM
    Java - Deprecated
    Java - Numbers
    Java - String
    Java - Time
    Java - Collections
    Java - Lambda
    Java - Exceptions
    Java - Switch
    Java - IO
    Java - Jar
    Java - Generics
    Java - Environment
    Java - Concurrency
    Java - Trouble Shooting
    Java - Install
    Java - Misc

Java - Collections

Updated: 2021-11-19

Java 7 vs Java 8: HashMap

HashMap collision: if two entries have the same key, they will be saved internally as follows

  • Java 7: use LinkedList, will be slow if there are too many collisions
  • Java 8: use red-black tree

Using Java 8 HashMaps buckets with too

Suppose we have a Person class defined and may have the following operations:

Map<Person, String> map = new HashMap<>();
map.put(person, “value");

In Java 8:

  • If there are too many entries under the same bucket, it will be tree-fied once a threshold value is met.
  • If Person is comparable, it will be much faster.
public int compareTo(Person person) {

Sort Map

public static <K, V extends Comparable<V>> List<Map.Entry<K,V>> getEntriesSortedByValues(Map<K,V> map){
    List<Map.Entry<K,V>> entries = new LinkedList<Map.Entry<K,V>>(map.entrySet());

    Collections.sort(entries, new Comparator<Map.Entry<K,V>>() {

        public int compare(Entry<K, V> o1, Entry<K, V> o2) {
            return o1.getValue().compareTo(o2.getValue());

    return entries;

The right way to print an array


Increment Count

private void incMapCnt(Map<String, Integer> map, String key) {
    int cnt = map.containsKey(key) ? map.get(key) : 0;
    map.put(key, cnt + 1);

Set Operation


Set<Type> union = new HashSet<>(s1);


Set<Type> intersection = new HashSet<>(s1);


Set<Type> difference = new HashSet<>(s1);

Unique and duplicates

Set<String> uniques = new HashSet<>();
Set<String> dups    = new HashSet<>();

for (String a : args)
    if (!uniques.add(a))

// Destructive set-difference

Set<Type> symmetricDiff = new HashSet<>(s1);
Set<Type> tmp = new HashSet<>(s1);

Array to list:

List<String> list = Arrays.asList(args);

Initialize Array/List

Arrays.asList(1, 2, 3, 4, 5)
new Integer[]{1, 2, 3, 4, 5}

Arrays.asList() UnsupportedOperationException

Arrays.asList() is a convenient way to create a List by literals:

List<Integer> l = Arrays.asList(0,1,2);

Arrays.asList() returns a fixed-size list that does not support any structural modification(add or remove).


This will throw UnsupportedOperationException

Caused by: java.lang.UnsupportedOperationException
	at java.util.AbstractList.add(
	at java.util.AbstractList.add(


List<Integer> list = new ArrayList<>(Arrays.asList(0));


static class LinkedListNode<E> {
    E data;
    LinkedListNode<E> next;

null in HashSet

null can be a unique value in a HashSet, and can be printed out:

public void testNullInSet() {
    Set<String> set = new HashSet<>();

Convert Array to String

String[] arr = new String[]{"foo", "bar"};

Array derives from Object, so there's a toString(), however it does not print the content but the hash: [Ljava.lang.String;@48f2f70c

String s = arr.toString();

Use Arrays.toString() instead: [foo, bar]

String s = Arrays.toString(arr);

To quoted Strings: ["foo","bar"]

ObjectMapper objectMapper = new ObjectMapper();
String s = objectMapper.writeValueAsString(arr);

Multi-dimension Array, use Arrays.deepToString()

String[][] arr = new String[][]{{"foo1", "bar1"}, {"foo2", "bar2"}};
//[[foo1, bar1], [foo2, bar2]]

Array vs List

String[] is a subset of Object[]:

//class [Ljava.lang.String;



String[] values = new String[]{"a", "b", "c"};

System.out.println(values instanceof String[]);
System.out.println(values instanceof Object[]);

However List<String> is NOT a subset of List<Object>.

Assume we have testList:

private void testList(List<Object> values) {}

this will cause compile error:

public void test() {
    List<String> stringList = Arrays.asList("a", "b", "c");

Compile Error:
    required: java.util.List<java.lang.Object>
    found: java.util.List<java.lang.String>

This is ok:

private void testArray(Object[] values) {}

public void test() {
    String[] stringArray = new String[]{"a", "b", "c"};

List to Array

List<String> x = ...

though x is List<String>, toArray() will return Object[], to return String[]:

String[] y = x.toArray(new String[0]);

Collections.unmodifiableList vs ImmutableList

  • Collections.unmodifiableList accepts null
  • ImmutableList(Guava) rejects null

HashMap vs. Map

A: Map is Interface, Hashmap is the class implements Map

HashMap vs. Hashtable

  • HashMap: not synchronized; permit NULL; order may change.
  • Hashtable: synchronized; not permit NULL; constant order.

Vector vs ArrayList

  • Vector is synchronized; ArrayList is not.

Array vs ArrayList


  • Fixed length
  • can be primitive types


  • Auto expands
  • Object only, no primitive types, use wrapper classes(Integer, Double, Character)


In the version 8 of Java, the Collection HashMap has been modified such that instead of using a LinkedList to store different elements with colliding hashcodes, a Red-Black tree is used. This results in the improvement of time complexity of searching such an element from O(n) to O(log n)