1

I'm really stuck over here. I'm just wondering if there's another way to compare the array to key of HashMap? Please see my comments on my code

          ArrayList<String> datalist = new ArrayList();
                datalist.add("test,test2,test3,test4");
                datalist.add("test2,test,test3,test4");
                datalist.add("test3,test4,test,test2");

         Map<String,String> hashdata = new HashMap();
                        hashdata= setHashMap(datalist);
     Set data_key = hashdata.keySet(); // so i get now the all Key of Hashmap

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

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

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

                    //I have this code compare line splitted by ',' compare to key 
                    if(!(data_key.contains(data[0]))){
System.out.println("Invalid");


                    //But what I want is to compare KeySet to line splitted by ','
                    if(//statement here){

                    }

                }

            private static Map<String,String> setHashMap(ArrayList<String> inputlist){
                   String[] data;
                   Map<String,String> hashmap = new HashMap<String,String>();
                   for(int i=0; i<inputlist.size(); i++){
                       data = inputlist.get(i).split(",");
                       hashmap.put(data[0], data[1]);
                   }
                   return hashmap;
                }

Data on file :

test1,test2,test3
test2,test3,test1
test3,test2,test1

Thank you in advance guys!

11
  • what do you mean "compare the elements of arraylist to line". Do you mean to see if any item in datalist matches any of the tokens in the input line, not just data[0]? If you want to see if any of the items in datalist are found in each line read in, then for each line read, Commented Jun 12, 2015 at 0:20
  • oops...continuing...check line.contains(<current item in iteration of list>) Commented Jun 12, 2015 at 0:27
  • What's wrong with the code you have now? Commented Jun 12, 2015 at 0:32
  • That's what your code is doing now. Commented Jun 12, 2015 at 0:35
  • 2
    It would be really, really helpful if you provided not only example input, but the corresponding desired results. Commented Jun 12, 2015 at 0:52

2 Answers 2

1

I would join your list using StringUtils.join to create one single string, and the compare it to each line, rather thansplitting the file into an array and iterate both the array against the list.

You can use a java code like this:

ArrayList<String> datalist = new ArrayList<String>();
datalist.add("test");
datalist.add("test2");
datalist.add("test3");

String line;
String data = StringUtils.join(datalist, ',');

LineIterator it = FileUtils.lineIterator(file,"UTF-8");

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

    if (data.equals(line)) {
        System.out.println("Valid");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

The get() method will allow you to pick a specific index from an ArrayList. (See Javadoc)

Here are a few ways to compare your defined ArrayList with an input array. If one of theses satisfies your needs, please let us know which one. ;)

public class temp {
    ArrayList<String> datalist = new ArrayList();
    datalist.add("test");
    datalist.add("test2");
    datalist.add("test3");

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

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

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

        //I have this code compare line to elements of arraylist
        if(datalist.contains(data[0]){
            System.out.println("Valid A");
        }

        // Test specific element of array list against a specific index in the file
        if(data[0] == datalist.get(0)){
            System.out.println("Valid B");
        }

        //test specific element in array list against each element in line
        for(int i = 0; i < data.length; i++)
            if(data[i] == datalist.get(0)){
                System.out.println("Valid C for index " + i);
            }

        //test if array list contains any element in line
        for (String element : datalist) {
            for (int i = 0; i < data.length; i++)
                if (datalist.contains(data[0])){
                    System.out.println("Valid D for index " + i);
                }
        }

    }

}

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.