Tag: collection

Map in Java

Java
Tables Map Overview HashMap TreeMap Hashtable LinkedHashMap Map Overview ⤴ There are 4 commonly used implementations of Map in Java SE - HashMap, TreeMap, Hashtable and LinkedHashMap. If we use only one sentence to describe each implementation, it would be the following: HashMap is implemented as a hash table, and there is no ordering on keys or values. TreeMap is implemented based on red-black tree structure, and it is ordered by the key. LinkedHashMap preserves the insertion order. Hashtable is synchronized, in contrast to HashMap. It has an overhead for synchronization. This is the reason that HashMap should be used if the program is thread-safe. HashMap ⤴ If the key of a HashMap is a self-defined object, then the equals() and hashCode() contr

List in Java

Java
Tables List Overview ArrayList vs. LinkedList vs. Vector Example ArrayList example LinkedList example Vector Performance of ArrayList vs. LinkedList List Overview ⤴ List, as its name indicates, is an ordered sequence of elements. When we talk about List, it is a good idea to compare it with Set which is a set of unique and unordered elements. The following is the class hierarchy diagram of Collection. From the hierarchy diagram you can get a general idea of Java Collections. ArrayList vs. LinkedList vs. Vector ⤴ From the hierarchy diagram, they all implement List interface. They are very similar to use. Their main difference is their implementation which causes different performance for different operations. ArrayList is implemented as a resizabl

Set in Java

Java
Tables Set Interface HashSet vs. TreeSet vs. LinkedHashSet Example TreeSet Example HashSet Example LinkedHashSet Example Set Interface ⤴ Set interface extends Collection interface. In a set, no duplicates are allowed. Every element in a set must be unique. You can simply add elements to a set, and duplicates will be removed automatically. HashSet vs. TreeSet vs. LinkedHashSet ⤴ HashSet is Implemented using a hash table. Elements are not ordered. The add, remove, and contains methods have constant time complexity O(1). TreeSet is implemented using a tree structure(red-black tree). The elements in a set are sorted, but the add, remove, and contains methods has time complexity of O(log(n)). It offers several methods to deal with the ordered set like

Arrays in java

Java
Array is a fundamental construct in any programming languages, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Tables Array Declaration Array Instantiation ArrayIndexOutOfBoundsException Java Array Default Values Iterating a Java Array Multidimensional Arrays Iterate a java multidimensional array Sort a Java array Copy a Java array The Arrays Class Array Declaration ⤴ Syntax for declaring an array variable in Java dataType[] arrayRefVar; // preferred way dataType arrayRefVar[]; // for accommodate C/C++ programmers, but not preferred, Array Instantiat

Collection in Java

Java
Tables Collection vs. Collections Class hierarchy of Collection Class hierarchy of Map Collection Classes Summary Table Code Example Collection vs. Collections ⤴ First of all, “Collection” and “Collections” are two different concepts. As you will see from the hierarchy diagram below: “Collection” is a root interface in the Collection hierarchy. but “Collections” is a class which provide static methods to manipulate on some Collection types. Class hierarchy of Collection ⤴ Class hierarchy of Map ⤴ Collection Classes Summary Table ⤴ Code Example ⤴ List<String> a1 = new ArrayList<String>(); a1.add("Program"); a1.add("Creek"); a1.add("Java"); a1.add("Java"); System.out.println("ArrayList Elements"); System.out.print("\t" + a