Results 1 to 5 of 5
  1. #1
    elvinny is offline Member
    Join Date
    May 2008
    Posts
    2
    Rep Power
    0

    Default Hashmap to TXT and TXT to Hashmap

    Hi!!!
    I'm Luis from Portugal and i would like to know how i can save values from a HashMap(huge size) to a text file(.txt, .dat or something else that can be read). After that how can i load that file to a HasMap.

    Thanks:)

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    It depends on you.... ( structuring patterns )

    You can use FileWriter class for saving values in a file.

    You can use Scanner class for reading back the generated text file.
    freedom exists in the world of ideas

  3. #3
    elvinny is offline Member
    Join Date
    May 2008
    Posts
    2
    Rep Power
    0

    Default

    I'm using an Hashmap to keep float values:

    Java Code:
    ...
     public HashMap qValues = new HashMap(10000,(float)0.75);
    ...
    qValues.put(p.hashCode(), new Float (v));
    ...

    p is a class of two different strings (Pair state/action). The hash code generated is the sum of the hash code of each different string
    v is the value i want to keep (Q value in Reinforcement Learning )

    Thanks for the reply:)

  4. #4
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default

    I've seen this question quite a few times and have never seen an easy fix for it. I with there was a .toString() function that had all of the values in it, but there aren't. So unless you find something we all haven't you are going to have to traverse the hashmap saving each value then read them back in and put them back into the hashmap for loading. It should be pretty trivial to program, but it will not be quick on large data. Goodluck.

  5. #5
    rrusso2007 is offline Member
    Join Date
    Feb 2011
    Posts
    1
    Rep Power
    0

    Default

    I tried looking this up and this seems to be the most popular thread. Although after reading everyones answers to this question I thought I might add that there is a 'tostring' function that saves all the data. A hashmap is serializable and so as long as your key and value object types are also serializable then if you want to save the entire thing to a file using java alone then you just an ObjectOutputStream.

    Here is a fully functional example:

    Java Code:
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    import java.util.HashMap;
    
    
    public class hashTest {
    	public static void main(String [] Args){
    		try {
    
    			HashMap<Integer, data> map = new HashMap<Integer, data>();
    			for(int i = 0; i < 20; i++){
    				map.put(i, new data("data" + i, i*10, i*100, i*1000));
    			}
    			
    			ObjectOutputStream obj = new ObjectOutputStream(new FileOutputStream("map.txt"));
    			obj.writeObject(map);
    			obj.close();
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    		
    		try {
    			ObjectInputStream obj = new ObjectInputStream(new FileInputStream("map.txt"));
    			HashMap<Integer, data> map = (HashMap<Integer, data>)obj.readObject();
    			for(data d : map.values())
    				System.out.println(d.toString());
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (ClassNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    	}
    }
    
    
    class data implements Serializable {
    	private static final long serialVersionUID = 1L;
    	
    	
    	String s;
    	int a,b,c;
    	
    	public data(String s, int a, int b, int c){
    		this.s = s;
    		this.a = a;
    		this.b = b;
    		this.c = c;
    	}
    	
    	public String toString(){
    		return s + " " + a + " " + b + " " + c;
    	}
    }

Similar Threads

  1. HashTable vs HashMap
    By bugger in forum New To Java
    Replies: 7
    Last Post: 01-06-2011, 03:15 PM
  2. HashMap with objects
    By otoro_java in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 03:28 PM
  3. HashMap visual example
    By jhetfield18 in forum Advanced Java
    Replies: 1
    Last Post: 12-12-2007, 07:45 PM
  4. Hashmap
    By dirtycash in forum New To Java
    Replies: 5
    Last Post: 12-03-2007, 02:58 AM
  5. what is hashmap
    By gabriel in forum New To Java
    Replies: 5
    Last Post: 08-03-2007, 01:23 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
  •