0% found this document useful (0 votes)
2 views15 pages

Practical 2

The document outlines the implementation of various data structures in Java, including List, Set, Map interfaces, and Lambda expressions. It provides code examples for creating and manipulating ArrayLists, LinkedLists, HashSets, and HashMaps, along with the use of Lambda expressions for conversions. Each section concludes with a statement confirming the successful implementation of the respective interface.

Uploaded by

pkept8g8sa
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)
2 views15 pages

Practical 2

The document outlines the implementation of various data structures in Java, including List, Set, Map interfaces, and Lambda expressions. It provides code examples for creating and manipulating ArrayLists, LinkedLists, HashSets, and HashMaps, along with the use of Lambda expressions for conversions. Each section concludes with a statement confirming the successful implementation of the respective interface.

Uploaded by

pkept8g8sa
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/ 15

ATHARVA KALE PRACTICAL 2 ROLL NO: 27

TITLE: Implementation of multiple data structures.

Aim: To implement List Interface, Set Interface, Map


Interface, Lambda Expression.

A) LIST INTERFACE:
 THEORY:
The List interface provides a way to store the ordered collection. It is a
child interface of Collection. It is an ordered collection of objects in
which duplicate values can be stored. Since List preserves the insertion
order, it allows positional access and insertion of elements.

a. Create an ArrayList of type Interger, add element into


it traverse the arraylist and print the elements
 CODE:

package something;
import java.util.*;

class BoundedShape
{
static Scanner sc = new Scanner(System.in);

static void al()


{
ArrayList<Integer> nums = new ArrayList<Integer>();

System.out.println("ENTER NO OF ELEMENTS: ");

int n=sc.nextInt();

System.out.println("Enter the "+n+" elements");


ATHARVA KALE PRACTICAL 2 ROLL NO: 27

for(int i=0;i<n;i++)
{
int inp = sc.nextInt();
nums.add(inp);
}

Iterator<Integer> it = nums.iterator();
System.out.println("ARRAY IS\n");
while(it.hasNext()) {
System.out.println(it.next());
}

System.out.println(' ');
}
public static void main(String args[])
{
System.out.println(' ');
al();
}
}

 OUTPUT:
ATHARVA KALE PRACTICAL 2 ROLL NO: 27

 CONCLUSION:
Hence list interface Implemented successfully

b. Create a LinkedList of type String add 5 elements and


traverse the list and from both sides
 CODE:

package something;
import java.util.*;

class BoundedShape
{
static Scanner sc = new Scanner(System.in);
static void ll()
{

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

System.out.println("ENTER NO OF ELEMENTS: ");

int n=sc.nextInt();

System.out.println("Enter the "+n+" elements");

for(int i=0;i<n;i++)
{
String inp = sc.next();
li.add(inp);
}
System.out.println("LINK LIST IS\n");
for(String i:li)
{
System.out.println(i);
}
System.out.println(' ');
}

public static void main(String args[])


{
System.out.println(' ');
ll();
ATHARVA KALE PRACTICAL 2 ROLL NO: 27
}
}

 OUTPUT:

c. Create an employee class (id, name, salary) create an


Arralist of type employee, add 5 employee, traverse
the ArrayList and print the elements, Remove one
element and print the list
 CODE:

package something;
import java.util.*;

class employee {

static class emp{


ATHARVA KALE PRACTICAL 2 ROLL NO: 27

int id;
String name;
Double sal;

emp(int id,String name,Double sal)


{
this.id=id;
this.name= name;
this.sal = sal;
}
}

static Scanner sc= new Scanner(System.in);

static ArrayList<emp> data= new ArrayList<emp>();

static void add()


{
System.out.println("Enter Details Of Employee");

System.out.print("ID : ");
int id1=sc.nextInt();

System.out.print("NAME : ");
String name1=sc.next();

System.out.print("SALARY : ");
Double salary1=sc.nextDouble();

data.add(new emp(id1, name1, salary1));

static void pri()


{
for(int j=0;j<data.size();j++)
{
emp e= data.get(j);
System.out.println(e.id);
System.out.println(e.name);
System.out.println(e.sal);
}
}
static void remov()
{
ATHARVA KALE PRACTICAL 2 ROLL NO: 27
System.out.println("Enter the index");
int inde = sc.nextInt();
data.remove(inde);
System.out.println("Arraylist after removing data!");
pri();
}

public static void main(String args[])


{
while(true)
{
System.out.println(" ");
System.out.println("Enter the no: \n 1)ADD \n
2)Print\n 3)Remove");
int ip=sc.nextInt();
if(ip==1)
{
add();
}
else if(ip==2)
{
pri();
}
else if(ip==3)
{
remov();
}
else
{
break;
}}}}

 OUTPUT:
ATHARVA KALE PRACTICAL 2 ROLL NO: 27

o CONCLUION:
Hence list interface Implemented successfully.
ATHARVA KALE PRACTICAL 2 ROLL NO: 27

B) SET INTERFACE:
 THEORY:
