Skip to content
| Marketplace
Sign in
Visual Studio Code>Other>JavaDebug 2.0New to Visual Studio Code? Get it now.
JavaDebug 2.0

JavaDebug 2.0

Java Dev Tools

|
20 installs
| (0) | Free
Java debugging helper extension
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

List Ops PROGRAM 1 Create a List of Strings and Print Using For-Each Loop import java.util.*;

public class ListForEach { public static void main(String[] args) {

    // Creating a List of Strings
    List<String> items = new ArrayList<>();
    items.add("Apple");
    items.add("Banana");
    items.add("Mango");
    items.add("Orange");

    // Printing using for-each loop
    System.out.println("Items in the List:");
    for (String item : items) {
        System.out.println(item);
    }
}

}


PROGRAM 2 List Using Iterator + Reverse Using ListIterator import java.util.*;

public class ListIteratorExample { public static void main(String[] args) {

    List<String> names = new ArrayList<>();
    names.add("Amit");
    names.add("Neha");
    names.add("Ravi");
    names.add("Pooja");

    // Display using Iterator (Forward)
    System.out.println("Using Iterator (Forward):");
    Iterator<String> itr = names.iterator();
    while (itr.hasNext()) {
        System.out.println(itr.next());
    }

    // Display using ListIterator (Backward)
    System.out.println("\nUsing ListIterator (Backward):");
    ListIterator<String> listItr = names.listIterator(names.size());
    while (listItr.hasPrevious()) {
        System.out.println(listItr.previous());
    }
}

}


PROGRAM 3 List of Grocery/Beauty Items Using ListIterator Forward & Backward import java.util.*;

public class GroceryListIterator { public static void main(String[] args) {

    List<String> grocery = new ArrayList<>();
    grocery.add("Shampoo");
    grocery.add("Soap");
    grocery.add("Facewash");
    grocery.add("Toothpaste");

    ListIterator<String> listItr = grocery.listIterator();

    // Forward Direction
    System.out.println("Forward Direction:");
    while (listItr.hasNext()) {
        System.out.println(listItr.next());
    }

    // Backward Direction
    System.out.println("\nBackward Direction:");
    while (listItr.hasPrevious()) {
        System.out.println(listItr.previous());
    }
}

}

SET Ops PROGRAM 1 Set Operations — Add, Remove, Search, Insert One Set into Another Question: Write a Java program using Set interface containing list of items and perform operations: Add items, Remove items, Search item, Insert one set into another set. Code: package practical; import java.util.*;

public class SetOperations { public static void main(String[] args) {

    Set<String> set1 = new HashSet<>();

    // Add items
    set1.add("Apple");
    set1.add("Banana");
    set1.add("Mango");
    set1.add("Orange");

    System.out.println("Set 1: " + set1);

    // Remove item
    set1.remove("Banana");
    System.out.println("After Removing Banana: " + set1);

    // Search item
    if(set1.contains("Mango")) {
        System.out.println("Mango found in Set");
    } else {
        System.out.println("Mango not found");
    }

    // Second Set
    Set<String> set2 = new HashSet<>();
    set2.add("Grapes");
    set2.add("Pineapple");

    // Insert set2 into set1
    set1.addAll(set2);

    System.out.println("Final Set after merging: " + set1);
}

} ________________________________________ PROGRAM 2 Set of Strings using Iterator + Reverse Direction Question: Write a Java program to create a Set containing list of items of type String and print the items using Iterator. Also print the list in reverse/backward direction. Code: package practical; import java.util.*;

public class SetIteratorReverse { public static void main(String[] args) {

    Set<String> items = new HashSet<>();
    items.add("Pen");
    items.add("Book");
    items.add("Pencil");
    items.add("Eraser");

    // Iterator Forward
    System.out.println("Forward Direction:");
    Iterator<String> itr = items.iterator();
    while(itr.hasNext()) {
        System.out.println(itr.next());
    }

    // Convert Set to List for reverse printing
    List<String> list = new ArrayList<>(items);

    System.out.println("\nBackward Direction:");
    ListIterator<String> listItr = list.listIterator(list.size());
    while(listItr.hasPrevious()) {
        System.out.println(listItr.previous());
    }
}

}


PROGRAM 3 TreeSet — Forward & Reverse Traversal Question: Write a Java program to demonstrate TreeSet interface and iterate data in forward and reverse direction (grocery items / covid patient records). Code (Grocery Items Example): package practical; import java.util.*;

public class TreeSetExample { public static void main(String[] args) {

    TreeSet<String> grocery = new TreeSet<>();

    grocery.add("Rice");
    grocery.add("Sugar");
    grocery.add("Milk");
    grocery.add("Flour");
    grocery.add("Oil");

    // Forward Direction
    System.out.println("Forward Order:");
    Iterator<String> itr = grocery.iterator();
    while(itr.hasNext()) {
        System.out.println(itr.next());
    }

    // Reverse Direction
    System.out.println("\nReverse Order:");
    Iterator<String> descItr = grocery.descendingIterator();
    while(descItr.hasNext()) {
        System.out.println(descItr.next());
    }
}

}

MAP Ops PROGRAM — Map Interface Operations Question: Write a Java program using Map interface containing list of items having keys and associated values and perform the following operations: • Add items in the map • Remove items from map • Search specific key • Get value of specified key • Insert map elements into another map • Print all keys and values of the map


Java Code import java.util.*;

public class MapOperations { public static void main(String[] args) {

    // Creating Map
    Map<Integer, String> map1 = new HashMap<>();

    // Add items in the map
    map1.put(101, "Apple");
    map1.put(102, "Banana");
    map1.put(103, "Mango");
    map1.put(104, "Orange");

    System.out.println("Original Map:");
    System.out.println(map1);

    // Remove item from map
    map1.remove(102);
    System.out.println("\nAfter Removing Key 102:");
    System.out.println(map1);

    // Search specific key
    int searchKey = 103;
    if (map1.containsKey(searchKey)) {
        System.out.println("\nKey " + searchKey + " Found in Map");
    } else {
        System.out.println("\nKey " + searchKey + " Not Found");
    }

    // Get value of specified key
    System.out.println("\nValue of Key 101: " + map1.get(101));

    // Create another map
    Map<Integer, String> map2 = new HashMap<>();
    map2.put(105, "Grapes");
    map2.put(106, "Pineapple");

    System.out.println("\nSecond Map:");
    System.out.println(map2);

    // Insert map2 elements into map1
    map1.putAll(map2);

    System.out.println("\nFinal Map after Merging:");
    System.out.println(map1);

    // Print all keys and values
    System.out.println("\nAll Keys and Values:");
    for (Map.Entry<Integer, String> entry : map1.entrySet()) {
        System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
    }
}

}


GENERIC OPS PROGRAM 1 Generic Class for Integer and Double Calculator Question: Write a Java program to demonstrate a Generic Class for Integer and Double calculator. Code: class Calculator { T num1, num2;

Calculator(T num1, T num2) {
    this.num1 = num1;
    this.num2 = num2;
}

double add() {
    return num1.doubleValue() + num2.doubleValue();
}

double subtract() {
    return num1.doubleValue() - num2.doubleValue();
}

double multiply() {
    return num1.doubleValue() * num2.doubleValue();
}

double divide() {
    return num1.doubleValue() / num2.doubleValue();
}

}

public class GenericCalculator { public static void main(String[] args) {

    Calculator<Integer> intCalc = new Calculator<>(10, 5);
    System.out.println("Integer Calculator:");
    System.out.println("Addition: " + intCalc.add());
    System.out.println("Subtraction: " + intCalc.subtract());
    System.out.println("Multiplication: " + intCalc.multiply());
    System.out.println("Division: " + intCalc.divide());

    Calculator<Double> doubleCalc = new Calculator<>(10.5, 2.5);
    System.out.println("\nDouble Calculator:");
    System.out.println("Addition: " + doubleCalc.add());
    System.out.println("Subtraction: " + doubleCalc.subtract());
    System.out.println("Multiplication: " + doubleCalc.multiply());
    System.out.println("Division: " + doubleCalc.divide());
}

}


