Module V - OOPS 1
Module V - OOPS 1
Module V - OOPS 1
class AL1 {
public static void main(String[] args){
// create ArrayList
ArrayList<String> languages = new ArrayList<>();
import java.util.ArrayList;
class AL1 {
public static void main(String[] args){
ArrayList<String> languages = new ArrayList<>();
languages.add("Java");
languages.add("Python");
languages.add("C++");
import java.util.ArrayList;
class AL1 {
public static void main(String[] args){
ArrayList<String> languages = new ArrayList<>();
languages.add("Java");
languages.add("Python");
languages.add("C++");
System.out.println("Before: " + languages);
languages.set(2,"Ruby");
System.out.println("After: " + languages);
}}
O/P:
Before: [Java, Python, C++]
After: [Java, Python, Ruby]
Dr. Venkata Rami Reddy Ch , Sr. Assistant Professor, SCOPE
Remove ArrayList Elements
• To remove an element from the arraylist, we can use remove() method.
import java.util.ArrayList;
class AL1 {
public static void main(String[] args){
ArrayList<String> languages = new ArrayList<>();
languages.add("Java");
languages.add("Python");
languages.add("C++");
import java.util.ArrayList;
class AL1 {
public static void main(String[] args){
languages.add("Java");
languages.add("Python");
languages.add("C++");
String str="Java"; O/P:
System.out.println("Found: " +languages.contains(str)); Found: true
}
Dr. Venkata Rami Reddy Ch , Sr. Assistant Professor, SCOPE
Iterator
• Iterator in Java is used in the Collection framework to retrieve elements one by one.
• Iterator object can be created by calling iterator() method present in Collection
interface.
• c is collection object.
Syntax:
for (datatype variable : array)
{
statements using var;
}
for(int i:al)
sum=sum+i;
double avg=(double)sum/(al.size());
System.out.println("Average of elements in the arraylist
is:"+avg);
} }Reddy Ch , Sr. Assistant Professor, SCOPE
Dr. Venkata Rami
Sort elements in the ArrayList using sort()
import java.util.*;
public class sort{
public static void main(String[] args)
{
int sum=0;
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(8);
al.add(5);
al.add(3);
al.add(70);
al.add(4);
al.add(25);
System.out.println("Before:"+al);
Collections.sort(al);
System.out.println("After:"+al);
int n=al.size();
System.out.println("Max element in the arraylist is:"+al.get(n-1));
}}
Dr. Venkata Rami Reddy Ch , Sr. Assistant Professor, SCOPE
Java program that creates an ArrayList to store the name, address, and
age of three researchers. The program will display the entire list of
researchers with all the details and display the average age of all the
researchers.
Creating a HashSet
HashSet<datatype> name = new HashSet();
Ex:
HashSet<String> names= new HashSet();
import java.util.*;
public class Average {
public static void main(String[] args)
{
int sum=0;
HashSet<Integer> hs = new HashSet();
hs.add(5);
hs.add(10);
hs.add(3);
System.out.println(hs);
for(int i:hs)
sum=sum+i;
double avg=(double)sum/(hs.size());
System.out.println("Average is:"+avg);
}}
Dr. Venkata Rami Reddy Ch , Sr. Assistant Professor, SCOPE
Java program that creates a HashSet to store the name, author, and
price of three books. The program will display the entire list of
books with all the details and display the average price of all the
books
Methods of Map:
put(K, V) - Inserts the key K and a value V into the map
get(K) - Returns the value associated with the specified key K.
containsKey(K) - Checks if the specified key K is present in the map or not.
containsValue(V) - Checks if the specified value V is present in the map or not.
remove(K) - Removes the entry from the map represented by the key K.
keySet() - Returns a set of all the keys present in a map.
values() - Returns a set of all the values present in a map.
entrySet() - Returns a set of all the key/value mapping present in a map.
map.put(1,"Mango");
map.put(2,"Apple");
map.put(3,"Banana");
map.put(4,"Grapes");
System.out.println(map.get(3));
map.remove(4);
System.out.println(map);
Dr. Venkata Rami Reddy Ch , Sr. Assistant Professor, SCOPE
Iterating through HashMap
import java.util.*;
public class HM1{
public static void main(String args[]){
int sum=0;
HashMap<String,Integer> map=new HashMap();
map.put("Java",80);
map.put("Python",90);
map.put("C",70);
for(Map.Entry m : map.entrySet()){
sum=sum+(int)m.getValue();
}
double avg=(double)sum/(map.size());
System.out.println("Average marks is:"+avg);
}
}
}
}
class Demo{
class Box<T extends Number> { public static void main(String args[])
private T value; {
public void setValue(T value) { Box<Integer> intBox = new Box<>();
this.value = value; intBox.setValue(10);
} System.out.println(intBox.getValue());
public T getValue() { Box<Double> doubleBox = new Box<>();
return value; doubleBox.setValue(3.14);
}} System.out.println(doubleBox.getValue());
}}
Dr. Venkata Rami Reddy Ch , Sr. Assistant Professor, SCOPE