Searching a hashMap

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 17872 Views - Last Post: 19 June 2011 - 06:50 PM Rate Topic: -----

#1 Dolemite88   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 27-December 10

Searching a hashMap

Posted 18 June 2011 - 06:04 AM

public class User
{
  private static int identifier = 100;
  private String userName;
  private int storageCapacity = 1000;
  private int availableSpace;
  private File f;
  private String name;
  private String date;
  private String type;
  private String size;
  HashMap hm = new HashMap();
  Collection c = hm.values();
  
  public User(String userNameIn){
    userName = userNameIn;
    identifier--;
    }
   public void addFile(String nameIn, String dateIn, String typeIn, String sizeIn){
      name = nameIn;
      date = dateIn;
      type = typeIn;
      size = sizeIn;
      f = new File (name, date, type,size);
      hm.put(type,f);
      
    }
    public void searchFilesByType(String type){
          Set<String> theKeys = hm.keySet();
              if(theKeys.equals(type)){
                  System.out.println(hm.get(type));
                }
            
    
    }
    public String getUserName(){
       return userName;
    }

    }

I am trying to use the 'searchfilesbytype' method to iterate through the hashmap and produce values that are identical to those inputted by the user, but I'm getting an undesirable result.

Is This A Good Question/Topic? 0
  • +

Replies To: Searching a hashMap

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3542
  • View blog
  • Posts: 16,146
  • Joined: 20-September 08

Re: Searching a hashMap

Posted 18 June 2011 - 06:15 AM

Quote

if(theKeys.equals(type))


can never be true - a Set can't possibly be equal to a String
Was This Post Helpful? 0
  • +
  • -

#3 Dolemite88   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 27-December 10

Re: Searching a hashMap

Posted 18 June 2011 - 07:38 AM

Noted, so how do I fix this?
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Searching a hashMap

Posted 18 June 2011 - 07:57 AM

Something like, following your actual logic:

    public void searchFilesByType(String type){
          Set<String> theKeys = hm.keySet();
          Iterator<String> itr = theKeys.iterator();
          while(itr.hasNext()) {
              if(itr.next().equals(type)){
                  System.out.println(hm.get(type));
                }
          }
    }


but there are better and better ways like
    	if(hm.containsKey(type)) {
                  System.out.println(hm.get(type));    		
    	}



and File does not have a constructor that expects as parameters String, String, String, String as in

f = new File (name, date, type,size);
Was This Post Helpful? 1
  • +
  • -

#5 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3542
  • View blog
  • Posts: 16,146
  • Joined: 20-September 08

Re: Searching a hashMap

Posted 18 June 2011 - 08:30 AM

Your concept is not really sound. Since a Map can have only unique keys, you will need to have Map<String, List<File>> to map a type name to files of that type. Personally i wouldn't use 'File' as a class name since it already exists in the JDK and you might want to leverage it in your own code.

This post has been edited by g00se: 18 June 2011 - 08:32 AM

Was This Post Helpful? 0
  • +
  • -

#6 Dolemite88   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 27-December 10

Re: Searching a hashMap

Posted 18 June 2011 - 10:30 AM

Thanks but, The iteration only picks out one of the file types and there could be two or more types in the map.
Was This Post Helpful? 0
  • +
  • -

#7 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Searching a hashMap

Posted 18 June 2011 - 10:55 AM

Depends how your File class looks like and especially its equals() method.
The HashMap will allows you to enter only one File object if you try to insert another File object which is equals() to one that is already in the HashMap you can't

So if your File equals() method looks like
boolean equals(File another) {
  return this.name.equals(another.name);
}


you will be able to insert abc.txt and xyz.txt but not abc.java

if your File equals() method looks like
boolean equals(File another) {
  return this.type.equals(another.type);
}


you will be able to insert abc.txt and abc.java but no other .txt or .java
Was This Post Helpful? 0
  • +
  • -

#8 Dolemite88   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 27-December 10

Re: Searching a hashMap