PROGRAM 2 Generic Method Demonstration Question: Write a Java program to demonstrate Generic Methods. Code: public class GenericMethodExample {

public static <T> void printArray(T[] array) {
    for (T element : array) {
        System.out.println(element);
    }
}

public static void main(String[] args) {

    Integer[] numbers = {10, 20, 30, 40};
    String[] names = {"Amit", "Neha", "Ravi"};

    System.out.println("Integer Array:");
    printArray(numbers);

    System.out.println("\nString Array:");
    printArray(names);
}

}


PROGRAM 3 Upper Bound Wildcards in Java Generics Question: Write a Java program to demonstrate Upper Bound Wildcards in Java Generics. Code: import java.util.*;

public class UpperBoundWildcard {

public static double calculateSum(List<? extends Number> list) {
    double sum = 0;
    for (Number num : list) {
        sum += num.doubleValue();
    }
    return sum;
}

public static void main(String[] args) {

    List<Integer> intList = Arrays.asList(10, 20, 30);
    List<Double> doubleList = Arrays.asList(5.5, 6.5, 7.5);

    System.out.println("Sum of Integer List: " + calculateSum(intList));
    System.out.println("Sum of Double List: " + calculateSum(doubleList));
}

}


PROGRAM 4 Lower Bound Wildcards in Java Generics Question: Write a Java program to demonstrate Lower Bound Wildcards in Java Generics. Code: import java.util.*;

public class LowerBoundWildcard {

public static void addNumbers(List<? super Integer> list) {
    list.add(10);
    list.add(20);
    list.add(30);
}

public static void main(String[] args) {

    List<Number> numberList = new ArrayList<>();

    addNumbers(numberList);

    System.out.println("List after adding Integer values:");
    for (Object obj : numberList) {
        System.out.println(obj);
    }
}

}


LAMBDA OPS PROGRAM 1 Lambda Expression to Concatenate Two Strings Question: Write a Java program using Lambda Expression to concatenate two strings. Code: interface StringConcat { String concat(String s1, String s2); }

public class LambdaConcat { public static void main(String[] args) {

    StringConcat sc = (s1, s2) -> s1 + s2;

    String result = sc.concat("Hello ", "World");
    System.out.println("Concatenated String: " + result);
}

}


PROGRAM 2 Lambda Expression with Single Parameter Question: Write a Java program using Lambda Expression with a single parameter. Code: interface SingleParam { void display(String name); }

public class LambdaSingleParameter { public static void main(String[] args) {

    SingleParam sp = (name) -> System.out.println("Welcome " + name);

    sp.display("Aditya");
}

}


PROGRAM 3 Lambda Expression with Multiple Parameters (Add Two Numbers) Question: Write a Java program using Lambda Expression with multiple parameters to add two numbers. Code: interface AddNumbers { int add(int a, int b); }

public class LambdaMultipleParameters { public static void main(String[] args) {

    AddNumbers addition = (a, b) -> a + b;

    int result = addition.add(10, 20);
    System.out.println("Sum = " + result);
}

}


PROGRAM 4 Lambda Expression With and Without Return Keyword Question: Write a Java program using Lambda Expression with or without return keyword. Code: package JavaCo;

interface MyCalculator { int calculate(int a, int b); }

public class LambdaReturnExample { public static void main(String[] args) {

    MyCalculator add = (a, b) -> a + b;

    
    MyCalculator multiply = (a, b) -> {
        return a * b;
    };

    System.out.println("Addition Result: " + add.calculate(5, 3));
    System.out.println("Multiplication Result: " + multiply.calculate(5, 3));
}

}


Fahrenheit → Celsius


import java.util.function.Function;

public class FtoC { public static void main(String[] args) { Function<Double, Double> convert = f -> (f - 32) * 5 / 9; System.out.println("Celsius: " + convert.apply(98.6)); } }


Celsius → Fahrenheit


import java.util.function.Function;

public class CtoF { public static void main(String[] args) { Function<Double, Double> convert = c -> (c * 9 / 5) + 32; System.out.println("Fahrenheit: " + convert.apply(37.0)); } }


Kilometers → Miles


import java.util.function.Function;

public class KmToMiles { public static void main(String[] args) { Function<Double, Double> convert = km -> km * 0.621371; System.out.println("Miles: " + convert.apply(10.0)); } }


Lambda Calculator (Add, Sub, Mul, Div)


interface Calculator { double operate(double a, double b); }

public class LambdaCalc { public static void main(String[] args) {

    Calculator add = (a, b) -> a + b;
    Calculator sub = (a, b) -> a - b;
    Calculator mul = (a, b) -> a * b;
    Calculator div = (a, b) -> a / b;

    System.out.println("Add: " + add.operate(10, 5));
    System.out.println("Sub: " + sub.operate(10, 5));
    System.out.println("Mul: " + mul.operate(10, 5));
    System.out.println("Div: " + div.operate(10, 5));
}

}


Largest of Three Numbers


interface MaxThree { int findMax(int a, int b, int c); }

public class LargestLambda { public static void main(String[] args) { MaxThree max = (a, b, c) -> Math.max(a, Math.max(b, c)); System.out.println("Largest: " + max.findMax(10, 25, 15)); } }


Sum 1 to 100 + Reverse Display


import java.util.stream.IntStream;

public class SumReverse { public static void main(String[] args) {

    int sum = IntStream.rangeClosed(1, 100).sum();
    System.out.println("Sum = " + sum);

    System.out.println("Reverse Order:");
    IntStream.iterate(100, n -> n - 1)
             .limit(100)
             .forEach(n -> System.out.print(n + " "));
}

}


Area of Triangle


interface Triangle { double area(double base, double height); }

public class TriangleArea { public static void main(String[] args) { Triangle t = (b, h) -> 0.5 * b * h; System.out.println("Area: " + t.area(10, 5)); } }


Circumference of Circle


interface Circle { double circumference(double r); }

public class CircleCircumference { public static void main(String[] args) { Circle c = r -> 2 * Math.PI * r; System.out.println("Circumference: " + c.circumference(7)); } }


Perimeter of Rectangle


interface Rectangle { double perimeter(double l, double w); }

public class RectPerimeter { public static void main(String[] args) { Rectangle r = (l, w) -> 2 * (l + w); System.out.println("Perimeter: " + r.perimeter(10, 5)); } }


Volume of Cylinder


interface Cylinder { double volume(double r, double h); }

public class CylinderVolume { public static void main(String[] args) { Cylinder c = (r, h) -> Math.PI * r * r * h; System.out.println("Volume: " + c.volume(7, 10)); } }


Volume of Sphere


interface Sphere { double volume(double r); }

public class SphereVolume { public static void main(String[] args) { Sphere s = r -> (4.0 / 3) * Math.PI * r * r * r; System.out.println("Volume: " + s.volume(7)); } }


Write a program to demonstrate Spring AOP before advice. HelloService.java – public class HelloService { public void sayHello() { System.out.println("Hello from target method"); } }

LoggingAspect.java – import org.aspectj.lang.JoinPoint; public class LoggingAspect { public void beforeAdvice(JoinPoint jp) { System.out.println("Before advice executed before method: " + jp.getSignature().getName()); } }

MainApp.java – import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloService hs = (HelloService) ctx.getBean("helloService"); hs.sayHello(); } }

applicationContext.xml –

aop:config <aop:aspect ref="loggingAspect"> <aop:pointcut id="pc" expression="execution(* HelloService.sayHello(..))"/> <aop:before method="beforeAdvice" pointcut-ref="pc"/> </aop:aspect> </aop:config>


Write a program to demonstrate Spring AOP after advice. HelloService.java – public class HelloService { public void sayHello() { System.out.println("Hello from target method"); } }

LoggingAspect.java – import org.aspectj.lang.JoinPoint; public class LoggingAspect { public void afterAdvice(JoinPoint jp) { System.out.println("After advice executed after method: " + jp.getSignature().getName()); } }

MainApp.java – import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloService hs = (HelloService) ctx.getBean("helloService"); hs.sayHello(); } }

applicationContext.xml –

aop:config <aop:aspect ref="loggingAspect"> <aop:pointcut id="pc" expression="execution(* HelloService.sayHello(..))"/> <aop:after method="afterAdvice" pointcut-ref="pc"/> </aop:aspect> </aop:config> ________________________________________Write a program to demonstrate Spring AOP around advice. HelloService.java – public class HelloService { public void sayHello() { System.out.println("Hello from target method"); } }

LoggingAspect.java – import org.aspectj.lang.ProceedingJoinPoint; public class LoggingAspect { public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable { System.out.println("Before method (Around)"); Object obj = pjp.proceed(); System.out.println("After method (Around)"); return obj; } }

