JAVA - How To Use HashMap With Class In Java NetBeans

JAVA Collection - Using HashMap With Class As Value In Java NetBeans

                                                                                                                                                            
using hasmap with your own class in java



In this java Collection Code we will see How To Use HashMap With Our Class As Value In Java NetBeans 


Source Code:

package javaapp;

import java.util.HashMap;

 // Creat a class
   class User{
       private int id;
       private String name;
       
       public User(){}
       
       public User(int _id,String _name){
          this.id = _id;
          this.name =_name;
       }
       
       public int getId(){
           return this.id;
       }
       
       public String getName(){
           return this.name;
       }
       
       public void setId(int id){
           this.id = id;
       }
       
       public void setName(String name){
           this.name = name;
       }
       
// create a tostring methode
       public String ToString(){
           return "ID = "+id+"  NAME = "+name ;
       }
   }

public class Woirk {

    
    public static void main(String[] args){

        HashMap<Integer,User> userList = new HashMap<Integer,User>();

// filling the HashMap with put method
        userList.put(10, new User(1,"omar"));
         userList.put(21, new User(2,"bilal"));
          userList.put(32, new User(3,"kamal"));
           userList.put(43, new User(4,"rachid"));
            userList.put(54, new User(5,"rami"));
             userList.put(65, new User(6,"karim"));
             
// create another hashmap
      HashMap<Integer,User> userList2 = new HashMap<Integer,User>();
        userList2.put(53, new User(62,"khalid"));
          userList2.put(25, new User(18,"mounir"));
          
// replace element in hashmap
        userList.replace(10, new User(100,"tarik"), new User(1,"omar"));

        // insert a map elements in another map
        userList.putAll(userList2);

        printAllData(userList);

        userList.remove(3);
        
        printAllData(userList);
          
        User u = userList.replace(10, new User(8,"morad"));
        
        printAllData(userList);
        
        userList.put(400,null);

/*
       If the specified key is not associated with a value
       or the value is null associates it with the given value                        in this example  the key is 400 and value is null
*/
        userList.putIfAbsent(400, new User(19,"TEXT"));
      
        printAllData(userList);
        
       User us = userList.getOrDefault(400,null);
       System.out.println(us.ToString());
     

      User u1 = userList.get(54);
      User u2 = new User(600, "TheNewUser");
  
    System.out.println("---------------------");
    printAllData(userList);
   userList.remove(54, u1);
       printAllData(userList);
   
    System.out.println("Size: "+userList.size());
    userList.clear();
    System.out.println("The userList is Empty: "+userList.isEmpty());
  }  
    
    // method to print hashmap data
    public static void printAllData(HashMap<Integer,User> list){
      for(Integer i : list.keySet())
        {
          System.out.println(list.get(i).ToString());
        }
      System.out.println("----------------------");
    }
    
 // method to print hashmap keys
    public static void printKeys(HashMap<Integer,User> list){
      for(Integer i : list.keySet())
        {
          System.out.println(i);
        }
      System.out.println("----------------------");
    } 
}

//OUTPUT:

ID = 3  NAME = kamal
ID = 6  NAME = karim
ID = 2  NAME = bilal
ID = 62  NAME = khalid
ID = 5  NAME = rami
ID = 18  NAME = mounir
ID = 1  NAME = omar
ID = 4  NAME = rachid
----------------------
ID = 3  NAME = kamal
ID = 6  NAME = karim
ID = 2  NAME = bilal
ID = 62  NAME = khalid
ID = 5  NAME = rami
ID = 18  NAME = mounir
ID = 1  NAME = omar
ID = 4  NAME = rachid
----------------------
ID = 3  NAME = kamal
ID = 6  NAME = karim
ID = 2  NAME = bilal
ID = 62  NAME = khalid
ID = 5  NAME = rami
ID = 18  NAME = mounir
ID = 8  NAME = morad
ID = 4  NAME = rachid
----------------------
ID = 3  NAME = kamal
ID = 19  NAME = TEXT
ID = 6  NAME = karim
ID = 2  NAME = bilal
ID = 62  NAME = khalid
ID = 5  NAME = rami
ID = 18  NAME = mounir
ID = 8  NAME = morad
ID = 4  NAME = rachid
----------------------
ID = 19  NAME = TEXT
---------------------
ID = 3  NAME = kamal
ID = 19  NAME = TEXT
ID = 6  NAME = karim
ID = 2  NAME = bilal
ID = 62  NAME = khalid
ID = 5  NAME = rami
ID = 18  NAME = mounir
ID = 8  NAME = morad
ID = 4  NAME = rachid
----------------------
ID = 3  NAME = kamal
ID = 19  NAME = TEXT
ID = 6  NAME = karim
ID = 2  NAME = bilal
ID = 62  NAME = khalid
ID = 18  NAME = mounir
ID = 8  NAME = morad
ID = 4  NAME = rachid
----------------------
Size: 8
The userList is Empty: true




Share this

Related Posts

Previous
Next Post »