Sabtu, 16 Mei 2015

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

Baca selengkapnya

Jumat, 15 Mei 2015

JAVA - How To Use CardLayout In Java NetBeans

CardLayout Example In Java NetBeans

                                                                                                                                                            

In this java Code we will see How To Use The BorderLayout LayoutManager In Java NetBeans

Source Code:


import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Window {
       static JFrame frame = new JFrame();
       static JPanel PANEL = new JPanel(new BorderLayout());
       static JPanel panelButton = new JPanel();
       static JPanel panelText = new JPanel();
       static JPanel panelLabel = new JPanel();
       static JPanel panelRed = new JPanel();
       static JPanel panelGreen = new JPanel();
       static JPanel panelBlack = new JPanel();
       static JPanel panelWhite = new JPanel();
       static CardLayout card = new CardLayout();
       
    public static void main(String[] args){

        panelText.setName("panelText");
        panelLabel.setName("panelLabel");
        
        JButton buttonText = new JButton("TextField");
        JButton buttonLabel = new JButton("Label");
        JButton buttonFirst = new JButton("First");
        JButton buttonLast = new JButton("Last");
        JButton buttonNext = new JButton("Next");
        JButton buttonPrevious = new JButton("Previous");
        
        JTextField text1 = new JTextField(20);
        JTextField text2 = new JTextField(20);
        JTextField text3 = new JTextField(20);
        
        JLabel label1 = new JLabel("label1");
        JLabel label2 = new JLabel("label2");
        JLabel label3 = new JLabel("label3");
      
        panelButton.add(buttonText);
        panelButton.add(buttonLabel);
        panelButton.add(buttonFirst);
        panelButton.add(buttonLast);
        panelButton.add(buttonNext);
        panelButton.add(buttonPrevious);
        
        panelText.add(text1);
        panelText.add(text2);
        panelText.add(text3);
        
        panelLabel.add(label1);
        panelLabel.add(label2);
        panelLabel.add(label3);
        
        panelRed.setBackground(Color.red);
        panelGreen.setBackground(Color.green);
        panelBlack.setBackground(Color.black);
        panelWhite.setBackground(Color.white);
        
        
        // show the card who contain JTextFields
        buttonText.addActionListener(new ActionListener(){
         
            @Override
            public void actionPerformed(ActionEvent e){
                card.show(PANEL, "panelText");
            }
        });
        
        // show the card who contain JLabels
        buttonLabel.addActionListener(new ActionListener(){
         
            @Override
            public void actionPerformed(ActionEvent e){
                card.show(PANEL, "panelLabel");
            }
        });
        
         // show the first card
        buttonFirst.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
               card.first(PANEL);
            }
        });
        
        // show the last card
        buttonLast.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
             
                card.last(PANEL);
            }
        });
        
        // show the next card
        buttonNext.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
            
                card.next(PANEL);
            }
        });
        
        // show the previous card
        buttonPrevious.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                card.previous(PANEL);
            }
        });

        // set a Hgap to the cardlayout
        card.setHgap(20);
        
        // set a Vgap to the cardlayout
        card.setVgap(20);
        
        PANEL.setLayout(card);
        PANEL.add(panelText,"panelText");
        PANEL.add(panelLabel,"panelLabel");
        PANEL.add(panelRed,"RED");
        PANEL.add(panelGreen,"GREEN");
        PANEL.add(panelBlack,"BLACK");
        PANEL.add(panelWhite,"WHITE");

        card.layoutContainer(PANEL);
        
        frame.getContentPane().add(panelButton,BorderLayout.NORTH);
        frame.getContentPane().add(PANEL,BorderLayout.CENTER);
        
        frame.setTitle("CardLayout");
        frame.setSize(500, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

//OUTPUT:


java cardlayout
using cardlayout in java

Baca selengkapnya

JAVA - How To Use BorderLayout In Java NetBeans

BorderLayout Example In Java NetBeans

                                                                                                                                                            

In this java Code we will see How To Use The BorderLayout LayoutManager In Java NetBeans

Source Code:

package javaapp;

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class SwingLayout {

    public static void main(String[] args){
        
      JFrame frame = new JFrame("BorderLayout");
      
      JPanel panel1 = new JPanel();
      
      panel1.setLayout(new BorderLayout());
      
      // set the button in the top of the container
      panel1.add(new JButton("NORTH"), BorderLayout.NORTH);
      
      // set the button in the right of the container
      panel1.add(new JButton("EAST"), BorderLayout.EAST);
      
      // set the button in the middle of the container
      panel1.add(new JButton("CENTER"), BorderLayout.CENTER);
      
      // set the button in the top of left container
      panel1.add(new JButton("WEST"), BorderLayout.WEST);
      
      // set the button in the bottom of the container
      panel1.add(new JButton("SOUTH"), BorderLayout.SOUTH);
     
      // create an instance of borderlayout with gaps
      BorderLayout bl = new BorderLayout(50,50);
      
      JPanel panel2 = new JPanel(bl);
      
      panel2.add(new JButton("NORTH"), BorderLayout.NORTH);
      panel2.add(new JButton("EAST"), BorderLayout.EAST);
      panel2.add(new JButton("CENTER"), BorderLayout.CENTER);
      panel2.add(new JButton("WEST"), BorderLayout.WEST);
      panel2.add(new JButton("SOUTH"), BorderLayout.SOUTH);
      
        bl.removeLayoutComponent(new JButton("NORTH"));
      
      // return the state of the borderlayout Hgap and Vgap
      System.out.println(bl.toString());
      
      // return the borderlayout Hgap and Vgap
      System.out.println(bl.getHgap()+" - "+bl.getVgap());     
      
      frame.add(panel2);     
       
      frame.setSize(400, 400);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
      
    }
}


//OUTPUT:

borderlayout in java
using the panel1

java borderlayout
using the panel2

java.awt.BorderLayout[hgap=50,vgap=50]
50 - 50
Baca selengkapnya

Rabu, 13 Mei 2015

Java - How To Use HashMap With BiFunction In Java

JAVA Collection - Using HashMap With BiFunction In Java NetBeans

                                                                                                                                                            
hasmap with BiFunction in java



In this java Collection Code we will see How To Use A HashMap With BiFunction In Java        NetBeans .

Source Code:

package javaapp;

import java.util.HashMap;

// create a class to use it with the hashmap
   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;
       }
       
       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>();
        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"));
  
         printAllData(userList);

      User u1 = userList.get(54);
      User u2 = new User(600, "TheNewUser");
      
      BiFunction<User, User,Boolean> bi = (x, y) -> {      
      Boolean state = false;

      for(Integer i : userList.keySet()){
          if(userList.get(i).equals(x)){
              userList.replace(i, y);
              state = true;
              break;
          }
      }
       return state;
    };
     
    System.out.println(bi.apply(u1, u2)); 
    System.out.println("---------------------");
    printAllData(userList);
   }  
    
    // cretate a function to print data from the hashmap 
    public static void printAllData(HashMap<Integer,User> list){
      for(Integer i : list.keySet())
        {
          System.out.println(list.get(i).ToString());
        }
      System.out.println("----------------------");
    }
     

}

