Menu

#24 Collection Framework

1.0
open
2021-11-03
2021-11-03
No

-- String collection
Create a java project called CollectionDemo.
Write a class called StringCollectionsDemo with main method.
Create a private static method that sorts a list of String of country names.
Singapore
India
Nepal
UK
USA
Japan
NOTE: Create an iterator from the ArrayList and use while loop to iterate through the list to print out the list before sorting and after sorting. You can use Collections class sort method to sort in ascending order.

At the end reverse the order and print the list again.

-- Objects Collection
Create an Employee DTO (Data transfer object) or POJO(Plain Old Java Object) with id, firstName, lastName and age attributes.
Provide getters and setters and override toString method and equals method and hashCode method.
Create another class called EmployeeAgeComparator which implements Comparator<employee> and override compareTo method for age to sort based on age.
Create another class called EmployeeNameComparator which compares based on firstName and lastName. Here also implement Comparator<employee> interface.
Create a class EmployeesDemo with main method and create another method in the class called sortEmployees which takes in list of employees as parameter and return list of employees sorted.</employee></employee>

Following are employees.
Employee [id=1000, firstName=Ashim, lastName=Pokharel, age=24]
Employee [id=1001, firstName=Jamuna, lastName=Adhikari, age = 22]
Employee [id=1003, firstName=Rajin, lastName=Gautam, age = 20]
Employee [id=1004, firstName=Resh, Malla=Adhikari, age = 21]
Employee [id=1005, firstName=Subash, lastName=Shrestha, age = 25]

Create an object of this class and invoke the sortEmployees method by passing in the above list of employees.
Sort the employees based on age.
Sort the employees based on firstName and lastName.
Print the employees list before and after sorting.

-- HashMap Operations
Define a class called HashMapDemo with main method.
Define a hashmap collection type of employees and put above Employee objects where id is the key and object is the value.
Now iterate through the map using Map.Entry<k, v=""> and print out the keys and values in console.</k,>

Discussion

  • khagendra baniya

    • summary: Object (Employee) Collections --> Collection Framework
     
  • khagendra baniya

    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,8 +1,22 @@
    -Create a java project called EmployeeCollectionDemo. 
    +-- String collection
    +Create a java project called CollectionDemo. 
    +Write a class called StringCollectionsDemo with main method.
    +Create a private static method that sorts a list of String of country names.
    +Singapore
    +India
    +Nepal
    +UK
    +USA
    +Japan
    +NOTE: Create an iterator from the ArrayList and use while loop to iterate through the list to print out the list before sorting and after sorting. You can use Collections class sort method to sort in ascending order.
    +
    +At the end reverse the order and print the list again.
    +
    +-- Objects Collection
     Create an Employee DTO (Data transfer object) or POJO(Plain Old Java Object) with id, firstName, lastName and age attributes.
     Create another class called EmployeeAgeComparator which implements Comparator&lt;Employee&gt; and override compareTo method for age to sort based on age.
     Create another class called EmployeeNameComparator which compares based on firstName and lastName.    Here also implement Comparator&lt;Employee&gt; interface.
    -Create a class with main method and create another method in the class called sortEmployees which takes in list of employees as parameter and return list of employees sorted.
    +Create a class EmployeesDemo with main method and create another method in the class called sortEmployees which takes in list of employees as parameter and return list of employees sorted.
    
     Following are employees.
     Employee [id=1000, firstName=Ashim, lastName=Pokharel, age=24]
    @@ -11,9 +25,11 @@
     Employee [id=1004, firstName=Resh, Malla=Adhikari, age = 21]
     Employee [id=1005, firstName=Subash, lastName=Shrestha, age = 25]  
    
    
    +    Create an object of this class and invoke the sortEmployees method by passing in the above list of employees. 
       Sort the employees based on age.
       Sort the employees based on firstName and lastName. 
       Print the employees list before and after sorting.
    
    -
    +-- HashMap Operations
    
    
    +    
    
     
  • khagendra baniya

    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -25,7 +25,7 @@
     Employee [id=1004, firstName=Resh, Malla=Adhikari, age = 21]
     Employee [id=1005, firstName=Subash, lastName=Shrestha, age = 25]  
    
    
    -    Create an object of this class and invoke the sortEmployees method by passing in the above list of employees. 
    +Create an object of this class and invoke the sortEmployees method by passing in the above list of employees. 
       Sort the employees based on age.
       Sort the employees based on firstName and lastName. 
       Print the employees list before and after sorting.
    
     
  • khagendra baniya

    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -14,6 +14,7 @@
    
     -- Objects Collection
     Create an Employee DTO (Data transfer object) or POJO(Plain Old Java Object) with id, firstName, lastName and age attributes.
    +Provide getters and setters and override toString method and equals method and hashCode method.
     Create another class called EmployeeAgeComparator which implements Comparator&lt;Employee&gt; and override compareTo method for age to sort based on age.
     Create another class called EmployeeNameComparator which compares based on firstName and lastName.    Here also implement Comparator&lt;Employee&gt; interface.
     Create a class EmployeesDemo with main method and create another method in the class called sortEmployees which takes in list of employees as parameter and return list of employees sorted.
    @@ -31,5 +32,8 @@
       Print the employees list before and after sorting.
    
     -- HashMap Operations
    
    +    Define a class called HashMapDemo with main method.
    +    Define a hashmap collection type of employees and put above Employee objects where id is the key and object is the value.
    +    Now iterate through the map using Map.Entry&lt;K, V&gt; and print out the keys and values in console.
    
     

Log in to post a comment.

MongoDB Logo MongoDB