比较两个arraylists并从那里删除额外的元素

时间:2013-02-04 03:07:36

标签: android arraylist

如何比较两个arraylists?

我有一个sdCoverList数组和thumbnailList数组。

02-04 11:05:05.210: I/System.out(4035): HASHED THUMBNAIL[-2122410790, 2043473787, 1914391068, 1785308349, 1656225630, 1527142911, 1398060192, 1268977473, 1139894754, 1010812035, 1242943301, 1113860582, 984777863, 855695144, 726612425, 597529706, 468446987, 339364268, 210281549]
02-04 11:05:05.210: I/System.out(4035): SD COVER LIST[-2122410790, 2043473787, 1914391068, 1785308349, 1268977473, 1656225630, 1527142911, 1139894754, 1398060192, 1010812035, 1242943301, 1113860582, 984777863, 855695144, 726612425, 597529706, 468446987, 339364268, 210281549, 717409028]

在我的sd封面列表中,我有一个额外的值,我想删除,但首先我必须先与thumbnailList数组进行比较。如果两者都有相同的元素,不要做任何事情,除非有额外的,请将其从SD卡中删除。

我的数组列出

对于sdCoverList:

// CHECK DIRECTORY BEFORE DELETING THEM
            File strPath = new File(Environment.getExternalStorageDirectory() + folderName+"/Covers");
            File yourDir = new File(strPath, "");
            if(yourDir.length()!=0)
            for (File f : yourDir.listFiles()) {
                if (f.isFile())
                {
                    String name = f.getName();
                    sdCoverList.add(name);
                    //System.out.println(sdCoverList);
                }

for hashedthumbnail

for (int a=0;a<=thumbnailList.size()-1;a++)
        {
            String hashed = String.valueOf(thumbnailList.get(a).hashCode());
            Log.d("hashed",hashed);
            hashedThumbnail.add(hashed);
        }

1 个答案:

答案 0 :(得分:8)

List<String> sdCoverList = new ArrayList<String>();
sdCoverList.add("-2122410790");
sdCoverList.add("2043473787");
sdCoverList.add("717409028");

List<String> hashedThumbnail = new ArrayList<String>();
hashedThumbnail.add("-2122410790");
hashedThumbnail.add("2043473787");

System.out.println("sdCover List: " + sdCoverList);
System.out.println("hashedThumbnail List: " + hashedThumbnail);


List<String> temp = new ArrayList<String>();
temp.addAll(sdCoverList);
temp.removeAll(hashedThumbnail);

System.out.println("temp List: " + temp);

sdCoverList.removeAll(temp);

System.out.println("sdCover List: " + sdCoverList);

输出

sdCover List: [-2122410790, 2043473787, 717409028]
hashedThumbnail List: [-2122410790, 2043473787]
temp List: [717409028]
sdCover List: [-2122410790, 2043473787]