What are Java HashMaps?
There are several methods available in HashMap
Object put (object key, object value)
Enumeration keys () – it will fetch keys
Enumeration elements () – it will fetch elements
Object get (object keys) – pass the key and get the value associated with it
Boolean contains key (object key) – used for checking whether a key is present in HashMap or not
Boolean contains value (object key) – pass the key
Object remove (object key) – pass the key and remove the object
Int size () – for using size
String to String () – for converting into string
There are corresponding values for each key where values can be null in the HashMap also.
Creation of HashMap.
HashMap hashmap = new HashMap();
Putting elements
hashmap.put(“Ankita”, 9634.58);
hashmap.put(“Vishal”, 1283.48);
hashmap.put(“Gurinder”, 1478.10);
hashmap.put(“Krishna”, 199.11);
Here, we pass key and the value.
Displaying the value – Get an iterator
Iterator iterator = hashmap.entrySet().iterator();
Here, the values are present in the set so we use entrySet.
Along with the line:
While(iterator.hasNext()){
Map.Entry entry=(Map.Entry) iterator.next();
System.out.print(entry.getKey()+”:”);
System.out.printIn(entry.getValue());
}
What is ArrayList class?
The ArrayList class is a concrete implementation of the list interface. It allows duplicate elements. Also a list can grow or shrink dynamically which is one of its main advantages. The demerit of an array is that once it is created it remains fixed.
Here, the size of ArrayList is 5. Index starts from 0.
Steps to be followed:
1) Go to C Drive
2) Go to Program Files/ JAVA/ JDK
3) Open RAR file named ‘SRC’
4) Here, we will find Java and util and within that we can find ArrayList.
Methods in ArrayList
- Boolean add (object e) – we can pass the object using the add function
- Void add (int index, object element) – we pass the index and the element that has to be added
- Boolean addALL (Collection c) – we can pass the entire collection
- Object get (int index) – we pass the index
- Object set (int index, object element) – we pass the index and object to get the value
- Object remove (int index) – we can pass the index and remove the value
- Iterator iterator () – for fetching techniques
- ListIterator listIterator ()
- Int indexOf ()– indexer gives the index of particular element
- Int lastIndexOf ()
- Int index (object element)
- Int size ()– gives the size of ArrayList
- Void clear ()– clears the ArrayList
Understanding ArrayList
Creating object of an Arraylist
//Create an arraylist
ArrayList<String> arraylist = new ArrayList<String>();
We use the add function to add values
Arraylist.add(“rose”);
Arraylist.add(“lily”);
Arraylist.add(“jasmine”);
Arraylist.add(“rose”);
Here, duplicate values have been posted in ArrayList.
Also, we use Index 2 to remove element
ArrayList.remove(2);
The elements are displayed with the coding
Iterator<String> iterator = arrayList.iterator();
Using the iterator, we can access the elements from the function hasNext used here:
While(iterator.hasNext()){
String element = iterator.next().toString();
System.out.println(element + “ “);
}
It converts value to string and prints the element.
In the statement:
System.out.println(arraylist.indexOf(“Rose”));
This will give us an output:
Rose Lily Rose 0
Here, size is not an exception. It will read the first element and not the second element (Rose).
We again alter the statement:
System.out.println(arraylist.get(2)));
The output will be:
Rose Lily Rose Rose
How to trace elements of ArrayList?
We can use items such as
- For-each loop
- Iterator
- ListIterator
- Enumeration
For-each loop – Its action is similar for loop. It races through all the elements of array or ArrayList.
We use :
For ( String s : ArrayList_name)
For – keyword
String – type of data stored in ArrayList
ArrayList_name – name of ArrayList
We don’t need to mention size of ArrayList. But in case of a simple loop we need to mention the size.
Got a question for us? Mention them in the comments section and we will get back to you.
Related Posts: