【问题标题】:Filtering out specific objects from a search query in Alfresco using Java使用 Java 从 Alfresco 中的搜索查询中过滤掉特定对象
【发布时间】:2010-03-31 01:45:06
【问题描述】:

我有一个 HashSet,其中包含我从数据库中检索到的所有组。我被要求通过删除两个特定组来过滤此结果。这似乎微不足道,但我似乎无法想出一个可靠的解决方案来存储我想要过滤掉的特定组。

我的想法是创建一个数组,其中包含对我需要过滤掉的两个组的引用。然后我可以用数组中的任何内容过滤掉我的搜索查询。我担心的是,将来他们可能会要求过滤掉更多的组,也许数组可能不是一个好主意。

//Creates the array containing groups to filter out

String[] hiddenGroups = {"group1","group2"};
//retrieves all groups
Set<String>allGroups = new HashSet<String>();
allGroups.addAll(authorityService.getAllAuthorities(AuthorityType.GROUP);
List<String>results = new ArrayList<String>();

//filters out specified groups 
for (String group : allGroups) {
  boolean isHidden = false;
  for (String hiddenGroup : hiddenGroups) {
    if (hiddenGroup.equalsIgnorecase(group)) {
      isHidden = true;
    }
  }
  if (!isHidden){
    results.add(group);
  }
}

【问题讨论】:

    标签: java filter alfresco


    【解决方案1】:

    在 HashSet 中查找元素可以在恒定时间内完成。因此,您可以通过不循环遍历 HashSet 中的元素,而是从完整集合中工作并在发现字符串包含在完整集合中时删除字符串来提高代码效率。

    //Creates the array containing groups to filter out
    
    String[] hiddenGroups = {"group1","group2"};
    
    //retrieves all groups
    Set<String>allGroups = new HashSet<String>();
    allGroups.addAll(authorityService.getAllAuthorities(AuthorityType.GROUP);
    Set<String>results = allGroups.clone();
    
    //filters out specified groups 
    for (String group : hiddenGroups) {
      if (allGroups.contains(group)) {
        results.remove(group);
      }
    }
    

    即使有大量组,这也会很快,因为每个组都是在恒定时间内查找的。​​p>

    【讨论】:

    • HI Bialecki,感谢您帮助我优化我的代码。我不知道 Hashset 有这些能力。然而,我想补充的一件事是,使用我上面提出的 for 循环会产生并发修改异常。为了解决这个问题,我刚刚创建了一个迭代器并用它来遍历集合。
    猜你喜欢
    • 2010-09-28
    • 2017-11-10
    • 2017-09-06
    • 2019-09-05
    • 1970-01-01
    • 2022-11-17
    • 2016-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多