Experiment No 12-13 - VV

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Experiment No 12

Aim: WAP in java to illustrate stack/queue


Code:
import java.util.*;
public class Main {
public static void main(String args[]){
Stack<Integer> s=new Stack<>();
s.push(1);
s.push(2);
s.push(3);
s.push(4);

boolean option=true;
while (option) {
System.out.println("1.peek into stack");
System.out.println("2.pop into stack");
System.out.println("3.Push into stack");
System.out.println("4.exit");
Scanner sc=new Scanner(System.in);
int choice=sc.nextInt();

switch (choice) {
case 1:{
System.out.println("top element of stack is : "+ s.peek());
break;
}
case 2:{
System.out.println("poped element is : "+ s.pop());
break;
}

case 3:{
System.out.println("element to be pushed in stack : ");
int data=sc.nextInt();
s.push(data);
break;

}
case 4:{
option=false;
break;
}

}
}

}
Output window
Experiment No 13
Aim: WAP to illustrate the difference between lambda function, anonymous
inner class implementation and method reference implementation of
functional interface

Code:
public class Main{
interface Func {
void wish(String name);
}

public static void main(String[] args) {


Func lambdaFunc = (name) -> System.out.println("Hello, " + name + " from
Lambda");
lambdaFunc.wish("Lambda");

Func anonymousClass = new Func() {


public void wish(String name) {
System.out.println("Hello, " + name + " from Anonymous Inner Class");
}
};
anonymousClass.wish("Anonymous Inner Class");

Func methodReference = System.out::println;


methodReference.wish("Hello, Method Reference");
}
}
Output window

You might also like