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:
1 | ArrayList<String> al=new ArrayList<String>(); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import java.util.*; class Arraylist_Java { public static void main(String args[]) { // creating arraylist ArrayList<String> myList = new ArrayList<String>(); // adding object in arraylist myList.add("Pune"); myList.add("Mumbai"); myList.add("Delhi"); myList.add("Hyderabad"); // To get the Index of an Item from arraylist. System.out.println(myList.indexOf("Delhi")); // To remove an Item from arraylist. myList.remove(1); // To reset value of an arraylist item. myList.set(2, "Bangalore"); // getting Iterator from arraylist to traverse elements Iterator<String> itr = myList.iterator(); // Printing each elements using Iterator while (itr.hasNext()) { System.out.println(itr.next()); } // Printing Element with for each loop for (String obj : myList) System.out.println(obj); } } |
Output:
1 2 3 4 5 6 7 | 2 Pune Delhi Bangalore Pune Delhi Bangalore |
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:
1 | HashMap hashmap = new 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:
1 | HashMap<Integer,String> map=new HashMap<Integer,String>(); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import java.util.HashMap; import java.util.Map; class Hashmap_Java { public static void main(String args[]) { HashMap<Integer, String> map = new HashMap<Integer, String>(); map.put(100, "Mumbai"); map.put(101, "Delhi"); map.put(102, "Pune"); // Add Element map.put(103, "Surat"); // Size of map System.out.println(map.size()); // clears hashmap , removes all element map.clear(); // Remove element from hashmap map.remove(100); // Checking if HashMap is empty System.out.println("Is HashMap is empty: " + map.isEmpty()); for (Map.Entry m : map.entrySet()) { System.out.println(m.getKey() + " " + m.getValue()); } } } |
Output:
1 2 | 4 Is HashMap is empty: true |