//OUTPUT:

ID = 3  NAME = kamal
ID = 6  NAME = karim
ID = 2  NAME = bilal
ID = 5  NAME = rami
ID = 1  NAME = omar
ID = 4  NAME = rachid
----------------------
true
---------------------
ID = 3  NAME = kamal
ID = 6  NAME = karim
ID = 2  NAME = bilal
ID = 600  NAME = TheNewUser
ID = 1  NAME = omar
ID = 4  NAME = rachid

----------------------
Baca selengkapnya

Minggu, 10 Mei 2015

JAVA - How To Use Vector Collection In Java

How To Use Vector Collection In Java - Java Vector Example

                                                                                                                                                            
java vector collection

In this java Collection tutorial we will see How To Use A Vector In Java NetBeans
With Some Java Vector Example of Methodes.
1: how to create a Vector
2: how to dispaly data from it
3: how to add an elements in the first
4: how to add an elements in the end
5: how to clear all elements from it
and more........

Source Code:


package javaapp;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class Woirk {

    public static void main(String[] args){
       
       //create vector one v
       Vector v = new Vector(); 
       v.add(54);
       v.add("java");
       v.add("csharp");
       v.add(100);
       v.add("java");
       v.add("pc");
       v.add("laptop");
       v.add(10);
       System.out.println(v);
       
       //create vector two v2
       Vector v2 = new Vector();
       v2.add("java");
       v2.add(2015);
       v2.add("V2");
       
       //add v2 to v
       v.addAll(v2);
       System.out.println(v);
      
       //add element at the specified position
       v.add(2, 222);
       System.out.println(v);
       
       //create a vector three v3
       Vector v3 = new Vector();
       v3.add("V3");
       v3.add("VCT");
       
       v.addAll(v3);
       
     //add collection at the specified position to another collection
       v.addAll(0, v3);
       System.out.println(v);
       
       //add element and incress the size by one
       v.addElement("AddElement");
       
       //insert element at the specified position
       v.insertElementAt("insertElementAt", 5);
       
       //get the capacity of the vector
       int vectorCapacity = v.capacity();
       System.out.println(vectorCapacity);
       
       //check if an element exist in the collection
       boolean exist = v.contains(2015);
       System.out.println("2015 Exist: "+exist);
       
       //check if a collection exist in the vector
       boolean containCollection = v.containsAll(v3);
       System.out.println("V3 Exist: "+containCollection);
      
       //get the object by his index
       Object obj = v.elementAt(1);
       System.out.println(obj);
    
       //return Enumeration of the victor
       Enumeration<Object> e = v.elements();
       while(e.hasMoreElements()){
       System.out.print(e.nextElement()+" - ");
       }
       System.out.println();
       
       //insert element in the specified place
       v.set(0,1111);
       v.set(1, 2222);
       System.out.println(v);
       
       // get a sub list between two index
       List subV = v.subList(2,7);
       System.out.println(subV);
       
       //how to use toArray() methode with vector
       Object[] obj2 = v.toArray();
       for(Object ob : obj2){
           System.out.print(ob+", ");
       }
       System.out.println();
       
      //how to use toArray(T[] a) methode with vector
       Object[] ob = v.toArray(new Object[0]);
       for(Object o : ob){
           System.out.print(o+", ");
       }
       System.out.println();
       
       //display all vector items with iterator
       Iterator it = v.iterator();
       while(it.hasNext()){
       System.out.print(it.next()+", ");
       }
       System.out.println();
       
       //create a copy from a vector
       Object[] copy = new Object[v.size()];
       v.copyInto(copy);
       for(Object o : copy){
           System.out.print(o+", ");
       }System.out.println();
       
       //clone the vector
       Object VectorClone = v.clone();
       System.out.println(VectorClone);
       
       //get the element at a specified index
       Object elementAt = v.elementAt(3);
       System.out.println(elementAt);
       
       //check the collection is equals to another collection
       System.out.println(v.equals(v));
       
       //return the element at a specified index
       System.out.println(v.get(0));
       
 //Returns the index of the first occurrence of the element in the vector
       // -1 if not exist
       System.out.println(v.indexOf("java"));
       
       /*
  get the index of the first occurrence of an element after the given index
       */
       System.out.println(v.indexOf("java",4));
       
       //return the last index of an element
       System.out.println(v.lastIndexOf("java"));
       
     /*
  get the index of the last occurrence of an element after the given index
       */
       System.out.println(v.lastIndexOf("java",4));
       
       //return the last element of the victor
       System.out.println(v.lastElement());
       
       //check if the vector is empty
       System.out.println(v.isEmpty());
       
       //remove element by index
       Object removedByIndex = v.remove(0);
        System.out.println(removedByIndex);
     
        //remove element by his value
        //only the first occurrence
        boolean removedByVal = v.remove("VCT");
        System.out.println(removedByVal);
        
        //remove a collection from the victor
        boolean removeCollection = v.removeAll(v3);
        System.out.println(removeCollection);
        
        //the element 2015 will be deleted
        v.removeElement(2015);
        
        //the element who has the index 2 will be deleted
        v.removeElementAt(2);
        
        //return the victor size
        int vSize = v.size();
        System.out.println(vSize);
        
        //clear all elements
        v.clear();
        //clear all elements and set the size to zero
        v.removeAllElements();
        vSize = v.size();
        System.out.println(vSize);
        v.removeAllElements();
    }
}

