有没有一种优雅的方式可以从哈希表中删除键不在给定项目列表中的项?如果有人能提供代码片段,我将不胜感激。如果没有,我可能会像这样做:
public HashMap<Integer, NameAndID> getTasksWithWordInFormula(Session session,
HashMap<Integer, NameAndID> taskMap, int sectionID, int topicID, int wordID) {
@SuppressWarnings("unchecked")
List<Integer> goodList = session.createCriteria(Frbw.class)
.add(Restrictions.in("id.formulaId", taskMap.keySet()))
.add(Restrictions.eq("sectionId", sectionID))
.add(Restrictions.eq("topicId", topicID))
.add(Restrictions.eq("wordId", wordID))
.setProjection(Projections.projectionList()
.add(Projections.property("id.formulaId")))
.setCacheable(true).setCacheRegion("query.DBParadox").list();
ArrayList<Integer> toRemove = new ArrayList<Integer>();
for (Integer formulaID : taskMap.keySet())
if (!goodList.contains(formulaID))
toRemove.add(formulaID);
for (Integer formulaID : toRemove)
taskMap.remove(formulaID);
return taskMap;
}
values()
而不是keySet()
有效吗? - Populus