A Set is a Collection that cannot contain duplicate elements. It models
the mathematical set abstraction.The Set interface contains only
methods inherited from Collection and adds the restriction that duplicate
elements are prohibited.

a. Write a Java program using Set interface containing


list of items and perform the following operations: a.
Add items in the set. b. Insert items of one set in to
other set. c. Remove items from the set d. Search the
specified item in the set.

 CODE:

package something;
import java.util.*;

public class sets {


public static void main(String args[]) {
Set<String> s= new HashSet<String>();
s.add("IRON MAN");
s.add("DOCTOR STRANGE");
s.add("CAPTAIN AMERICA");
s.add("THOR");
s.add("HULK");
s.add("VISION");
System.out.println("Priting values of SET A: \n");
Iterator<String> it=s.iterator();
while(it.hasNext())
{
System.out.println(it.next());
ATHARVA KALE PRACTICAL 2 ROLL NO: 27
}

System.out.println("\n");

Set<String> s2=new HashSet<String>(s);


System.out.println("Entering values from Set A to Set B and
printing Set B:\n");
Iterator<String> it2=s2.iterator();
while(it2.hasNext())
{
System.out.println(it2.next());
}

System.out.println("\n");

System.out.println("Removing items VISION AND IRON MAN from


Set B\n ");
s2.remove("VISION");
s2.remove("IRON MAN");

System.out.println("Items removed from Set B\n");

System.out.println("Serching for removed item IRON MAN\n");


System.out.println("Does Set B contains IRON MAN?
"+s2.contains("IRON MAN"));
}
}

 OUTPUT:
ATHARVA KALE PRACTICAL 2 ROLL NO: 27

 CONCLUSION:
Hence set interface Implemented successfully

C) MAP INTERFACE:
 THEORY:
The Map interface maps unique keys to values. A key is an object that you
use to retrieve a value at a later date.Given a key and a value, you can
store the value in a Map object. After the value is stored, you can retrieve
it by using its key.
ATHARVA KALE PRACTICAL 2 ROLL NO: 27

a. Create a class Customer(Account_no Integer, Name


Sting), Create a HashMap of type Customer put
elements, print elements, check if element with
account number 101 is present or not? What is the
value for Customer 101.
 CODE:

package something;
import java.util.*;

public class Customer {


int Account_No;
String Name;
public Customer(int Account_No,String Name)
{
this.Account_No=Account_No;
this.Name=Name;
}

public String getname()


{
return this.Name;
}

public int getacc()


{
return this.Account_No;
}

public static void main(String args[])


{
HashMap<Customer,Integer> cus = new HashMap<>();
Customer c1 = new Customer(101,"IRON MAN");
Customer c2=new Customer(102,"DOCTOR STRANGE");
Customer c3 = new Customer(103,"THOR");
cus.put(c1, c1.getacc());
cus.put(c2, c2.getacc());
ATHARVA KALE PRACTICAL 2 ROLL NO: 27
cus.put(c3, c3.getacc());

System.out.println("PRINTING ELEMENTS FROM HASHMAP:\n");

for (Customer i : cus.keySet())


{
System.out.println("ACCOUNT NO:" + cus.get(i) + "
NAME:" + i.getname());
}
System.out.println("\nWhether there is a customer with
ACCOUNT NO: 101? "+cus.containsValue(101));
for (Customer i : cus.keySet())
{
if(cus.get(i)==101)
{
System.out.println("ACCOUNT NO:" + cus.get(i) + "
NAME:" + i.getname());
}
break;
}}}
 OUTPUT:

 CONCLUSION:
Hence map interface Implemented successfully
ATHARVA KALE PRACTICAL 2 ROLL NO: 27

D) LAMBDA EXPRESSION:
 THEORY:
Lambda Expressions were added in Java 8.A lambda expression is a
short block of code which takes in parameters and returns a value.
Lambda expressions are similar to methods, but they do not need a
name and they can be implemented right in the body of a method.

a. Write a Java program using Lambda Expression to


calculate the following: a. Convert Fahrenheit to
Celsius b. Convert Kilometers to Miles.
1)Fahrenheit to celcius
 CODE:

package something;
interface ftoc
{
public int convert(int c);
}

public class lamb {


public static void main(String[] args)
{
ftoc s1=(c)->{
return ((c-32)*5/9);
};

System.out.println("Value in celcius is:


"+s1.convert(50));
}
}
ATHARVA KALE PRACTICAL 2 ROLL NO: 27

 OUTPUT:

2) Kilometers to miles:

 CODE:

package something;

interface ktom
{
public double travel(double c);
}

public class lamb {


public static void main(String[] args)
{
ktom a=(double c)->{
return (c*0.6213);
};

System.out.println("Value in km to miles is:


"+a.travel(5));
}
}

 OUTPUT:
ATHARVA KALE PRACTICAL 2 ROLL NO: 27

 CONCLUSION:
Hence lambda expressions Implemented successfully.

You might also like