MainApp.java – import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloService hs = (HelloService) ctx.getBean("helloService"); hs.sayHello(); } }

applicationContext.xml –

aop:config <aop:aspect ref="loggingAspect"> <aop:pointcut id="pc" expression="execution(* HelloService.sayHello(..))"/> <aop:around method="aroundAdvice" pointcut-ref="pc"/> </aop:aspect> </aop:config>


Write a program to demonstrate Spring AOP after returning advice. Code: HelloService.java – public class HelloService { public void sayHello() { System.out.println("Hello from target method"); } }

LoggingAspect.java – import org.aspectj.lang.JoinPoint; public class LoggingAspect { public void afterReturningAdvice(Object result) { System.out.println("After Returning advice executed"); } }

MainApp.java – import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloService hs = (HelloService) ctx.getBean("helloService"); hs.sayHello(); } }

applicationContext.xml –

aop:config <aop:aspect ref="loggingAspect"> <aop:pointcut id="pc" expression="execution(* HelloService.sayHello(..))"/> <aop:after-returning method="afterReturningAdvice" pointcut-ref="pc" returning="result"/> </aop:aspect> </aop:config>


Write a program to demonstrate Spring AOP after throwing advice. HelloService.java – public class HelloService { public void sayHello() { int a = 10 / 0; } }

LoggingAspect.java – import org.aspectj.lang.JoinPoint; public class LoggingAspect { public void afterThrowingAdvice(Exception ex) { System.out.println("After Throwing advice executed: " + ex.getMessage()); } }

MainApp.java – import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloService hs = (HelloService) ctx.getBean("helloService"); hs.sayHello(); } }

applicationContext.xml –

aop:config <aop:aspect ref="loggingAspect"> <aop:pointcut id="pc" expression="execution(* HelloService.sayHello(..))"/> <aop:after-throwing method="afterThrowingAdvice" pointcut-ref="pc" throwing="ex"/> </aop:aspect> </aop:config>


Spring Framework to demonstrate the working of dependency injection via setter method.. Address.java – package com.example; public class Address { private String city; private String state; private String zipcode; public Address() {} public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getState() { return state; } public void setState(String state) { this.state = state; } public String getZipcode() { return zipcode; } public void setZipcode(String zipcode) { this.zipcode = zipcode; } @Override public String toString() { return city + ", " + state + " - " + zipcode; } } App.java – package com.example; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml"); Student s = (Student) ctx.getBean("studentBean"); s.display(); ((ClassPathXmlApplicationContext) ctx).close(); } } Course.java – package com.example; public class Course { private String courseName; private String duration; public Course() {} public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } public String getDuration() { return duration; } public void setDuration(String duration) { this.duration = duration; } @Override public String toString() { return courseName + " (" + duration + ")"; } } Student.java – package com.example; public class Student { private int rollNo; private String name; private Course course;
private Address address;
public Student() {} public int getRollNo() { return rollNo; } public void setRollNo(int rollNo) { this.rollNo = rollNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Course getCourse() { return course; } public void setCourse(Course course) { this.course = course; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public void display() { System.out.println("Student Roll No : " + rollNo); System.out.println("Student Name : " + name); System.out.println("Student Course : " + (course != null ? course : "N/A")); System.out.println("Student Address : " + (address != null ? address : "N/A")); } } spring.xml –


Spring Framework to demonstrate dependency injection via constructor Author.java – package com.example; public class Author { private String name; private String country; public Author(String name, String country) { this.name = name; this.country = country; } public String getName() { return name; } public String getCountry() { return country; } @Override public String toString() { return name + " from " + country; } }

Book.java – package com.example; public class Book { private String title; private double price; private Author author;
public Book(String title, double price, Author author) { this.title = title; this.price = price; this.author = author; } public void display() { System.out.println("Book Title : " + title); System.out.println("Book Price : " + price); System.out.println("Author Info : " + author); } }

App.java – package com.example; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml"); Book book = (Book) ctx.getBean("bookBean"); book.display(); ((ClassPathXmlApplicationContext) ctx).close(); } }

spring.xml –


  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
© 2026 Microsoft