Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
Java HashMap is a class which is used to perform operations such as inserting, deleting and locating elements in a map. We create a map, where we pass two kinds of values which are ‘key’ and ‘value’. While using HashMaps, values will be put in HashMap and whenever the user retrieves a value, the key will be used in order to use the value. The map is an interface that maps keys to the elements. Maps are unsorted and unordered. They allow one null key and multiple null values. The values are stored in key and value. One key or multiple values could be null in the entire HashMap. A key can be any object.
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.
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
We can use items such as
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:
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 7th December,2024 7th December SAT&SUN (Weekend Batch) | View Details |
edureka.co