Making a list unmodifiable

The list unmodifiable method is used to returns an unmodifiable view of the specified list. The Collections class provides a way to make a list unmodifiable: List ls = new ArrayList();List unmodifiableList = Collections.unmodifiableList(ls); If you want an unmodifiable list with one item you can use: List unmodifiableList = Collections.singletonList(“Only string in the list”); Moving objects around … Read more

Lists in Java with Examples

A list is an ordered collection of values. In Java, lists are part of the Java Collections Framework. Lists implement the java.util.List interface, which extends java.util.Collection. Sorting a generic list The Collections class offers two standard static methods to sort a list: sort(List list) applicable to lists where T extends Comparable, and sort(List list, Comparator … Read more

Immutable Empty Collections and iterators in Java with Examples

Sometimes it is appropriate to use an immutable empty collection. There are several ways to create immutable empty List in Java. The Immutable Empty Collections class provides methods to get such collections in an efficient way: List anEmptyList = Collections.emptyList(); Map anEmptyMap = Collections.emptyMap(); Set anEmptySet = Collections.emptySet(); These methods are generic and will automatically … Read more

Collections in Java

The collections framework in java.util provides a number of generic classes for sets of data with functionality that can’t be provided by regular arrays. Collections framework contains interfaces for Collection, with main sub-interfaces List and Set, and mapping collection Map. Collections are the root interface and are being implemented by many other collection frameworks. Removing … Read more

Array Covariance

Object arrays are covariant, which means that just as Integer is a subclass of Number, Integer[] is a subclass of Number[]. This may seem intuitive, but can result in surprising behavior: Integer[] integerArray = {1, 2, 3}; Number[] numberArray = integerArray; // valid Number firstElement = numberArray[0]; // valid numberArray[0] = 4L; // throws ArrayStoreException … Read more

Java Arrays

Creating a List from an Array The Arrays.asList() method can be used to return a fixed-size list containing the elements of the given array.. The resulting List will be of the same parameter type as the base type of the array. String[] stringArray = {“foo”, “bar”, “baz”};List stringList = Arrays.asList(stringArray); Note: This list is backed … Read more

Enums with constructors

Enums with Constructors

An enum cannot have a public constructor; however, private constructors are acceptable (constructors for enums are package-private by default): public enum Coin { PENNY(1), NICKEL(5), DIME(10), QUARTER(25); // usual names for US coins // note that the above parentheses and the constructor arguments match private int value; Coin(int value) { this.value = value; } public … Read more

Enum Map in Java

ENUM Map in Java

Java EnumMap class is the specialized Map implementation for enum keys. It inherits Enum and AbstractMap classes. the Parameters for java.util.EnumMap class. K: It is the type of keys maintained by this map. V: It is the type of mapped values. Enum Map Book Example import java.util.*; class Book { int id; String name,author,publisher; int … Read more

TreeMap and TreeSet in Java

Treemap and Treeset in Java

TreeMap and TreeSet are basic Java collections added in Java 1.2. TreeMap is a mutable, ordered, Map implementation. Similarly, TreeSet is a mutable, ordered Set implementation. TreeMap is implemented as a Red-Black tree, which provides O(log n) access times. TreeSet is implemented using a TreeMap with dummy values. Both collections are not thread-safe. TreeMap of … Read more

SortedMap in Java

The SortedMap in Java. The SortedMap interface extends Map. It ensures that the entries are maintained in ascending key order. Introduction to sorted Map Keypoint: SortedMap interface extends Map. entries are maintained in an ascending key order. Methods of sorted Map : Comparator comparator( ).Object firstKey( ).SortedMap headMap(Object end).Object lastKey( ).SortedMap subMap(Object start, Object end).SortedMap tailMap(Object start). Example: public … Read more

WeakHashMap in Java

WeakHashMap in Java

Concepts of weakHashmap in Java Concepts of WeakHashmap Key Points: Implementation of Map. stores only weak references to its keys. Weak References: The objects that are referenced only by weak references are garbage collected eagerly; the GC won’t wait until it needs memory in that case. Difference between Hashmap and WeakHashMap: If the Java memory … Read more

LinkedHashMap in Java

LinkedHashMap in Java

LinkedHashMap in Java class is the Hash table and Linked list implementation of the Map interface, with predictable iteration order. It inherits the HashMap class and implements the Map interface. The important points about Java LinkedHashMap class are: A LinkedHashMap contains values based on the key. It contains only unique elements. It may have one … Read more

List vs Set in Java

List vs Set in Java

Difference between List and Set in Java. The list is a type of ordered collection that maintains the elements in insertion order while Set is a type of unordered collection so elements are not maintained any order. The list allows duplicates while Set doesn’t allow duplicate elements. Related Article: The Set Interface in Java List vs Set import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public … Read more

The Set Interface in Java

The Set Interface in Java

The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited. Initialization A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction. Set have its implementation in various classes like HashSet, TreeSet, LinkedHashSet. For example: HashSet: Set set = new HashSet(); Here … Read more

Java Nested and Inner Classes

Using Java, developers have the ability to define a class within another class. Such a class is called a Nested Class. Nested Classes are called Inner Classes if they were declared as non-static, if not, they are simply called Static Nested Classes. This page is to document and provide details with examples of how to … Read more

Reference Data Types & Java Compiler – ‘javac’

Java-programming-tutorials

Reference datatypes in java are those which contains reference/address of dynamically created objects. These are not predefined like primitive data types. Dereferencing In Java Dereferencing happens with the . operator: Object obj = new Object();String text = obj.toString(); // ‘obj’ is dereferenced. The Dereferencing follows the memory address stored in a reference, to the place in … Read more

Arrays in Java

Arrays in Java

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the arrays are created. After creation, its length is fixed. Parameter Details ArrayType Type of the array. This can be primitive (int, long, byte) or Objects (String, MyObject, etc). index Index refers to the position of … Read more