1

I'm really stucked on this problem. I'm not sure if this logic is possible, is there any other whay to achieve what I want?

I'm creating a HashMap like this.

List<String> data1 = new ArrayList();
data1.add("valid1");
data1.add("valid2");

List<String> data2 = new ArrayList();
data2.add("valid3");
data2.add("valid4");

Map<String,ArrayList> hashList = new HashMap();
hashList.put("one",data1);
hashList.put("two",data2);

So the Output will be like this:

{one=[valid1,valid2], two=[valid3,valid4]}

But what am I doing is, i'm reading a file and compare it to hashmap

Code:

String line;
String[] token;
try {
    LineIterator it = FileUtils.lineIterator(file,"UTF-8");

    while(it.hasNext()){

    line = it.nextLine();

    token = StringUtils.split(line,(","));

    if(token[1].equalsIgnoreCase( //check if its equal to the value of the hashmap){
    System.out.println("Valid");
        }
    }
}

my file looks like this :

test1,valid1,check1
test2,valid3,check2

So what I want to do is, to check if the "token[1]" is valid to the value of hashmap.

Thank you in advance!

3
  • "is valid to the value of hashmap ... which value? Do you wan't to check each value of every key? Btw: Map<String,ArrayList> You might want to avoid the raw list type. Commented Jun 24, 2015 at 22:06
  • Just to clarify: Are you trying to check if token[1] is equal to any of the items in any of the ArrayLists? Commented Jun 24, 2015 at 22:06
  • Yes, I want to check if token[1] is equal to the value in every key in HashMap. If the token[1] is equal to the value in HashMap then its true, and I want to store it to String from the value in HashMap but it's arrayList Commented Jun 24, 2015 at 23:28

3 Answers 3

1

Convert the map to list

List<String> list = new ArrayList<String>(map.values());

Then use

list.contains(token[1])

to check if its equal to the list content which is the value of the hashmap

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

2 Comments

But I want to store the token[1] value and equal value of that in HashMap. How can I do that? Because it's boolean / arraylist type I cant store it to String
You didnt mention that in your question so kindly close this question by accepting an answer and create a new one for the mentioned concern
0
ArrayList<String> check;
check = your_hash_map.get(token[1]);
if(check != null){System.out.println("Valid");}

1 Comment

token[1] is a value of an unknown key, not a key itself.
0

If you want to get the valid key for the token, the most expensive way of achieving your goal is iterating over the hash keys than iterating over the array value for that key and check if any string inside it matches your conditional.

It should looks like this:

String getValidKey(String token, Map<String, List<String>> hashList)  {
    for (String key: hashList.keySet()) {
        for (String valid: hashList.get(key)) {
            if (valid.equalsIgnoreCase(token)) {
                return key;
            }
        }
    }

    return null;
}

If you only need to know if the token is valid or not this should be enough:

boolean isValid(String token, Map<String, List<String>> hashList)  {
    for (List<String> list: hashList.values()) {
        for (String valid: list) {
            if (valid.equalsIgnoreCase(token)) {
                return true;
            }
        }
    }

    return false;
}

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.