JavaRush /Java Blog /Random EN /Coffee break #132. Let's take a look at ZGC, the newest g...

Coffee break #132. Let's take a look at ZGC, the newest garbage collector in the JDK. What is the difference between HashMap, LinkedHashMap and TreeMap in Java

Published in the Random EN group

Introducing ZGC, the JDK's newest garbage collector

Source: Inside Java ZGC or Z Garbage Collector is the most modern garbage collector in the JDK. It originally appeared in JDK 11 as an experimental feature. ZGC became a production feature in JDK 15 after the JEP 377 enhancement was approved. In this article, we will look at the goals and future of ZGC. Coffee break #132.  Let's take a look at ZGC, the newest garbage collector in the JDK.  What is the difference between HashMap, LinkedHashMap and TreeMap in Java - 1

ZGC Review

ZGC is designed to be a scalable, low-latency garbage collector. The maximum pause time during ZGC operation rarely exceeds 250 microseconds. Yes, exactly microseconds, with an average pause time of tens of microseconds. ZGC is highly scalable, with minimum heap sizes ranging from 8 MB to 16 TB. It is important to note that the pause time does not increase with heap size. Thus, even with a heap of several terabytes in size, the pause time will still be measured in microseconds. Although ZGC provides low latency and high scalability, it comes at the cost of throughput that is approximately 10% lower compared to G1. The exact throughput reduction depends on the application design, system architecture, and business needs.

Using ZGC

Since G1 is the default garbage collector starting with JDK 9, you will need to set the VM flag -XX:+UseZGC to use ZGC. When using ZGC, the most important configuration is to set the maximum heap size, -Xmx. The heap size should be large enough to handle your application's live-set. It must also have additional reserve to perform garbage collection. The more space available on the heap, the less frequently garbage collection will be required. But note that this must be balanced with memory usage.

Further development of ZGC

Since its release as a production feature in JDK 15, ZGC has continued to be actively developed. With the release of JDK 16, it introduced thread stack parallel processing (JEP 376), and JDK 18 added row deduplication as an additional feature. In the future, it is planned that ZGC will become multi-generational, although the specific time frame for implementing this improvement has not yet been established.

More information about ZDC

What is the difference between HashMap, LinkedHashMap and TreeMap in Java

Source: Rrtutors If you want to store key-value pairs in a Java program, then Java collections offer many options for this depending on your needs. These include LinkedHashmap , HashMap and TreeMap . The key differences between these three classes lie in their internal implementation and the specific application in certain cases. Coffee break #132.  Let's take a look at ZGC, the newest garbage collector in the JDK.  What is the difference between HashMap, LinkedHashMap and TreeMap in Java - 2

Differences between HashMap, LinkedHashMap and TreeMap in Java

Here are the main differences between the three mentioned classes based on implementation, ordering, sorting, and support for null keys and values.

Implementation

Both HashMap and LinkedHashMap implement the Map interface , while TreeMap implements the Map , NavigableMap , and vSortedMap interfaces. LinkedHashMap is implemented as a double-linked list bucket, HashMap is implemented as a hash table, and TreeMap is implemented as a tree.

Ordering and sorting

  • HashMap makes no guarantees regarding the order of iterations. However, it can change completely when new elements are added.

  • LinkedHashMap will iterate in the order in which the entries were placed into the map.

  • TreeMap iterates according to the “natural order” of the keys according to their compareTo() method (or external Comparator). It also implements the SortedMap interface , which contains methods that depend on the sort order.

Null Keys and Values

HashMaps and LinkedHashMap support null values ​​as well as key values, while TreeMaps do not support null values ​​as they support natural element types. For a visual example, let's create a HashMap , LinkedHashMap and TreeMap .
import java.util.HashMap;

import java.util.LinkedHashMap;

import java.util.Map;

import java.util.TreeMap;

public class HashMap_LinkedHashMap_TreeMap {

            public static void main(String[] args) {

                  Map Treemapu = new TreeMap();

                  Treemapu.put("First", "Java");

                  Treemapu.put("Second", "Python");

                  System.out.println("TreeMap values are: \n" + Treemapu);

                  Map Hashimapu = new HashMap();

                  Hashimapu.put("First", "Java");

                  Hashimapu.put("Second", "Python");

                  System.out.println("HashMap Values are: \n" + Hashimapu);

                  Map LinkedHashiMapu = new LinkedHashMap();

                  LinkedHashiMapu.put("First", "Java");

                  LinkedHashiMapu.put("Second", "Python");

                  System.out.println("LinkedHashMap values are: \n" + LinkedHashiMapu);

            }

}
At the output we get:
{First=Java, Second=Python} HashMap Values ​​are: {Second=Python, First=Java} LinkedHashMap values ​​are: {First=Java, Second=Python}
Thus, all three classes represent a mapping from unique keys to values ​​and therefore implement the Map interface .
  • HashMap is a map based on key hashing. It supports O(1) get/put operations. Keys must have consistent hashCode() and equals() implementations .

  • A LinkedHashMap is very similar to a HashMap , but it has an order in which elements are added (or accessed), so the iteration order is the same as the placement order (or access order, depending on the design parameters).

  • TreeMap is a tree-based mapping. Its put/get operations take O(log n) time. This requires that the elements have some kind of comparison mechanism, either with a comparison or a comparator. The order of iterations is determined by this mechanism.

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION