> For the complete documentation index, see [llms.txt](https://gyansetu-core-java-for-java.gitbook.io/project/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gyansetu-core-java-for-java.gitbook.io/project/practice-tests/untitled-3.md).

# Test - Collections

![](/files/-Lb8SAbxAc055Se05tP5)

![](/files/-Lb8SHsJ3njgGUQUeTwq)

```java
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:

```java
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)

![](/files/-LbBljyBxY3CwNx1UzFA)

#### 2)

![](/files/-LbBlmmYk0MuQYnqxDc0)

#### 3)

![](/files/-LbBmDLhPRuotd5uNiEv)

#### 4)

![](/files/-LbBmRP2lSwUuPJFWhlA)

#### 5)

![](/files/-LbBmY8DBUEXD67MA-cc)

#### 6)

![](/files/-LbBmbaecZWOeH-bWgTg)

#### 7)

![](/files/-LbBmfdrQFjmbfv8g5gw)

#### 8)

![](/files/-LbBmjae5gMJnZTdwQGn)

#### 9)

![](/files/-LbBmmTCD47mLIUtQQgD)

#### 10)

![](/files/-LbBmooT3GWGo9es23Uo)

#### 11)

![](/files/-LbBmy4qvt9ucOTzvGE_)

#### 12)

![](/files/-LbBn22R0B_DgZ5ihiy_)

#### 13)

![](/files/-LbBn4nIlPO5Wf-PqxWq)

#### 14)

![](/files/-LbBn7R7fYiRxBfRXJyd)

#### 15)

![](/files/-LbBnAAPzE1tdHOMHUMn)

#### 16)

![](/files/-LbBnDp0mPGGcDKSa4uL)

#### 17)

![](/files/-LbBnIEfluPz15WAyETn)

#### 18)

![](/files/-LbBnLfMUD9FKe5H8vPD)

#### 19)

![](/files/-LbBnNpmszsJsXo0YS1t)

#### 20)

![](/files/-LbBnQHFpu11D-KeFWL7)

#### 21)

![](/files/-LbBnSqkPkJD-TdL9DSy)

#### 22)

![](/files/-LbBnZ3_qfMgGIAR8d0r)

#### 23)

![](/files/-LbBnanV3ROShq34K2P0)
