Java foundation series - JAVA collection ArrayList, Vector, HashMap, HashTable and other use

com.test4 Package; 

Import Classes in java.util *;. 

/ ** 
 * set the JAVA ArrayList, Vector, HashMap, HashTable such as the use 
 * / 
public class Test4 { 
    public static void main (String [] args) { 
        // the ArrayList asynchronous thread unsafe, high-performance Vector synchronized, thread-safe, low-performance 
        // define an ArrayList object 
        ArrayList = arrayList new new ArrayList (); 
        // display size 
        System.out.println (arrayList.size ()); 
        // add employees 
        Employer employer1 = new Employer (1000, "Joe Smith", 20 is); 
        Employer Employer employer2 new new = (1001, "John Doe", 25); 
        Employer Employer employer3 new new = (1002, "Wang Wu", 30); 
        arrayList.add ( employer1); 
        arrayList.add (employer2);
        arrayList.add (employer3); 
        Employer employer = (Employer) arrayList.get ( 0);

        System.out.println ( "--------- foreach loop ------------------- the ArrayList"); 
        // ITER + Tab shortcut key generation foreach code block 
        for (Object Item: the arrayList) { 
            System.out.println ( "employee information:" + item.toString ()); 
        } 


        System.out.println ( "--------- loop for ArrayList ------------------- "); 
        // Fori for rapid generation block 
        for (int I = 0; I <arrayList.size (); I ++) { 
            the System. out.println ( "employee information:" + arrayList.get (I) .toString ()); 
        } 

        System.out.println ( "--------- ------- employee information acquiring a ------------ "); 
        // get an object 
        System.out.println (" employee information: "+ employer.getName ()); 

        System.out.println ("--------- ----------------- display ArrayList size "); 
        // display size 
        System.out.println (arrayList.size ()); 

        System.out.println ( "------ --- HashMap use ------------------- "); 
        / ** 
         * HashMap HashMap asynchronous use, thread-safe, high-performance HashTable synchronization, thread-safe, low performance 
         * / 
        the Map = new new HM the HashMap (); 
        hm.put ( "1000", employer1); 
        hm.put ( "1001", employer2); 
        hm.put ( "1002", employer3); 
        // iterators 
        iterator iter hm.keySet = () Iterator ();. 
        the while (iter.hasNext ()) { 
            // remove Key 
            String Key = iter.next () toString ();. 
            // fetch objects according to the Key
            EMP = Employer (Employer) hm.get (Key); 
            System.out.println ( "employee information:" + emp.toString ()); 
        } 


        / ** 
         * console display 
         0 
         employee information: Employer {eid = 1002, name = ' Wang Wu'} = 30 Age 
         --------- employee information acquiring a ------------------- 
         an employee information: Zhang
         --------- foreach loop ArrayList ------------------- 
         employee information: Employer {eid = 1000, name = ' John Doe', age = 20 } 
         employee information: Employer {eid = 1001, name = ' John Doe', age = 25} 
         employee information: Employer {eid = 1002, name = ' Wang Wu', Age = 30} 
         --------- ArrayList ------------------- loop for 
         employee information: Employer {eid = 1000, name = ' John Doe', age = 20} 
         employee information: Employer {eid = 1001 , name = 'John Doe', Age = 25} 
         --------- display size ------------------- ArrayList 
         . 3 
         ------ --- ------------------- using the HashMap 
         employee information: Employer {eid = 1002, name = ' Wang Wu', age = 30} 
         employee information: Employer {eid = 1001, name = 'John Doe', age = 25} 
         employee information: Employer {eid = 1000, name = ' John Doe',age=20}
         */
    } 
} 


class Employer { 
    public Employer (int EID, String name, int Age) { 
        this.eid = EID; 
        this.name name = ; 
        this.age = Age; 
    } 

    Private int EID; 
    Private String name; 
    Private int Age; 

    public int getEid () { 
        return EID; 
    } 

    public void setEid (int EID) {
        this.eid = eid;
    }

    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;
    }

    @Override
    public String toString() {
        return "Employer{" +
                "eid=" + eid +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

  

Guess you like

Origin www.cnblogs.com/smartsmile/p/11547027.html