//OUTPUT:

[54, java, csharp, 100, java, pc, laptop, 10]
[54, java, csharp, 100, java, pc, laptop, 10, java, 2015, V2]
[54, java, 222, csharp, 100, java, pc, laptop, 10, java, 2015, V2]
[V3, VCT, 54, java, 222, csharp, 100, java, pc, laptop, 10, java, 2015, V2, V3, VCT]
20
2015 Exist: true
V3 Exist: true
VCT
V3 - VCT - 54 - java - 222 - insertElementAt - csharp - 100 - java - pc - laptop - 10 - java - 2015 - V2 - V3 - VCT - AddElement - 
[1111, 2222, 54, java, 222, insertElementAt, csharp, 100, java, pc, laptop, 10, java, 2015, V2, V3, VCT, AddElement]
[54, java, 222, insertElementAt, csharp]
1111, 2222, 54, java, 222, insertElementAt, csharp, 100, java, pc, laptop, 10, java, 2015, V2, V3, VCT, AddElement, 
1111, 2222, 54, java, 222, insertElementAt, csharp, 100, java, pc, laptop, 10, java, 2015, V2, V3, VCT, AddElement, 
1111, 2222, 54, java, 222, insertElementAt, csharp, 100, java, pc, laptop, 10, java, 2015, V2, V3, VCT, AddElement, 
1111, 2222, 54, java, 222, insertElementAt, csharp, 100, java, pc, laptop, 10, java, 2015, V2, V3, VCT, AddElement, 
[1111, 2222, 54, java, 222, insertElementAt, csharp, 100, java, pc, laptop, 10, java, 2015, V2, V3, VCT, AddElement]
java
true
1111
3
8
12
3
AddElement
false
1111
true
true
13
0



Baca selengkapnya