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

Queues and Deques Interfaces in Java with Examples

Queues and Deques Interfaces in Java

The current Deques interface in java. Utility Package is a Queue interface subtype. The Deque is related to the double-ended queue that supports elements from either end of the data structure being added or removed. It can either be used as a queue(first-in-first-out/FIFO) or as a stack(last-in-first-out/LIFO). The usage of the PriorityQueue PriorityQueue is a … Read more

Bit Manipulation in Java Tutorial

Bit Manipulation in Java Tutorial

Bit Manipulation in Java – Bitwise and Bit Shift operations. Java enables you to manipulate integers on a bit level, which means operating on specific bits, which represent an integer number. In some cases, it can be really handy. Bit Manipulation Checking, setting, clearing, and toggling individual bits. Using long as bit mask Assuming we want to modify bit n of an integer primitive, i (byte, … Read more

Comparing BigIntegers in Java

Comparing BigIntegers in Java

You can compare BigIntegers the same as you compare String or other objects in Java. For example: BigInteger one = BigInteger.valueOf(1); BigInteger two = BigInteger.valueOf(2); if(one.equals(two)){ System.out.println(“Equal”); } else{ System.out.println(“Not Equal”); } Output: Not Equal Note: In general, do not use use the == operator to compare BigIntegers == operator: compares references; i.e. whether two … Read more

NumberFormat in Java

Number Format in Java

Java NumberFormat tutorial shows how to format numbers in Java.  Different countries have different number formats and considering this we can have different formats using Locale of java. Using locale can help in formatting Locale locale = new Locale(“en”, “IN”);NumberFormat numberFormat = NumberFormat.getInstance(locale); using above format you can perform various tasks Format Number numberFormat.format(10000000.99); Number … Read more

Mathematical operations with BigDecimal in Java

Mathematical Operations in Java

This example shows how to perform basic mathematical operations using BigDecimal in Java. 1.Addition BigDecimal a = new BigDecimal(“5”);BigDecimal b = new BigDecimal(“7”);//Equivalent to result = a + bBigDecimal result = a.add(b);System.out.println(result); Result : 12 2.Subtraction BigDecimal a = new BigDecimal(“5”);BigDecimal b = new BigDecimal(“7”);//Equivalent to result = a – bBigDecimal result = a.subtract(b);System.out.println(result); Result … Read more

BigDecimal in Java

Bigdecimal in Java

The BigDecimal in java class provides operations for arithmetic (add, subtract, multiply, divide), scale manipulation, rounding, comparison, hashing, and format conversion. The BigDecimal represents immutable, arbitrary-precision signed decimal numbers. This class shall be used in the necessity of high-precision calculation. Comparing BigDecimals The method compareTo should be used to compare BigDecimals: BigDecimal a = new … Read more

Java LocalTime

Java LocalTime

Java LocalTime class is an immutable class that represents time with a default format of hour-minute-second. It inherits Object class and implements the Comparable interface. Method Output LocalTime.of(13, 12, 11) 13:12:11 LocalTime.MIDNIGHT 00:00 LocalTime.NOON 12:00 LocalTime.now() Current time from system clock LocalTime.MAX The maximum supported local time 23:59:59.999999999 LocalTime.MIN The minimum supported local time 00:00 … Read more

Usage of various classes of Date Time API

Usage of various classes of Date Time API

The following example also has an explanation required for understanding the example within it. import java.time.Clock; import java.time.Duration; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.TimeZone; public class SomeMethodsExamples { /** * Has the methods of the class {@link LocalDateTime} / public static void checkLocalDateTime() { LocalDateTime localDateTime = … Read more

Dates and Time (java.time.*)

Dates And Time in Java

Calculate Difference between 2 LocalDates Use LocalDate and ChronoUnit: LocalDate d1 = LocalDate.of(2017, 5, 1);LocalDate d2 = LocalDate.of(2017, 5, 18); now, since the method between of the ChronoUnit enumerator takes 2 Temporals as parameters so you can pass without a problem the LocalDate instances long days = ChronoUnit.DAYS.between(d1, d2);System.out.println( days ); Related Article: How to … Read more