Java Collections:

  • java Collections are like containers that group multiple items into one unit.
  • Examples: A jar of chocolate, a list of names, etc.
  • The collection comes in almost every programming language and when it comes to Java, it also comes with some archive classes such as lists, sets, maps, etc.
  • In this session, I will cover ArrayList and HashMap which is commonly used in Selenium Automation test.

 

1. ArrayList: 

  • The Java ArrayList class uses a dynamic array to store the elements.
  • This increases the abstract class and implements the list interface.
  • Java error class may contain duplicate elements.
  • Java ArrayList retains the class insertion order.
  • The Java ArrayList allows random access because the array works based on the index.

 

Syntax:

 

Example:
In the following example, I have described how the array list object is created, how to add values to the array list, how to get an array of array item items, how to remove an array list, how to reset array list and print item.

Output:

 

2. Hashmap:

  • A hashmap basically specifies a unique key for those values that can be recovered at any point.
  • Java HashMap is a class that is used to perform tasks such as putting, removing and detecting elements in a map. We create a map, where we pass two types of values that are ‘key’ and ‘value’.
  • When using the Hashmaps, the values will be put into Hasham and whenever the user gets a value, the key will be used to use the value.
  • The map is an interface that is the map keys for elements, maps are organized and uncategorized without permission, they allow a blank key and multiple zeros values.
  • Values are stored in value and value A key or multiple values can be blank in the entire hememap. A key can be any object in Java Collections.

 

Creation of HashMap:

 

Add any element in HashMap you need to provide 2 thing, key and value.

  • Key : key with which specified value will be associated. null is allowed.
  • Value : value to be associated with specified key.

 

Syntax: 

 

Example:

In following example I have described how to create hashmap object, how to add and remove values, How to get size of hashmap, How to clear the hashmap, etc.

Output: