# Test - Collections

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-Lb8S7o9vYUv1zVE-EWt%2F-Lb8SAbxAc055Se05tP5%2Fimage.png?alt=media\&token=21f53e6f-2bda-42f7-9fd3-24ff6536e6cc)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-Lb8S7o9vYUv1zVE-EWt%2F-Lb8SHsJ3njgGUQUeTwq%2Fimage.png?alt=media\&token=e8d8a36c-3cc0-4c2f-aaa3-af2f9cb2b0de)

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

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LbBlRXORjJLpHT1XuuQ%2F-LbBljyBxY3CwNx1UzFA%2FScreen%20Shot%202019-03-30%20at%209.19.33%20AM.png?alt=media\&token=388cce63-8a96-4d76-bbf3-bceccacf4e7c)

#### 2)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LbBlRXORjJLpHT1XuuQ%2F-LbBlmmYk0MuQYnqxDc0%2FScreen%20Shot%202019-03-30%20at%209.19.42%20AM.png?alt=media\&token=d2edb602-4db4-45cf-b41a-af7954ab5eb2)

#### 3)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LbBlRXORjJLpHT1XuuQ%2F-LbBmDLhPRuotd5uNiEv%2FScreen%20Shot%202019-03-30%20at%209.19.51%20AM.png?alt=media\&token=461d9661-51b7-4d7d-8d4d-b585f78f6e84)

#### 4)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LbBlRXORjJLpHT1XuuQ%2F-LbBmRP2lSwUuPJFWhlA%2FScreen%20Shot%202019-03-30%20at%209.20.15%20AM.png?alt=media\&token=7b58906c-f37e-4310-a5c2-4a16509d9e19)

#### 5)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LbBlRXORjJLpHT1XuuQ%2F-LbBmY8DBUEXD67MA-cc%2FScreen%20Shot%202019-03-30%20at%209.20.31%20AM.png?alt=media\&token=a2098f73-ad75-4654-97ad-fccfe41e148a)

#### 6)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LbBlRXORjJLpHT1XuuQ%2F-LbBmbaecZWOeH-bWgTg%2FScreen%20Shot%202019-03-30%20at%209.20.38%20AM.png?alt=media\&token=6f8634c9-3d6c-434c-a802-861159af5070)

#### 7)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LbBlRXORjJLpHT1XuuQ%2F-LbBmfdrQFjmbfv8g5gw%2FScreen%20Shot%202019-03-30%20at%209.20.45%20AM.png?alt=media\&token=98cf2d42-51ea-4841-99ab-26c137c6fcf5)

#### 8)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LbBlRXORjJLpHT1XuuQ%2F-LbBmjae5gMJnZTdwQGn%2FScreen%20Shot%202019-03-30%20at%209.25.20%20AM.png?alt=media\&token=2ccf633e-c26b-4089-8b31-9c1a97b7c65d)

#### 9)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LbBlRXORjJLpHT1XuuQ%2F-LbBmmTCD47mLIUtQQgD%2FScreen%20Shot%202019-03-30%20at%209.27.33%20AM.png?alt=media\&token=a0609542-c8fd-438e-a8c6-9f998b2c42fc)

#### 10)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LbBlRXORjJLpHT1XuuQ%2F-LbBmooT3GWGo9es23Uo%2FScreen%20Shot%202019-03-30%20at%209.28.15%20AM.png?alt=media\&token=b9f12101-426a-4ec0-83e1-7257a24af8c0)

#### 11)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LbBlRXORjJLpHT1XuuQ%2F-LbBmy4qvt9ucOTzvGE_%2FScreen%20Shot%202019-03-30%20at%209.28.36%20AM.png?alt=media\&token=6fcc67c8-8738-4ea4-a5f3-dd4f89943857)

#### 12)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LbBlRXORjJLpHT1XuuQ%2F-LbBn22R0B_DgZ5ihiy_%2FScreen%20Shot%202019-03-30%20at%209.28.49%20AM.png?alt=media\&token=5f05dea5-6e37-4222-9a83-a58690dceb1b)

#### 13)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LbBlRXORjJLpHT1XuuQ%2F-LbBn4nIlPO5Wf-PqxWq%2FScreen%20Shot%202019-03-30%20at%209.29.59%20AM.png?alt=media\&token=d46c42d1-435c-42fe-92e9-b54ac21141d3)

#### 14)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LbBlRXORjJLpHT1XuuQ%2F-LbBn7R7fYiRxBfRXJyd%2FScreen%20Shot%202019-03-30%20at%209.30.10%20AM.png?alt=media\&token=d426a67e-f769-4978-8a5c-36376afd40e5)

#### 15)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LbBlRXORjJLpHT1XuuQ%2F-LbBnAAPzE1tdHOMHUMn%2FScreen%20Shot%202019-03-30%20at%209.30.23%20AM.png?alt=media\&token=42aee38b-c153-485e-b1d9-65a478b6e495)

#### 16)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LbBlRXORjJLpHT1XuuQ%2F-LbBnDp0mPGGcDKSa4uL%2FScreen%20Shot%202019-03-30%20at%209.32.37%20AM.png?alt=media\&token=161a48de-0dfb-4beb-a998-69f3cec70831)

#### 17)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LbBlRXORjJLpHT1XuuQ%2F-LbBnIEfluPz15WAyETn%2FScreen%20Shot%202019-03-30%20at%209.32.56%20AM.png?alt=media\&token=2058ca25-6884-41a0-b1d8-ebc144e0035f)

#### 18)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LbBlRXORjJLpHT1XuuQ%2F-LbBnLfMUD9FKe5H8vPD%2FScreen%20Shot%202019-03-30%20at%209.33.39%20AM.png?alt=media\&token=5eac9010-6072-484c-8746-c3b97501809a)

#### 19)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LbBlRXORjJLpHT1XuuQ%2F-LbBnNpmszsJsXo0YS1t%2FScreen%20Shot%202019-03-30%20at%209.33.50%20AM.png?alt=media\&token=59f69d76-6425-42c2-8ad8-d774f0b2b6a2)

#### 20)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LbBlRXORjJLpHT1XuuQ%2F-LbBnQHFpu11D-KeFWL7%2FScreen%20Shot%202019-03-30%20at%209.33.59%20AM.png?alt=media\&token=f964a8bd-6715-435c-8b9b-8487ce42be5c)

#### 21)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LbBlRXORjJLpHT1XuuQ%2F-LbBnSqkPkJD-TdL9DSy%2FScreen%20Shot%202019-03-30%20at%209.34.07%20AM.png?alt=media\&token=f8932325-7b2e-4470-ac44-00d334a81408)

#### 22)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LbBlRXORjJLpHT1XuuQ%2F-LbBnZ3_qfMgGIAR8d0r%2FScreen%20Shot%202019-03-30%20at%209.34.16%20AM.png?alt=media\&token=3d95cd6c-d7ac-410e-8b1a-f0aaf0da7195)

#### 23)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LbBlRXORjJLpHT1XuuQ%2F-LbBnanV3ROShq34K2P0%2FScreen%20Shot%202019-03-30%20at%209.34.29%20AM.png?alt=media\&token=721ba815-316b-4a9c-aea3-53d9849c1bfe)
