我正在尝试将捆绑包名称及其最新版本存储在地图中。
下面是我newDirs
的资料ArrayList<Map<String, String>>()
,我应该从中获取捆绑包名称及其最新版本-
[{objectName=/storage/Model/Framework/1.0.0/, objectId=4fa042a5a56c861104fa05c246cf850522a2354ca223, objectType=DIRECTORY},
{objectName=/storage/Model/Framework/1.0.1/, objectId=4fa042a5a66c860d04fa056bbe1cf50522a14094ca3f, objectType=DIRECTORY}]
现在,从上面的列表中,我应该提取的最新版本Framework bundle
。因此,在上述情况下为1.0.1 version
,捆绑名称为Framework
。因此,在上述情况下,我的地图将存储Framework
为Key和1.0.1
捆绑软件的版本。
以下是我的代码-
final List<Map<String, String>> newDirs = new ArrayList<Map<String, String>>();
for(String ss : list) {
//some code here for newDirs
Map<String, String> map = storageDirectorySort(newDirs);
System.out.println(map);
}
/**
* Sort the list and then return the map as the Bundle Name and its Latest version
*
*/
private static Map<String, String> storageDirectorySort(List<Map<String, String>> newDirs) {
Map<String, String> map = new LinkedHashMap<String, String>();
// do the sorting here and always give back the latest version of the bundle and its name
return map;
}
谁能帮我这个忙。我不确定执行此操作的最佳方法是什么?
您需要另一种帮助方法来帮助解析版本号。然后在您的storageDirectorySort方法中调用它:
private static int getVersion(Map<String, String> dir) {
String objectName = dir.get("objectName");
// Get the various parts of the name
String[] nameParts = objectName.split("/");
// Get the version from the nameParts
String[] versionString = nameParts[nameParts.length - 1].split("\\.");
// Parse version String into an int
return (Integer.valueOf(versionString[0]) * 1000000)
+ (Integer.valueOf(versionString[1]) * 10000)
+ (Integer.valueOf(versionString[2]) * 100);
}
private static Map<String, String> storageDirectorySort(
List<Map<String, String>> newDirs) {
Map<String, String> latestVersion = null;
for (Map<String, String> bundle : newDirs) {
int version = getVersion(bundle);
if (latestVersion == null || version > getVersion(latestVersion)) {
latestVersion = bundle;
}
}
return latestVersion;
}
注意:此代码中没有异常处理,建议添加一些。除此之外,我已经对其进行了测试以验证其是否有效。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句