hashmap /vector searching?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: hashmap /vector searching?

  1. #1
    Join Date
    Sep 2004
    Posts
    59

    hashmap /vector searching?

    Hello

    Im using a textfield and a button to check if a certain object extist in a vector/hasmap, if it does the object will be displayed in a panel.

    How do i pass the text in the vector / hashmap and search within it?
    My own java video tutorials, feel free to watch it.
    http://www.engineeringserver.com/for...amming-b311.0/ Need java help? For beginning and intermediate java coders!

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    You don't pass anything into the collections, you loop through all the elements of each.

    Say I had a Vector containing Song objects. And you searched by song titles.
    Code:
    Vector songs = new Vector();
    //add Song objects to vector
    
    String search = "some user typed title";
    
    for(int i=0; i<songs.size(); i++)
    {
        String title = ((Song)songs.get(i)).getTitle();
        if (title.indexOf(search) > -1)
        {
             //the object you seek is at index "i"
             break;
        }
    }

  3. #3
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    For your hashmap you have some unique "key" which is fed to a hashing function which tells you exactly where to look for the associated "value or object". If you are using the API's Hashmap, use the get() method, passing the key as the argument to the method. If the object is in the structure, you'll have the object returned, otherwise you receive a null.

    For your program you'll have a unique key associated with each object. The event triggered by a button push will be passing the key associated with the object of interest to the Hashmap object's get() method. Then you need to handle the object returned.

  4. #4
    Join Date
    Feb 2006
    Location
    Cologne - Germany
    Posts
    271
    Create a vector:
    Code:
    Vector v = new Vector();
    Add to vector:
    Code:
    v.add("mystring");
    search for it:
    Code:
    if (v.contains("mystring")) System.out.println("found");
    else  System.out.println("NOT found");
    Create a hashmap:
    Code:
    Map m = new HashMap();
    add to hashmap:
    Code:
    m.put("mystring", "mystring");
    search for it:
    Code:
    if (m.containsKey("mystring")) System.out.println("found");
    else  System.out.println("NOT found");
    by the way: i sugest to use a hashset for this purpose,
    since you don't have to bother about a key, have maximum performance on search and you can be shure, every element exists only once (that's the principle of a set):

    Code:
    Set s = new HashSet();
    s.add("mystring");
    if (s.contains("mystring")) System.out.println("found");
    else  System.out.println("NOT found");

Similar Threads

  1. Replies: 1
    Last Post: 02-27-2006, 11:20 AM
  2. hashmap
    By big_bull81 in forum C++
    Replies: 1
    Last Post: 12-29-2005, 11:24 AM
  3. getting Employee Object out of HashMap
    By fischen in forum Java
    Replies: 7
    Last Post: 12-27-2005, 06:37 PM
  4. Serialize HashMap?
    By Tim Cornwell in forum Java
    Replies: 2
    Last Post: 04-24-2002, 01:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center