我在java中有一个对象列表。每个对象应根据其属性值对与其相关的类别进行排序,结果也应进行排序。排序应基于配置文件中类别的位置。 我用json数组文件来定义结构
[
{"Risk assessment":["risk assessment","...","..."]},
{"Emergency plan":["emergency plan","..."]},
{"Emergency maps":["emergency map","..."]},
{"Evacuation test":["emergency test","..."]},
]
我想这样的操作很快就会在Java8中实现。 是否存在任何库来概括使用json文件来定义结构?
这是我的代码从json文件
应用结构 public static List<DocumentCategory> applyStructureToList(List<GISDocument> documents,String jsonFile) throws IOException{
categoryMap = readMap(documents,jsonFile);
List<DocumentCategory> documentCategories = new ArrayList<DocumentCategory>();
for(GISDocument gisDocument:documents ){
final String title = gisDocument.getTitle().toUpperCase();
mapLoop:
for(Map<String,List<String>> mapElement:categoryMap){
for(String key : mapElement.keySet()){
for(String value:mapElement.get(key)){
//document title must contain one of the values of the category defined in the jsonFile.
if(title.contains(value.toUpperCase())){
DocumentCategory dc = new DocumentCategory();
dc.setTitle(key);
// if a category already exists then add the current document to the items
// otherwise append the new category
if(documentCategories.contains(dc)){// the method contains compares title property
documentCategories.get(documentCategories.indexOf(dc)).getItems().add(gisDocument);
}else{
dc.getItems().add(gisDocument);
documentCategories.add(dc);
}
break mapLoop;
}
}
}
}
}
Collections.sort(documentCategories);
return documentCategories;
}