HashMap <! - ?,Collection <? - >不更新

时间:2013-02-03 14:56:00

标签: android collections hashmap

在我正在开发的应用程序中,我需要能够保存与另一个(多个其他?)包名称相关联的列表。除了性能保存部分外,一切正常。以下是此过程的代码。

private boolean existsInApk(String entryName) {
    String fileStructure = entryName.replace(Constants.ASSETS + "/", "");
    String packageName = fileStructure.substring(0, fileStructure.indexOf("/"));
    String path = fileStructure.replace(packageName + "/", "");

    // For an instance which the package has not yet been checked.
    if (!mApkMap.containsKey(packageName)) {

        // Create a new Collection<String> to hold the values.
        Collection<String> files = new ArrayList<String>();

        try {
            String sourceDir = mContext.getPackageManager().getApplicationInfo(packageName, 0).sourceDir;

            ZipFile apk = new ZipFile(sourceDir);
            Enumeration<? extends ZipEntry> apkEntries = apk.entries();
            while (apkEntries.hasMoreElements()) {
                ZipEntry apkEntry = (ZipEntry) apkEntries.nextElement();
                String name = apkEntry.getName();
                if (!apkEntry.isDirectory()) {

                    // Add the name of the entry to the values list.
                    files.add(name);
                }
            }
        } catch (Exception e) {

        }

        // Save the list to the HashMap to be retreived if another item calls for the same package name
        mApkMap.put(packageName, files);
    } else {

        // The packageName has already been added to the HashMap so skip all the other stuff
        Log.e("", mApkMap.get(packageName) + "");
    }

    // Get the Collection from the hash map
    Collection<String> files = mApkMap.get(packageName);

    // Create a Collection using a formatted string
    Collection<String> item = new ArrayList<String>(Arrays.asList(path));
    files.retainAll(item);

    // Check to see if it exists in the package's file.
    if (files.size() > 0) {
        return true;
    }
    return false;
}

除了

返回的Collection之外,整个过程都有效
mApkMap.get(packageName);

始终是一个空数组。我已经检查确保项目确实已添加到列表

files.add(name);

如果我用HashMap删除部件,该功能运行完美,但速度非常慢。如果我错过任何事情让我知道,这是我真正需要正常工作的事情。

插入函数的示例:

com.android.systemui/res/drawable-hdpi/stat_sys_battery_100.png

0 个答案:

没有答案