Results 1 to 6 of 6

Thread: Collections

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Posts
    843

    Collections

    What is the class to make Collection in Java?
    "The difference between mad and genius is the success"

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Vector and ArrayList are the more popular ones. I've never used ArrayList, but Vectors work like :
    Code:
    Vector v = new Vector;
    Object o = new Object();
    
    v.addElement(o); //adds an Object to the Vector, and increases its size by 1
    v.removeElement(o); //removes the Object from the Vector and decreases its size by 1
    v.elementAt(index); //returns the Object at the given index
    v.elements(); //returns an Enumeration of all the Objects that the Vector contains
    v.contains(o); //returns true if the Object exists in the Vector
    Thats the basics of a Vector.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Vector, ArrayList, LinkedList are off the the List interface so depending on your requirments(lists allow duplicate objects, and maintain order) you can opt to use one of these concrete implementations. If you do not wish duplicates to be stored then i would go with using a set. TreeSet is a concrete implementation
    which implements the SortedSet interface so this would essentially give you order plus not allow duplicates to be stored.

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    To loop through a Vector that contains Strings you could do somthing like the following.
    Code:
      for(Iterator i = v.iterator();i.hasNext;){
       String s = (String) i.next();
       System.out.println(s); 
     }
    The iterator interface contains the following.
    public abstract boolean hasNext();
    public abstract Object next();
    public abstract void remove();

    So what you are essentially doing is invoking a method in the Vector class that returns a refrence or a handle the iterator interface so those methods can be invoked.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Posts
    843
    wow... thank you!! Dilenger4 and crptcblade. I will try them all. Thank you for all the information on the methods too!
    "The difference between mad and genius is the success"

  6. #6
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    One more thing. The vector and hashtable classes are declared synchronized so if you are looking for better performance an ArrayList would be the way to go since it is not declared synchronized.

    If you want you can actually synchronize and unsynchronized class through a decorator object.
    Code:
      Collections syncSet = Collections.synchronizeSet(someset); 
      // each thread must iterate when synchronized on the decorator
      synchronize(syncSet){
      for(Iterator i = syncSet.iterator(); i.hasNext();){
        processData(i.next());
      }  
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width