简体   繁体   中英

Java read text file store to HashMap

I'm just wondering if this logic is possible, what I want to do is to read text file line by line and store it to HashMap. I want to store the first 4 line into key of hashMap and when the lines read the checkpoint, the next line will be stored to value of HashMap.

File file1 = new File("C:\test\testfolder\test.txt");

HashMap<String,String> hashMap = new HashMap();
String check = "checkpointone";

try{
    LineIterator it = FileUtils.lineIterator(file1,"UTF-8");

    String line;

    while(it.hasNext()){
        line = it.nextLine();
        hashMap.put(line , null);

        if(line.contains(check)){
            //do the logic here
        }
    }
}
catch(Exception ex){
    ex.printStackTrace();
}

test.txt data :

test1
test2
test3
test4
checkpointone
get1
get2
get3
get4

Store the lines before the checkpoint in a list. After the checkpoint, insert each line into the hashmap, using each item in the list as the key.

...
boolean pastCheckpoint = false;
int keyIndex = 0;
// We will store keys in here
List<String> keys = new ArrayList<String>();

while(it.hasNext()){
    line = it.nextLine();

    if (line.contains(check)) {
        // We don't actually want to store this line,
        // so just set the flag 
        pastCheckpoint = true;
    }
    else if (pastCheckpoint) {
        // We must already have a key defined
        // Get that key and increment the counter for next time
        hashMap.put(keys.get(keyIndex++), line);
    }
    else {
        // We're not past the checkpoint so save this key for later
        keys.add(line);
    }
}
...

Note that this won't handle situations where the input file is malformed (eg more values than keys will cause an IndexOutOfBoundsException).

This logic is possible. However, Iterators and their subclasses do not have the contain() function.

It should be

if(hashMap.contains(check))

Then, you can break out of the loop once the checkpoint has been reached if that is the intention. Otherwise, you can let the loop proceed.

What @deyur suggested works perfectly fine and we can use a List to keep track of the order in which keys were added to the Map. But there is a data structure that supports such capability:

I assume you want your key values be like (test1,get1), (test2,get2), etc. Your code first puts (test1,null), (test2,null), etc into the HashMap. When you reach the checkpoint, you need to know what was the first entry that was added to the HashMap in order to set its value to the first line after checkpoint.Now, your problem is to retrieve the key values in the HashMap exactly in the same order as they were put in the Map and update their values. This is not possible in HashMap. Instead, you may use LinkedHashMap which supports such capability.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM