Friday 27 February 2015

Java : Collection Framework : Hashtable (default Constructor CountryCode)


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=RfHxc5ovkGE&list=UUhwKlOVR041tngjerWxVccw

HashtableExample.java
import java.util.Hashtable;

/*
 * Example of default Hashtable() Constructor.
 */
public class HashtableExample
{
    public static void main( String[] args )
    {

        /*
         * Constructs a new, empty hashtable with a default initial capacity
         * (11) and load factor (0.75).
         */
        Hashtable<String, String> hashtable = new Hashtable<String, String>();

        System.out.println("hashtable : " + hashtable + "\n");

        /*
         * Key = CountryCode,Value = CountryName
         */
        hashtable.put("AF", "AFGHANISTAN");
        hashtable.put("BE", "BELGIUM");
        hashtable.put("US", "UNITED STATES");
        hashtable.put("IN", "INDIA");

        System.out.println("hashtable : " + hashtable);
    }
}
Output
hashtable : {}

hashtable : {IN=INDIA, AF=AFGHANISTAN, BE=BELGIUM, US=UNITED STATES}
To Download HashtableDemoDefaultConsCountryCode Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/HashtableDemoDefaultConsCountryCode.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java : Collection Framework : Hashtable (default Constructor)


    Click here to watch in Youtube : 
    https://www.youtube.com/watch?v=Eg7sbK5WgzY&list=UUhwKlOVR041tngjerWxVccw

    HashtableExample.java
    import java.util.Hashtable;
    
    /*
     * Example of default Hashtable() Constructor.
     */
    public class HashtableExample
    {
        public static void main(String[] args)
        {
    
            /*
             * Constructs a new, empty hashtable with a default initial capacity
             * (11) and load factor (0.75).
             */
            Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>();
    
            System.out.println("hashtable : " + hashtable + "\n");
    
            hashtable.put(1, "Apple");
            hashtable.put(2, "Ball");
            hashtable.put(3, "Cat");
    
            System.out.println("hashtable : " + hashtable);
        }
    }
    
    Output
    hashtable : {}
    
    hashtable : {3=Cat, 2=Ball, 1=Apple}
    
    To Download HashtableDemoDefaultCons Project Click the below link
    https://sites.google.com/site/javaee4321/java-collections/HashtableDemoDefaultCons.zip?attredirects=0&d=1

    See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java : Collection Framework : TreeMap (Comparator - Desc Order CountryCode)


    Click here to watch in Youtube :
    https://www.youtube.com/watch?v=0N9mOZtGNa0&list=UUhwKlOVR041tngjerWxVccw

    DescendingCountryCodeComparator.java
    import java.util.Comparator;
    
    public class DescendingCountryCodeComparator implements Comparator<String>
    {
    
        /*
         * This method is used to arrange the CountryCodes in descending order.
         */
        @Override
        public int compare( String countryCode1, String countryCode2 )
        {
    
            System.out
                    .println("\nCompare method has been called in DescendingCountryCodeComparator,
                               \nto arrange"+ " the CountryCodes in decending order : ");
    
            System.out.println("countryCode1 = " + countryCode1
                    + ", countryCode2 = " + countryCode2 + "\n");
    
            return countryCode2.compareTo(countryCode1);
        }
    
    }
    
    TreeMapExample.java
    import java.util.TreeMap;
    
    /*
     * Example of comparator() method.
     */
    public class TreeMapExample
    {
        public static void main( String[] args )
        {
    
            DescendingCountryCodeComparator descendingCountryCodeComparator
                                                   = new DescendingCountryCodeComparator();
    
            TreeMap<String, String> treeMap = new TreeMap<String, String>(
                    descendingCountryCodeComparator);
            /*
             * Key is CountryCode - Value is CountryName
             */
    
            System.out.println("Key:CN,Value:CHINA is going to be add in treeMap");
            treeMap.put("CN", "CHINA");
            
            System.out.println("Key:BE,Value:BELGIUM is going to be add in treeMap");
            treeMap.put("BE", "BELGIUM");
            
            System.out.println("Key:AF,Value:AFGHANISTAN is going to be add in treeMap");
            treeMap.put("AF", "AFGHANISTAN");
            
            System.out.println("Key:DK,Value:DENMARK is going to be add in treeMap");
            treeMap.put("DK", "DENMARK");
    
            System.out.println("treeMap : " + treeMap + "\n");
    
        }
    }
    
    Output
    Key:CN,Value:CHINA is going to be add in treeMap
    
    Compare method has been called in DescendingCountryCodeComparator,
    to arrange the CountryCodes in decending order : 
    countryCode1 = CN, countryCode2 = CN
    
    Key:BE,Value:BELGIUM is going to be add in treeMap
    
    Compare method has been called in DescendingCountryCodeComparator,
    to arrange the CountryCodes in decending order : 
    countryCode1 = BE, countryCode2 = CN
    
    Key:AF,Value:AFGHANISTAN is going to be add in treeMap
    
    Compare method has been called in DescendingCountryCodeComparator,
    to arrange the CountryCodes in decending order : 
    countryCode1 = AF, countryCode2 = CN
    
    
    Compare method has been called in DescendingCountryCodeComparator,
    to arrange the CountryCodes in decending order : 
    countryCode1 = AF, countryCode2 = BE
    
    Key:DK,Value:DENMARK is going to be add in treeMap
    
    Compare method has been called in DescendingCountryCodeComparator,
    to arrange the CountryCodes in decending order : 
    countryCode1 = DK, countryCode2 = BE
    
    
    Compare method has been called in DescendingCountryCodeComparator,
    to arrange the CountryCodes in decending order : 
    countryCode1 = DK, countryCode2 = CN
    
    treeMap : {DK=DENMARK, CN=CHINA, BE=BELGIUM, AF=AFGHANISTAN}
    
    To Download TreeMapDemoDescOrderCountryCode Project Click the below link
    https://sites.google.com/site/javaee4321/java-collections/TreeMapDemoDescOrderCountryCode.zip?attredirects=0&d=1

    See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java : Collection Framework : TreeMap (Comparator - Asc Order CountryCode)


    Click here to watch in Youtube :
    https://www.youtube.com/watch?v=B4pwRRIaAaw&list=UUhwKlOVR041tngjerWxVccw

    AscendingCountryCodeComparator.java
    import java.util.Comparator;
    
    public class AscendingCountryCodeComparator implements Comparator<String>
    {
    
        /*
         * This method is used to arrange the CountryCodes in Ascending order.
         */
        @Override
        public int compare( String countryCode1, String countryCode2 )
        {
    
            System.out
                    .println("\nCompare method has been called in AscendingCountryCodeComparator,
                    \nto arrange"+ " the CountryCodes in ascending order : ");
    
            System.out.println("countryCode1 = " + countryCode1
                    + ", countryCode2 = " + countryCode2 + "\n");
    
            return countryCode1.compareTo(countryCode2);
        }
    
    }
    
    TreeMapExample.java
    import java.util.TreeMap;
    
    /*
     * Example of comparator() method.
     */
    public class TreeMapExample
    {
        public static void main( String[] args )
        {
    
            AscendingCountryCodeComparator ascendingCountryCodeComparator 
                                              = new AscendingCountryCodeComparator();
    
            TreeMap<String, String> treeMap = new TreeMap<String, String>(
                    ascendingCountryCodeComparator);
            /*
             * Key is CountryCode - Value is CountryName
             */
    
            System.out.println("Key:CN,Value:CHINA is going to be add in treeMap");
            treeMap.put("CN", "CHINA");
        
            System.out.println("Key:BE,Value:BELGIUM is going to be add in treeMap");
            treeMap.put("BE", "BELGIUM");
            
            System.out.println("Key:AF,Value:AFGHANISTAN is going to be add in treeMap");
            treeMap.put("AF", "AFGHANISTAN");
            
            System.out.println("Key:DK,Value:DENMARK is going to be add in treeMap");
            treeMap.put("DK", "DENMARK");
    
            System.out.println("treeMap : " + treeMap + "\n");
    
        }
    }
    
    Output
    Key:CN,Value:CHINA is going to be add in treeMap
    
    Compare method has been called in AscendingCountryCodeComparator,
    to arrange the CountryCodes in ascending order : 
    countryCode1 = CN, countryCode2 = CN
    
    Key:BE,Value:BELGIUM is going to be add in treeMap
    
    Compare method has been called in AscendingCountryCodeComparator,
    to arrange the CountryCodes in ascending order : 
    countryCode1 = BE, countryCode2 = CN
    
    Key:AF,Value:AFGHANISTAN is going to be add in treeMap
    
    Compare method has been called in AscendingCountryCodeComparator,
    to arrange the CountryCodes in ascending order : 
    countryCode1 = AF, countryCode2 = CN
    
    
    Compare method has been called in AscendingCountryCodeComparator,
    to arrange the CountryCodes in ascending order : 
    countryCode1 = AF, countryCode2 = BE
    
    Key:DK,Value:DENMARK is going to be add in treeMap
    
    Compare method has been called in AscendingCountryCodeComparator,
    to arrange the CountryCodes in ascending order : 
    countryCode1 = DK, countryCode2 = BE
    
    
    Compare method has been called in AscendingCountryCodeComparator,
    to arrange the CountryCodes in ascending order : 
    countryCode1 = DK, countryCode2 = CN
    
    treeMap : {AF=AFGHANISTAN, BE=BELGIUM, CN=CHINA, DK=DENMARK}
    
    To Download TreeMapDemoAscOrderCountryCode Project Click the below link
    https://sites.google.com/site/javaee4321/java-collections/TreeMapDemoAscOrderCountryCode.zip?attredirects=0&d=1

    See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java : Collection : TreeMap (Ascending and Descending Order Sample Program) - Playlist

    Java : Collection Framework : TreeMap (Comparator)


    Click here to watch in Youtube : 
    https://www.youtube.com/watch?v=pPIQIlLPVOo&list=UUhwKlOVR041tngjerWxVccw

    DescendingEmployeeIdComparator.java
    import java.util.Comparator;
    
    public class DescendingEmployeeIdComparator implements Comparator<Integer>
    {
    
        /*
         * This method used to arrange the employeeId's in descending order.
         */
        @Override
        public int compare( Integer empId1, Integer empId2 )
        {
    
            if( empId1 < empId2 )
            {
    
                return 1;
            }
            else
            {
                return -1;
            }
        }
    
    }
    
    TreeMapExample.java
    import java.util.Comparator;
    import java.util.TreeMap;
    
    /*
     * Example of comparator() method.
     */
    public class TreeMapExample
    {
        public static void main( String[] args )
        {
    
            DescendingEmployeeIdComparator descendingEmployeeIdComparator 
                                                  = new DescendingEmployeeIdComparator();
    
            TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>(
                    descendingEmployeeIdComparator);
            /*
             * Key is EmployeeId - Value is EmployeeName
             */
    
            treeMap.put(20, "Balu");
            treeMap.put(10, "Ajay");
            treeMap.put(40, "Carol");
            treeMap.put(30, "Dave");
    
            System.out.println("treeMap : " + treeMap + "\n");
            
            Comparator<?> comparator = treeMap.comparator();
            
            System.out.println("Comparator name : "+comparator.getClass().getName());
    
        }
    }
    
    Output
    treeMap : {40=Carol, 30=Dave, 20=Balu, 10=Ajay}
    
    Comparator name : DescendingEmployeeIdComparator
    
    To Download DisplayAllHeadersApp Project Click the below link
    https://sites.google.com/site/javaee4321/java-collections/TreeMapDemoComparator.zip?attredirects=0&d=1

    See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java : Collection Framework : TreeMap (Comparator - Desc Order EmployeeId)


    Click here to watch in Youtube : 
    https://www.youtube.com/watch?v=ty8GK1fCzrQ&list=UUhwKlOVR041tngjerWxVccw

    DescendingEmpIoyeeIdComparator.java
    import java.util.Comparator;
    
    public class DescendingEmpIoyeeIdComparator implements Comparator<Integer>
    {
    
        /*
         * This method used to arrange the employeeId's in descending order.
         */
        @Override
        public int compare( Integer employeeId1, Integer employeeId2 )
        {
    
            System.out
                    .println("\nCompare method has been called in 
                       DescendingEmpIoyeeIdComparator, \nto arrange"
                            + " the employeeId's in descending order : ");
    
            System.out
                    .println("employeeId1 = " + employeeId1 + ", employeeId2 = " + employeeId2 + "\n");
    
            if( employeeId1 < employeeId2 )
            {
                
                return 1;
            }
            else
            {
                return -1;
            }
        }
    
    }
    
    TreeMapExample.java
    import java.util.TreeMap;
    
    /*
     * Example of TreeMap(Comparator<? super K> comparator) Constructor.
     */
    public class TreeMapExample
    {
        public static void main(String[] args)
        {
    
            DescendingEmpIoyeeIdComparator descendingEmpIoyeeIdComparator 
                                                       = new DescendingEmpIoyeeIdComparator();
    
            TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>(
                    descendingEmpIoyeeIdComparator);
            /*
             * Key is EmployeeId - Value is EmployeeName
             */
    
            System.out.println("Key:20,Value:Balu is going to be add in treeMap");
            treeMap.put(20, "Balu");
    
            System.out.println("Key:10,Value:Ajay is going to be add in treeMap");
            treeMap.put(10, "Ajay");
    
            System.out.println("Key:40,Value:Carol is going to be add in treeMap");
            treeMap.put(40, "Carol");
    
            System.out.println("Key:30,Value:Dave is going to be add in treeMap");
            treeMap.put(30, "Dave");
    
            System.out.println("treeMap : " + treeMap + "\n");
    
        }
    }
    
    Output
    Key:20,Value:Balu is going to be add in treeMap
    
    Compare method has been called in DescendingEmpIoyeeIdComparator, 
    to arrange the employeeId's in descending order : 
    employeeId1 = 20, employeeId2 = 20
    
    Key:10,Value:Ajay is going to be add in treeMap
    
    Compare method has been called in DescendingEmpIoyeeIdComparator, 
    to arrange the employeeId's in descending order : 
    employeeId1 = 10, employeeId2 = 20
    
    Key:40,Value:Carol is going to be add in treeMap
    
    Compare method has been called in DescendingEmpIoyeeIdComparator, 
    to arrange the employeeId's in descending order : 
    employeeId1 = 40, employeeId2 = 20
    
    Key:30,Value:Dave is going to be add in treeMap
    
    Compare method has been called in DescendingEmpIoyeeIdComparator, 
    to arrange the employeeId's in descending order : 
    employeeId1 = 30, employeeId2 = 20
    
    
    Compare method has been called in DescendingEmpIoyeeIdComparator, 
    to arrange the employeeId's in descending order : 
    employeeId1 = 30, employeeId2 = 40
    
    treeMap : {40=Carol, 30=Dave, 20=Balu, 10=Ajay}
    
    To Download TreeMapDemoDescOrderEmpId Project Click the below link
    https://sites.google.com/site/javaee4321/java-collections/TreeMapDemoDescOrderEmpId.zip?attredirects=0&d=1

    See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java : Collection Framework : TreeMap (Comparator - Asc Order EmployeeId)


    Click here to watch in Youtube : 
    https://www.youtube.com/watch?v=hBaoCkE5Zks&list=UUhwKlOVR041tngjerWxVccw

    AscendingEmployeeIdComparator.java
    import java.util.Comparator;
    
    public class AscendingEmployeeIdComparator implements Comparator<Integer>
    {
    
        /*
         * This method used to arrange the employeeId's in ascending order.
         */
        @Override
        public int compare( Integer employeeId1, Integer employeeId2 )
        {
    
            System.out
                    .print("Compare method has been called in AscendingEmployeeIdComparator,\nto arrange"
                            + " the EmpId's in ascending order : ");
    
            System.out
                    .println("employeeId1 = " + employeeId1 + ", employeeId2 = " + employeeId2 + "\n");
    
            if( employeeId1 < employeeId2 )
            {
                return -1;
            }
            else
            {
                return 1;
            }
        }
    
    }
    
    TreeMapExample.java
    import java.util.TreeMap;
    
    /*
     * Example of TreeMap(Comparator<? super K> comparator) Constructor.
     */
    public class TreeMapExample
    {
        public static void main( String[] args )
        {
    
            AscendingEmployeeIdComparator ascendingEmployeeIdComparator 
                                                     = new AscendingEmployeeIdComparator();
    
            TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>(
                    ascendingEmployeeIdComparator);
            /*
             * Key is EmployeeId - Value is EmployeeName
             */
            
            System.out.println("Key:20,Value:Balu is going to be add in treeMap.");
            treeMap.put(20, "Balu");
            
            System.out.println("Key:10,Value:Ajay is going to be add in treeMap.");
            treeMap.put(10, "Ajay");
            
            System.out.println("Key:40,Value:Carol is going to be add in treeMap.");
            treeMap.put(40, "Carol");
            
            System.out.println("Key:30,Value:Dave is going to be add in treeMap.");
            treeMap.put(30, "Dave");
    
            System.out.println("treeMap : " + treeMap + "\n");
    
        }
    }
    
    Output
    Key:20,Value:Balu is going to be add in treeMap.
    Compare method has been called in AscendingEmployeeIdComparator,
    to arrange the EmpId's in ascending order : employeeId1 = 20, employeeId2 = 20
    
    Key:10,Value:Ajay is going to be add in treeMap.
    Compare method has been called in AscendingEmployeeIdComparator,
    to arrange the EmpId's in ascending order : employeeId1 = 10, employeeId2 = 20
    
    Key:40,Value:Carol is going to be add in treeMap.
    Compare method has been called in AscendingEmployeeIdComparator,
    to arrange the EmpId's in ascending order : employeeId1 = 40, employeeId2 = 20
    
    Key:30,Value:Dave is going to be add in treeMap.
    Compare method has been called in AscendingEmployeeIdComparator,
    to arrange the EmpId's in ascending order : employeeId1 = 30, employeeId2 = 20
    
    Compare method has been called in AscendingEmployeeIdComparator,
    to arrange the EmpId's in ascending order : employeeId1 = 30, employeeId2 = 40
    
    treeMap : {10=Ajay, 20=Balu, 30=Dave, 40=Carol}
    
    To Download TreeMapDemoAscOrderEmpId Project Click the below link
    https://sites.google.com/site/javaee4321/java-collections/TreeMapDemoAscOrderEmpId.zip?attredirects=0&d=1

    See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Thursday 26 February 2015

    Java : Collection Framework : LinkedHashMap (Add User-Defined Object)


    Click here to watch in Youtube : 
    https://www.youtube.com/watch?v=8XpccS80VuM&list=UUhwKlOVR041tngjerWxVccw

    Employee.java
    public class Employee
    {
    
        private String name;
        private int    age;
        private int    salary;
    
        public Employee( String name, int age, int salary )
        {
            super();
            this.name = name;
            this.age = age;
            this.salary = salary;
        }
    
        public String getName()
        {
            return name;
        }
    
        public void setName( String name )
        {
            this.name = name;
        }
    
        public int getAge()
        {
            return age;
        }
    
        public void setAge( int age )
        {
            this.age = age;
        }
    
        public int getSalary()
        {
            return salary;
        }
    
        public void setSalary( int salary )
        {
            this.salary = salary;
        }
    
        @Override
        public String toString()
        {
            return "Employee [name=" + name + ", age=" + age + ", salary=" + salary
                    + "]";
        }
    
    }
    
    LinkedHashMapExample.java
    import java.util.Map;
    import java.util.Set;
    import java.util.LinkedHashMap;
    
    /*
     * Example of adding User defined Object.
     */
    public class LinkedHashMapExample
    {
        public static void main(String[] args)
        {
    
            LinkedHashMap<Integer, Employee> linkedHashMap = new LinkedHashMap<Integer, Employee>();
    
            Employee john = new Employee("John", 32, 40000);
            Employee david = new Employee("David", 42, 80000);
            Employee peter = new Employee("Peter", 52, 150000);
    
            /*
             * Key is EmpId - Value is Employee Object
             */
    
            linkedHashMap.put(20, john);
            linkedHashMap.put(10, david);
            linkedHashMap.put(40, peter);
    
            System.out.println("linkedHashMap : " + linkedHashMap + "\n");
    
            Set<Map.Entry<Integer, Employee>> set = linkedHashMap.entrySet();
    
            System.out.println("set : " + set + "\n");
    
            System.out.println("-----------------------");
            System.out.println("Key" + " | " + "value");
            System.out.println("-----------------------");
    
            for (Map.Entry<Integer, Employee> entry : set)
            {
                int empId = entry.getKey();
                Employee employee = entry.getValue();
                System.out.println(empId + "   | " + employee);
            }
    
        }
    }
    
    Output
    linkedHashMap : {20=Employee [name=John, age=32, salary=40000], 10=Employee [name=David, age=42, salary=80000], 40=Employee [name=Peter, age=52, salary=150000]}
    
    set : [20=Employee [name=John, age=32, salary=40000], 10=Employee [name=David, age=42, salary=80000], 40=Employee [name=Peter, age=52, salary=150000]]
    
    -----------------------
    Key | value
    -----------------------
    20   | Employee [name=John, age=32, salary=40000]
    10   | Employee [name=David, age=42, salary=80000]
    40   | Employee [name=Peter, age=52, salary=150000]
    
    To Download LinkedHashMapDemoUserDefined Project Click the below link
    https://sites.google.com/site/javaee4321/java-collections/LinkedHashMapDemoUserDefined.zip?attredirects=0&d=1

    See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Wednesday 25 February 2015

    Java : Collection Framework : HashMap (Add User-Defined Object)


    Click here to watch in Youtube : 
    https://www.youtube.com/watch?v=Z1MIdaZ0vEM&list=UUhwKlOVR041tngjerWxVccw

    Employee.java
    public class Employee
    {
    
        private String name;
        private int    age;
        private int    salary;
    
        public Employee( String name, int age, int salary )
        {
            super();
            this.name = name;
            this.age = age;
            this.salary = salary;
        }
    
        public String getName()
        {
            return name;
        }
    
        public void setName( String name )
        {
            this.name = name;
        }
    
        public int getAge()
        {
            return age;
        }
    
        public void setAge( int age )
        {
            this.age = age;
        }
    
        public int getSalary()
        {
            return salary;
        }
    
        public void setSalary( int salary )
        {
            this.salary = salary;
        }
    
        @Override
        public String toString()
        {
            return "Employee [name=" + name + ", age=" + age + ", salary=" + salary
                    + "]";
        }
    
    }
    
    HashMapExample.java
    import java.util.Map;
    import java.util.Set;
    import java.util.HashMap;
    
    /*
     * Example of adding User defined Object.
     */
    public class HashMapExample
    {
        public static void main(String[] args)
        {
    
            HashMap<Integer, Employee> hashMap = new HashMap<Integer, Employee>();
    
            /*
             * Key is EmpId - Value is Employee Object
             */
    
            Employee john = new Employee("John", 32, 40000);
            Employee david = new Employee("David", 42, 80000);
            Employee peter = new Employee("Peter", 52, 150000);
    
            hashMap.put(20, john);
            hashMap.put(10, david);
            hashMap.put(40, peter);
    
            System.out.println("hashMap : " + hashMap + "\n");
    
            Set<Map.Entry<Integer, Employee>> set = hashMap.entrySet();
    
            System.out.println("set : " + set + "\n");
    
            System.out.println("-----------------------");
            System.out.println("Key" + " | " + "value");
            System.out.println("-----------------------");
    
            for (Map.Entry<Integer, Employee> entry : set)
            {
                int empId = entry.getKey();
                Employee employee = entry.getValue();
                System.out.println(empId + "   | " + employee);
            }
    
        }
    }
    
    Output
    hashMap : {20=Employee [name=John, age=32, salary=40000], 40=Employee [name=Peter, age=52, salary=150000], 10=Employee [name=David, age=42, salary=80000]}
    
    set : [20=Employee [name=John, age=32, salary=40000], 40=Employee [name=Peter, age=52, salary=150000], 10=Employee [name=David, age=42, salary=80000]]
    
    -----------------------
    Key | value
    -----------------------
    20   | Employee [name=John, age=32, salary=40000]
    40   | Employee [name=Peter, age=52, salary=150000]
    10   | Employee [name=David, age=42, salary=80000]
    
    To Download HashMapDemoUserDefined Project Click the below link
    https://sites.google.com/site/javaee4321/java-collections/HashMapDemoUserDefined.zip?attredirects=0&d=1

    See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java : Collection Framework : TreeMap (Add User-Defined Object)


    Click here to watch in Youtube :
    https://www.youtube.com/watch?v=KsPY3o22Gww&list=UUhwKlOVR041tngjerWxVccw

    Employee.java
    public class Employee
    {
    
        private String name;
        private int    age;
        private int    salary;
    
        public Employee( String name, int age, int salary )
        {
            super();
            this.name = name;
            this.age = age;
            this.salary = salary;
        }
    
        public String getName()
        {
            return name;
        }
    
        public void setName( String name )
        {
            this.name = name;
        }
    
        public int getAge()
        {
            return age;
        }
    
        public void setAge( int age )
        {
            this.age = age;
        }
    
        public int getSalary()
        {
            return salary;
        }
    
        public void setSalary( int salary )
        {
            this.salary = salary;
        }
    
        @Override
        public String toString()
        {
            return "Employee [name=" + name + ", age=" + age + ", salary=" + salary
                    + "]";
        }
    
    }
    
    TreeMapExample.java
    import java.util.Map;
    import java.util.Set;
    import java.util.TreeMap;
    
    /*
     * Example of adding User defined Object.
     */
    public class TreeMapExample
    {
        public static void main( String[] args )
        {
    
            TreeMap<Integer, Employee> treeMap = new TreeMap<Integer, Employee>();
    
            Employee john = new Employee("John", 32, 40000);
            Employee david = new Employee("David", 42, 80000);
            Employee peter = new Employee("Peter", 52, 150000);
    
            /*
             * Key is EmpId - Value is Employee Object
             */
            treeMap.put(20, john);
            treeMap.put(10, david);
            treeMap.put(40, peter);
    
            System.out.println("treeMap : " + treeMap + "\n");
    
            Set<Map.Entry<Integer, Employee>> set = treeMap.entrySet();
    
            System.out.println("set : " + set + "\n");
    
            System.out.println("-----------------------");
            System.out.println("Key" + " | " + "value");
            System.out.println("-----------------------");
    
            for( Map.Entry<Integer, Employee> entry : set )
            {
                int empId = entry.getKey();
                Employee employee = entry.getValue();
                System.out.println(empId + "   | " + employee);
            }
    
        }
    }
    
    Output
    treeMap : {10=Employee [name=David, age=42, salary=80000], 20=Employee [name=John, age=32, salary=40000], 40=Employee [name=Peter, age=52, salary=150000]}
    
    set : [10=Employee [name=David, age=42, salary=80000], 20=Employee [name=John, age=32, salary=40000], 40=Employee [name=Peter, age=52, salary=150000]]
    
    -----------------------
    Key | value
    -----------------------
    10   | Employee [name=David, age=42, salary=80000]
    20   | Employee [name=John, age=32, salary=40000]
    40   | Employee [name=Peter, age=52, salary=150000]
    
    To Download TreeMapDemoUserDefined Project Click the below link
    https://sites.google.com/site/javaee4321/java-collections/TreeMapDemoUserDefined.zip?attredirects=0&d=1

    See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial