Found 9179 Articles for Object Oriented Programming

How to iterate LinkedHashMap in Java?

Deepti S
Updated on 19-Oct-2023 12:23:08

3K+ Views

LinkedHashMap is a class that is identical to HashMap, however it additionally provides a feature that tracks the order wherein components are inserted. The sequence wherein elements were added isn't preserved by using HashMap, even though it does permit for quick element insertion, search, and deletion. By keeping a linked list of every entry in the map, LinkedHashMap addresses this issue. The elements are saved in the sequence that they were introduced thanks to this linked list. As a result, while iterating over a LinkedHashMap, the elements are returned according to how they were added. Methods Used To iterate LinkedHashMap ... Read More

How to Iterate Through HashTable in Java?

Deepti S
Updated on 19-Oct-2023 12:20:05

3K+ Views

The HashTable is a fundamental data structure that operates on the basis of key hashcodes without preserving the insertion order. It prohibits duplicate keys but allows for duplicate values. Remarkably, it accommodates a wide range of objects for both keys and values, fostering heterogeneity. Null values for keys and values are not allowed, though, as doing so would cause a RunTimeException called a NullPointerException. The HashTable implements the serializable and cloneable interfaces in terms of interfaces, but it fails to implement the RandomAccess interface. Additionally, all the methods within the HashTable are synchronized, ensuring thread safety for HashTable objects. When ... Read More

How to Iterate LinkedList in Java?

Deepti S
Updated on 19-Oct-2023 12:16:22

227 Views

The LinkedHashMap Class is similar to HashMap. But it has an additional feature in comparison to HashMap. The LinkedList class belongs to the java.util package. A doubly linked list is how LinkedList stores its elements. Given that our operations typically include insertion and deletion, LinkedList is the best option. The java.util package contains the LinkedList collection framework. It acts as an implementation of the non-contiguous LinkedList data structure, which saves elements in memory. Methods Used There are five main methods that you can use for iterating HashMap − Using the for loop Making use of a while loop Using ... Read More

How to Iterate the Vector Elements in the Reverse Order in Java?

Deepti S
Updated on 18-Oct-2023 16:35:11

240 Views

The Vector class has been a part of the Java collection system since Java version 1.2. Vectors are sometimes known as Dynamic Arrays because, unlike ordinary arrays, they can expand and contract in size. Ensuring thread-safety, Vectors are synchronized. While there exists a third approach that involves using Apache Commons to iterate through the vector in reverse, this method necessitates the downloading of additional jar files and packages, which is not widely supported by most systems. Essentially, there are only two primary methods to traverse through vector elements in reverse order. Methods Used There are two methods used here − ... Read More

How to Invoke Method by Name in Java Dynamically Using Reflection?

Deepti S
Updated on 18-Oct-2023 16:29:33

1K+ Views

The Reflection API in Java allows you to dynamically call any method using its string name. When using the java.lang.reflect API, you can load classes by name even if they aren't accessible at compile time thanks to reflection's robust mechanism. It empowers you to dynamically invoke any method using reflection and makes it possible you to retrieve all methods from a class, including private and public ones. Those who are unfamiliar with Java might find this idea strange. With no explicit code for calling the method during compilation, Java can execute a method when its name is provided as a ... Read More

How to iterate over a TreeMap in Java?

Deepti S
Updated on 18-Oct-2023 16:25:59

3K+ Views

With the help of the AbstractMap Class, Java's TreeMap implements the Map interface and NavigableMap. This map is sorted using either the keys' inherent ordering or a Comparator that was provided when the map was created. When working with a TreeMap in Java, the objective is to iterate via its elements. However, due to TreeMap not being a Collection, we cannot directly iterate over it using iterators. Methods Used To iterate through a TreeMap in Java, we need to utilize the TreeMap.entrySet() method. This function gives back a collection-view (SetMap.Entry>) of all the mappings that were saved in the TreeMap. ... Read More

How to Iterate HashMap in Java?

Deepti S
Updated on 18-Oct-2023 16:24:21

677 Views

(Key, Value) pairs are used to store data in Java HashMap collections. Although it is unsynchronized, it is comparable to HashTable. As a result, a HashMap can be accessed by numerous threads without encountering any troubles. Despite the fact that HashMap allows for the storage of null keys as well, there can most effective be one null key object and an infinite wide variety of null values. regarding the order of the map, this class gives no guarantees. To index the value, the key is utilised. We can store unique keys with HashMap. If we try to insert one, the ... Read More

How to iterate HashSet in Java?

Deepti S
Updated on 18-Oct-2023 16:22:29

3K+ Views

The Java HashSet class employs a hash table internally to implement the Set interface, which uses a HashMap instance. The class does not guarantee that the items will remain the same over time because the iteration order of the factors within the hash set is not always guaranteed. Null elements may be included in this class. The hash function provides efficient performance for fundamental operations like add, delete, contains, and size, assuming it effectively distributes the elements through the buckets. The Set interface is implemented by HashSet, which extends AbstractSet. It creates a collection using the hashing technique and saves ... Read More

How to iterate over a 2D list (list of lists) in Java?

Deepti S
Updated on 18-Oct-2023 16:03:46

3K+ Views

A data structure known as a 2D list or list of lists can be utilised for saving a collection of data in a two-dimensional format. A row in the 2D list is represented by each inner list in this list of lists. A 2D list could be utilised, for instance, to store the data for a chessboard, where every component in the list corresponds to a single square on the board. Methods Used A 2D list can be iterated in one of the two methods that follow − Using Loops Using iterator Method 1: Employing Loops There are ... Read More

How to Iterate LinkedHashMap in Reverse Order in Java?

Deepti S
Updated on 18-Oct-2023 16:01:17

2K+ Views

The LinkedHashMap serves the purpose of maintaining the precise order of element addition. It elucidates the method for accessing elements in the sequence they were inserted. In addition to storing values based on their keys, the LinkedHashMap class expands upon the functionalities of the Hash Map class and implements the Map interface. It exclusively accommodates unique elements or mappings. It affords us the flexibility to utilize various data types, such as text, float, integer, etc., for assigning keys and values. By initially reversing the elements, we can alter the order of elements in the linked hash map. Subsequently, we can ... Read More

Advertisements