views:

365

answers:

4

I have an hashmap declared as private HashMap testMessages = null;

I will be storing string values in both key and value part of the hashmap retrieved from oracle table.

I am not concerned about the hashmap keys. I want to retrieve the hashmap values alone and check whether string variable filename is prefixed with one of the hash map value and return true if it's same. I want to ensure that hash map values are not null and empty.

function (string filename) {..

loop thru hashmap values

check whether the variable file name is prefixed with one of the hashmap values if so return true otherwise return false }

hashmap example: key1,prod key2,test key3,dummy
filename example: test123_20012010.csv

should return true since the file name is prefixed with one of the hashmap values

How can i do it?

A: 

leeched from leepoint.net

public static void iterate_over_hashmap(Map mp) {
    Iterator it = mp.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pairs = (Map.Entry)it.next();
        System.out.println(pairs.getKey() + " = " + pairs.getValue());
    }
}

You have to treat each entry as a key/value pair and iterate over those as a single entity. Then you cast it into Map.Entry and then you can read both separately

Eric
It's a hashmap private HashMap can i pass it to a Map? private HashMap<Object,Object> testMessages = null;Can i call iterate_over_hash(testMessage) ? Also how can check whether the variable file name is prefixed with one of the hashmap values if so return true otherwise return false
Arav
He said that he doesn't care about the keys.
polygenelubricants
+4  A: 
for (String prefix : map.values()) {
   if (filename.startsWith(prefix)) {
      return true;
   }
}
return false;

It should be noted that this is linear time in the number of entries in the map in the worst case. If you have multiple filename that you want to do the check for, it's much better to preprocess the prefixes and build something like a patricia trie and other fast dictionary-like data structures.

polygenelubricants
I am getting compilation errorsTestHash.java:20: incompatible typesfound : java.lang.Objectrequired: java.lang.Stringfor (String prefix : hMap.values()) {Note: TestHash.java uses unchecked or unsafe operations.Note: Recompile with -Xlint:unchecked for details.1 error
Arav
How did you declare `hMap`? Is it just `HashMap hMap`? What is the type of the keys? Would something like `HashMap<String, String> hMap` work?
polygenelubricants
HashMap<Object,Object> hMap = null; hMap = new HashMap<Object,Object>(); hMap.put("1","One"); hMap.put("2","Two"); hMap.put("3","Three");javac TestHash.javaTestHash.java:26: incompatible typesfound : java.lang.Objectrequired: java.lang.Stringfor (String prefix : hMap.values()) { ^1 error
Arav
Looks to me that your keys and values are both `String` types. Then simply use `HashMap<String, String>` instead of `HashMap<Object, Object>`.
polygenelubricants
A function retrieving the records from a table and it get's stored as key and value. Both key and value are varchar2 datatype. The function retrieve these data and puts it as String in the below hashmap, HashMap<Object,Object> hMap = null; hMap = new HashMap<Object,Object>();for testing purpose i am writing a sample program and inserting string values. hMap.put("2","Two"); hMap.put("3","Three");
Arav
If you put them into the map as `String`, then use `HashMap<String, String>` instead of `HashMap<Object, Object>` to take advantage of the type-safety and convenience that generic offers.
polygenelubricants
Thanks a lot for the info
Arav
+1  A: 

Here's a brute force approach to iterate over the hash map values and check whether filename begins with the value.

// generics version
private HashMap<String, String> testMessages = buildMap();

for (String v : testMessages.values()) {
  if (filename.startsWith(v) {
    // found a map value that starts the file name
  }
}

// alternative non-generics version
private HashMap testMessages; // assigned somewhere

for (Object v : testMessages.values()) {
  if (filename.startsWith((String) v) {
    // found a map value that starts the file name
  }
}
marklai
I dont want to use buildmapI am getting compilation errors TestHash.java:20: incompatible types found : java.lang.Object required: java.lang.String for (String prefix : hMap.values()) { Note: TestHash.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 1 error
Arav
Replace buildMap() with whatever method you use to build the testMessages HashMap. My example uses generics so you wouldn't have to cast the Object instances you get out of the HashMap to a String.
marklai
updated answer with non-generics example
marklai
Thanks a lot for the solution
Arav
A: 
function(String fileName)
{
     for(String value : hashMap.values())
     {
      if(fileName.startsWith(value))
           return true;
     }
  return false;
}
Anantha Kumaran
@polygenelubricants i should take more time to read the question carefully.
Anantha Kumaran
Thanks a lot for the info
Arav