Skip to main content

Posts

Showing posts from December, 2019

Java TreeMap With Examples

TreeMap in Java is one of the implementation of the Map interface. How it differs from the other implementation HashMap is that unlike HashMap which is unordered, TreeMap is Sorted . TreeMap is sorted according to the natural ordering of its keys by default. If you want different sort ordering then you will have to provide a Comparator at the TreeMap construction time. Java TreeMap is a Red-Black tree based implementation of Map. TreeMap class extends AbstractMap and implements NavigableMap , Cloneable and Serializable interfaces. Features of TreeMap Some of the features of the TreeMap in Java which are discussed in this post are as follows- In TreeMap elements are sorted by keys. In TreeMap values may be duplicate but a key has to be unique. TreeMap doesn’t permit null keys, null values are permitted though. TreeMap in Java is not thread safe. The iterators returned by all of TreeMap’s "collection view methods" are fail-fast. Which means,

Java ArrayList With Examples

In Java programming language when you create an array you will have to provide its length. Once created, array is of fixed length and that length can’t be changed. In many cases you may not know the exact length in advanced in such scenarios you can use ArrayList in Java . Table of contents ArrayList class in Java Features of ArrayList in Java Java ArrayList Constructors Java example creating an ArrayList Methods in the ArrayList class ArrayList allows duplicate values and null Java example to remove elements from an ArrayList Adding primitive data types to an ArrayList Java ArrayList iterator with iteration examples ArrayList is not thread-safe ArrayList class in Java ArrayList in Java is a resizable-array implementation of the List interface and resides in java.util package. Since ArrayList is dynamic so it can grow or shrink automatically. Internally ArrayList uses an array which is created with an initial capacity. If that capac

Java Collections Framework Tutorial

Java Collections framework is an important API in Java programming language. In any Java application if you have to store objects you will certainly use one of the data structure defined in the Java collections. This Java collections tutorial gives an overview of Java collections framework; interfaces and classes that constitute the collections framework and the Java collections hierarchy. What is Java Collections framework A collection can be defined as a container that can store multiple elements into a single container. Collections framework in Java provides a unified architecture for defining such container classes that can store, retrieve and manipulate group of elements. Java Collections framework contains the following- Interfaces - Interfaces in the Java collections are the abstract data types that represent collections. These interfaces provide the generalized structure for the collection which are then implemented to provide specialized implementations. For exam

Java Condition Interface

Condition interface which resides in java.util.concurrent.locks has methods for inter thread communication similar to Object class monitor methods ( wait, notify and notifyAll ). Condition provides methods like await(), signal(), signalAll(). Where a Lock replaces the use of synchronized methods and statements, a Condition replaces the use of the Object monitor methods. Some of the methods defined in java.util.concurrent.locks.Condition interface are given below. await() - Causes the current thread to wait until it is signalled or interrupted. await(long time, TimeUnit unit) - Causes the current thread to wait until it is signalled or interrupted, or the specified waiting time elapses. awaitNanos(long nanosTimeout) - Causes the current thread to wait until it is signalled or interrupted, or the specified waiting time elapses. awaitUninterruptibly() - Causes the current thread to wait until it is signalled. awaitUntil(Date deadline) - Causes the current threa