Core java - Advance Topics
  • Welcome
  • Schedule
  • 1) Exception Handling
    • 1) Introduction to Exception Handling
    • 2) Categories of Exceptions
    • 3) Creating a method that throws an exception
    • 4) Creating Custom Exception Classes
    • 5)What happens when an exception is thrown?
      • 5.1) Creating try-catch-finally blocks
      • 5.2) Using a method that throws a checked exception
      • 5.3) Using a method that throws a runtime exception
      • 5.4) Using a method that throws an error
      • 5.5) Will a finally block execute even if the catch block defines a return statement?
      • 5.6) What happens if both a catch and a finally block define return statement?
      • 5.7) What happens if a finally block modifies the value returned from a catch block?
      • 5.8) Can a try block be followed only by a finally block?
      • 5.9) Does the order of the exceptions caught in the catch blocks matter?
      • 5.10) Can I rethrow an exception or the error I catch?
      • 5.11) Can I declare my methods to throw a checked exception instead of handling it?
      • 5.12) I can create nested loops, so can I create nested try-catch blocks too?
      • 5.13) Should I handle errors?
    • 6) Best Practices
    • 7) Cheat Sheet
    • 8) Problems
  • 2) Wrapper Classes and Enums
    • 2.1) Creating objects of the wrapper classes
    • Enums
  • 3) Inner Classes
    • 3.1) Static nested class (also called static inner class)
    • 3.2) Inner class (also called member class)
    • 3.3) Anonymous inner class
    • 3.4) Method local inner classes
    • CheatSheet
  • 4) Generics
    • Multiple Type parameters in Generic classes
    • Inheritance using Generics
    • Generic interfaces
    • Generic Methods
    • Bounded type parameters
    • Applications
  • 5) Equals and Hashcode
    • Problems
  • CompareTo method overview
  • Basic DS
    • 1) Simple Array List
    • 2) Simple HashMap
  • 5) Collections Framework - Part 1
    • Introducing the collections framework
    • Working with the Collection interface
      • The core Collection interface
      • Methods of the Collection interface
    • Creating and using List, Set, and Deque implementations
      • List interface and its implementations
      • Iterators
      • Sorting List using custom sorting technique
      • Comparable Interface
      • Custom Sorting using comparator
      • ArrayList - Examples and practice problems
    • Stack
    • Linked List
    • LinkedList Operations
  • 6) Collections Framework - Part 2
    • Sets
      • Set Types
      • Array to Set (vice versa)
    • Maps
    • TreeMap
    • Autoboxing And Unboxing
  • Collections Framework - Part 3
    • Basics : DS , Number System
    • Internal Working
      • HashMap
      • HashSet
  • 7) Reflection API
  • 8) Annotations
  • 9) Reading Input From Various Sources
    • File Handling
    • Reading From Xml
    • Reading From JSON
  • 10) Multi-threading (Concurrency)
    • Protect shared data
    • Thread-safe access to shared data
  • 11) Design Patterns
    • Singleton
    • DI
  • 12) Internal Working of JVM
  • 13) Garbage Collection
  • 14) More on Strings (Buffer and Builder)
  • 15) Cloning and Immutable Class
    • 16) Serialization And Deserialization
    • Untitled
  • JAVA 8
    • Interface Changes
    • Lambda
    • Method Ref
    • Optional
    • Streams
    • Predicates
  • Practice Tests
    • Test - Collections
    • OOPS
    • S-OOPS
Powered by GitBook
On this page
  • Complete the functions:
  • Questions:

Was this helpful?

  1. Practice Tests

Test - Collections

PreviousPredicatesNextOOPS

Last updated 6 years ago

Was this helpful?

public class Employee {
	private int id;
	private String name;
	private double salary;

	public Employee(int id, String name, double salary) {
		this.id = id;
		this.name = name;
		this.salary = salary;

	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}

}

Complete the functions:

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

public class Test1 {
	public static void main(String[] args) {
		Employee emp1 = new Employee(1, "Mohit", 423443);
		Employee emp2 = new Employee(2, "Rahul", 4335656);
		Employee emp3 = new Employee(3, "Shyam", 45443);
		Employee emp4 = new Employee(4, "Mohit", 878788);
		Employee emp5 = new Employee(5, "Swaraj", 767755);
		Employee emp6 = new Employee(3, "Shyam", 45443);
		Employee emp7 = new Employee(7, "Anish", 35546);

		List<Employee> empList = new ArrayList<>(10);

		empList.add(emp1);
		empList.add(emp2);
		empList.add(emp3);
		empList.add(emp4);
		empList.add(emp5);
		empList.add(emp6);
		empList.add(emp7);

		displayEmployeesBySimpleForLoop(empList);

		displayEmployeesByForEach(empList);

		displayEmployeesByIterator(empList);

		List<Employee> sortedEmpList = sortEmpByNameAndSalary(empList);

		List<Employee> sortedEmpList1 = sortEmpByName(empList);

		List<Employee> sortedEmpList2 = sortEmpBySalary(empList);

		int empId = 6;
		Employee empRemoved = removeById(empList, empId);

		HashMap<String, Employee> empMap = convertListToMap(empList);

		displayEmpMap(empMap);

		String nameOfTheEmp = "Mohit";

		removeByNameFromMap(empMap, nameOfTheEmp);

		List<Employee> empListWithoutDuplicate = removeDuplicateEntriesFromList(empList);

	}

	/**
	 * Use simple for loop
	 * 
	 * for(int i =0 ; i<size; i++){
	 * 
	 * }
	 * 
	 * @param empList
	 */
	private static void displayEmployeesBySimpleForLoop(List<Employee> empList) {

	}

	/**
	 * Use For each loop for(Double obj : list){
	 * 
	 * }
	 * 
	 * @param empList
	 */
	private static void displayEmployeesByForEach(List<Employee> empList) {

	}

	/**
	 * Use iterator Iterator iterator = list.iterator();
	 * 
	 * @param empList
	 */
	private static void displayEmployeesByIterator(List<Employee> empList) {

	}

	/**
	 * This method will return a sorted list of employees based name and is name are
	 * then by salary
	 * 
	 * @param empList
	 * @return
	 */
	private static List<Employee> sortEmpByNameAndSalary(List<Employee> empList) {

		return null;
	}

	/**
	 * Use comparator
	 * 
	 * @param empList
	 * @return
	 */
	private static List<Employee> sortEmpBySalary(List<Employee> empList) {

		return null;
	}

	/**
	 * Use comparator
	 * 
	 * @param empList
	 * @return
	 */
	private static List<Employee> sortEmpByName(List<Employee> empList) {
		return null;
	}

	/**
	 * This will remove and return the removed element based on id
	 * 
	 * @param empList
	 * 
	 * @param empId
	 * @return
	 */
	private static Employee removeById(List<Employee> empList, int empId) {
		return null;
	}

	/**
	 * Create a map using the list, assign keys as names of the employee and value
	 * as the employee object
	 * 
	 * @param empList
	 * @return
	 */
	private static HashMap<String, Employee> convertListToMap(List<Employee> empList) {
		return null;
	}

	/**
	 * 
	 * Iterate the whole map and print botth keys and values
	 * 
	 * @param empMap
	 */
	private static void displayEmpMap(HashMap<String, Employee> empMap) {

	}

	/**
	 * Remove all the entries from the map where emp name is "Mohit"
	 * 
	 * @param empMap
	 * @param nameOfTheEmp
	 */
	private static void removeByNameFromMap(HashMap<String, Employee> empMap, String nameOfTheEmp) {

	}

	/**
	 * Remove all the duplicate entries from the list and return the list .
	 * 
	 * @param empList
	 * @return
	 */
	private static List<Employee> removeDuplicateEntriesFromList(List<Employee> empList) {

		return null;
	}

}

Questions:

1)

2)

3)

4)

5)

6)

7)

8)

9)

10)

11)

12)

13)

14)

15)

16)

17)

18)

19)

20)

21)

22)

23)