0% found this document useful (0 votes)
14 views

Java Assignment Vtu SMVIT

Uploaded by

dsilvacarolm17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Java Assignment Vtu SMVIT

Uploaded by

dsilvacarolm17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Java ArrayList class uses a dynamic array for storing the elements. It is like an
array, but there is no size limit. We can add or remove elements anytime. So, it
is much more flexible than the traditional array. It is found in
the java.util package.
1. import java.util.*;
2. public class ArrayListExample1{
3. public static void main(String args[]){
4. ArrayList<String> list=new ArrayList<String>();//Creating arraylist
5. list.add("Mango");//Adding object in arraylist
6. list.add("Apple");
7. list.add("Banana");
8. list.add("Grapes");
9. //Printing the arraylist object
10. System.out.println(list);
11. }
12. }

The LinkedList class is a collection which can contain many objects of


the same type, just like the ArrayList.

The LinkedList class has all of the same methods as


the ArrayList class because they both implement the List interface.
This means that you can add items, change items, remove items and
clear the list in the same way.

The LinkedList stores its items in "containers." The list has a link to the first
container and each container has a link to the next container in the list. To
add an element to the list, the element is placed into a new container and
that container is linked to one of the other containers in the list.

// Import the LinkedList class

import java.util.LinkedList;

public class Main {

public static void main(String[] args) {

LinkedList<String> cars = new LinkedList<String>();

cars.add("Volvo");

cars.add("BMW");
cars.add("Ford");

cars.add("Mazda");

System.out.println(cars);

The Queue interface is present in java.util package and extends


the Collection interface is used to hold the elements about to be
processed in FIFO(First In First Out) order. It is an ordered list of
objects with its use limited to inserting elements at the end of the
list and deleting elements from the start of the list, (i.e.), it follows
the FIFO or the First-In-First-Out principle.

The Queue interface provides several methods for adding, removing, and
inspecting elements in the queue. Here are some of the most commonly
used methods:
add(element): Adds an element to the rear of the queue. If the queue is
full, it throws an exception.
offer(element): Adds an element to the rear of the queue. If the queue is
full, it returns false.
remove(): Removes and returns the element at the front of the queue. If
the queue is empty, it throws an exception.
poll(): Removes and returns the element at the front of the queue. If the
queue is empty, it returns null.
element(): Returns the element at the front of the queue without
removing it. If the queue is empty, it throws an exception.
peek(): Returns the element at the front of the queue without removing it.
If the queue is empty, it returns null.
import java.util.*;

public class GFG {

public static void main(String args[])


{
Queue<String> pq = new PriorityQueue<>();

pq.add("Geeks");
pq.add("For");
pq.add("Geeks");

System.out.println(pq);
}
}

In Java, user-defined classes can be stored in collections such as ArrayList,


HashSet, HashMap, and other collection types provided by the Java Collections
Framework. To illustrate how this works, let's go through examples using some of
these collections.
Example 1: Storing User-Defined Classes in an ArrayList
Let's create a simple user-defined class called Person and then store instances of this
class in an ArrayList.

import java.util.ArrayList;
import java.util.List;

class Person {
private String name;
private int age;

public Person(String name, int age) {


this.name = name;
this.age = age;
}

public String getName() {


return name;
}
public int getAge() {
return age;
}

@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + "}";
}
}

public class Main {


public static void main(String[] args) {
List<Person> people = new ArrayList<>();

// Adding instances of Person to the ArrayList


people.add(new Person("Alice", 30));
people.add(new Person("Bob", 25));
people.add(new Person("Charlie", 35));

// Iterating through the ArrayList


for (Person person : people) {
System.out.println(person);
}
}
}

The java collection framework often we want to cycle through the elements. For
example, we might want to display each element of a collection. The java provides
an interface Iterator that is available inside the java.util package to cycle through
each element of a collection.
🔔 The Iterator allows us to move only forward direction.
🔔 The Iterator does not support the replacement and addition of new elements.
We use the following steps to access a collection of elements using the Iterator.

• Step - 1: Create an object of the Iterator by calling collection.itertor(


) method.
• Step - 2: Use the method hasNext( ) to access to check does the collection
has the next element. (Use a loop).
• Step - 3: Use the method next( ) to access each element from the
collection. (use inside the loop).
import java.util.*;

public class TreeSetExample {

public static void main(String[] args) {

TreeSet set = new TreeSet();

Random num = new Random();


for(int i = 0; i < 10; i++)
set.add(num.nextInt(100));

Iterator collection = set.iterator();

System.out.println("All the elements of TreeSet collection:");


while(collection.hasNext())
System.out.print(collection.next() + ", ");

}
}

put(): java.util.HashMap.put() plays role in associating the specified


value with the specified key in this map. If the map previously
contained a mapping for the key, the old value is replaced. Syntax:
public V put(K key,V value)
Parameters:
key - key with which the specified value is to be associated
value - value to be associated with the specified key
Return: the previous value associated with
key, or null if there was no mapping for key.
2. get(): java.util.HashMap.get()method returns the value to which
the specified key is mapped, or null if this map contains no mapping for
the key. Syntax:
public V get(Object key)
Parameters:
key - the key whose associated value is to be returned
Return: the value to which the specified
key is mapped, or null if this map contains no mapping for
the key.
3. isEmpty(): java.util.HashMap.isEmpty() method returns true if the
map contains no key-value mappings. Syntax:
public boolean isEmpty()
Return: true if this map contains no key-value mappings
4. size(): java.util.HashMap.size() returns the number of key-value
mappings in this map. Syntax:
public int size()
Return: the number of key-value mappings in this map.
5. remove(): java.util.HashMap.remove() removes the key-value pair
associated with the specified key from the HashMap. Syntax:
public V remove(Object key)
Parameters:
key - the key whose mapping is to be removed from the map.
Return: the previous value associated with the key, or null if
there was no mapping for the key.
The TreeMap class in Java is part of the Java Collections Framework and implements
the NavigableMap interface. It is a sorted map that orders its keys based on their
natural ordering or by a specified comparator. Let's explore some of the methods
provided by the TreeMap class through examples.
Example: Demonstrating Methods of TreeMap
We'll create a TreeMap to store Person objects, where the key is an Integer
representing a unique ID.
o

o
o
o
o
o
o
o
o
o
o
o

You might also like