Java比较数组到ArrayList

时间:2015-06-12 00:15:37

标签: java arrays arraylist

我真的被困在这里。我只是想知道是否有另一种方法将数组与HashMap的键进行比较?请参阅我对我的代码的评论

          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;
                }

档案数据:

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

先谢谢你们!

2 个答案:

答案 0 :(得分:1)

我会使用StringUtils.join加入你的列表来创建一个单独的字符串,然后将它与每一行进行比较,而不是将文件分割成一个数组,并在列表中迭代这两个数组。

你可以使用这样的java代码:

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");
    }
}

答案 1 :(得分:0)

get()方法允许您从ArrayList中选择特定索引。 (See Javadoc

以下是将定义的ArrayList与输入数组进行比较的几种方法。如果其中一个满足您的需求,请告诉我们哪一个。 ;)

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);
                }
        }

    }

}