Java/J2EE and SOA (346 Blogs) Become a Certified Professional

Dynamic Data Allocation in Java

Last updated on Oct 09,2020 12.8K Views


What are Java HashMaps?

Java Logo - EdurekaJava 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.

Dynamic Data Allocation in Java

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:

Hadoop for Java Professionals

Learn Java/J2EE & SOA

Upcoming Batches For Java Certification Training Course
Course NameDate
Java Certification Training Course

Class Starts on 28th January,2023

28th January

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 25th February,2023

25th February

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Dynamic Data Allocation in Java

edureka.co