Posted 18 June 2011 - 11:27 AM

File is just the name I gave the class, I changed it to FileSys now, I'm just trying to simulate a filing system with a search function by file type.
Was This Post Helpful? 0
  • +
  • -

#9 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Searching a hashMap

Posted 18 June 2011 - 11:34 AM

So why using a HashMap ?
This is to garanty uniqueness of the elements entered. A file system might have many files with the same name in different directories.
So your FileSys will have to also contains the directory of the file
The FileSys equals() method will have to check for directory, name and type and return false if one of the 3 is different

To get all the FileSys of a certain type, you will have to make an ietrator as in the first part of my first reply and extract all the FileSys with a centain type by scanning all the FileSys in the Set

Happy coding
Was This Post Helpful? 0
  • +
  • -

#10 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3542
  • View blog
  • Posts: 16,146
  • Joined: 20-September 08

Re: Searching a hashMap

Posted 18 June 2011 - 02:27 PM

Quote

File is just the name I gave the class, I changed it to FileSys now, I'm just trying to simulate a filing system with a search function by file type.


That's ok, but you'll really need to do what i suggested. Personally i would use enum rather than String to define your types
Was This Post Helpful? 0
  • +
  • -

#11 Dolemite88   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 27-December 10

Re: Searching a hashMap

Posted 18 June 2011 - 06:09 PM

  public class User
{  
  private ArrayList<FileSys>file=new ArrayList<FileSys>();
  private static int identifier = 100;
  private String userName;
  private int storageCapacity = 1000;
  private int availableSpace;
  private FileSys f;
  private String name;
  private String date;
  private String type;
  private String size;

  
  public User(String userNameIn){
    userName = userNameIn;
    identifier--;
    }
     public void addFile(String nameIn, String dateIn, String typeIn, String sizeIn){
      name = nameIn;
      date = dateIn;
      type = typeIn;
      size = sizeIn;
      f = new FileSys(name, date, type,size);
      file.add(f);
      
    }



public void searchFilesByType(String typeIn){
	 type = typeIn;
	 Iterator<FileSys> itr = file.iterator();
	 while(itr.hasNext()){
	     if(itr.next().getType().equals(typeIn)){
	         System.out.println(f);
	       }
  

   }


I changed the approach from hashMap to an Arraylist same result I just get one file....frustrating now...
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12306
  • View blog
  • Posts: 45,403
  • Joined: 27-December 08

Re: Searching a hashMap

Posted 18 June 2011 - 07:02 PM

Look at the search method. You only print out the instance field FileSys object. Instead, you should invoke the next() method on the Iterator and assign it to a variable. If the type of the next element is the same as the type to search for, print out that object, not the instance field.
System.out.println(f);


Was This Post Helpful? 0
  • +
  • -

#13 Dolemite88   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 27-December 10

Re: Searching a hashMap

Posted 18 June 2011 - 07:24 PM

 public void searchFilesByType(String typeIn){
	    FileSys ff;
	    Iterator<FileSys> i = file.iterator();
        while(i.hasNext()){
            ff=i.next();
            if(i.next().getType().equals(typeIn)){
               System.out.println(ff);
              
               
            }
        }
	  
	  
   }


I tried that, still didn't work...I'm sure the answer is really simple arrgh.
Was This Post Helpful? 0
  • +
  • -

#14 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Searching a hashMap

Posted 18 June 2011 - 08:08 PM

You are calling next() twice in the loop ignoring the one you had retreive in ff

 public void searchFilesByType(String typeIn){

	    Iterator<FileSys> i = file.iterator();
        while(i.hasNext()){
            ff=i.next();
            if(ff.getType().equals(typeIn)){   // use ff here not next() again
               System.out.println(ff);
              
               
            }
        }
	  
	  
   }

Was This Post Helpful? 1
  • +
  • -

#15 Dolemite88   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 27-December 10

Re: Searching a hashMap

Posted 19 June 2011 - 12:46 AM

I'm just an idiot, thanks.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2