1

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
2
  • 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. Commented Jun 29, 2015 at 0:53
  • @chengpohi, he says "I want to store the first 4 line into key of hashMap". Commented Jun 29, 2015 at 0:54

3 Answers 3

2

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 (e.g. more values than keys will cause an IndexOutOfBoundsException).

Sign up to request clarification or add additional context in comments.

5 Comments

This works for me! Yes this one won't handle situations. But my text file has exact keys and values. Thank you! Awesome explanation between the codes.
how about if there's another checkpointtwo and checkpoint three? but same logic. when line reads checkpoint two, the first lines under checkpointtwo is put to the key, and when the lines read checkpointthree under of it will be store to values
Yes, that would also work, but it's not really needed. The first list is required because you need to know the key at the time that you store a value, so it needs to be available already. Though having two lists and building the map from both might be a bit more readable.
How can I do that? for example i have like this test1 test2 test3 checkpointone get1 get2 get3 checkpointtwo test4 test5 test6 checkpointthree get4 get5 get6
Oh, I see what you mean. If you hit checkpointtwo, remove everything from the key list. You'll need to change your test for what a 'checkpoint' is to account for both checkpointone and checkpointthree. Honestly though, this is a pretty odd way of reading in multiple values. You'd be much better off having the input file be a list of comma separated key-value pairs, with one per line.
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.