【问题标题】:Search through Java collection using class attribute使用类属性搜索 Java 集合
【发布时间】:2013-01-16 01:30:07
【问题描述】:

我有一个这样的抽象类:

public abstract class Ingredient {
    protected int id;
}

还有一个列表

List <Ingredient> ingredientList = new ArrayList <Ingredient>()

我希望能够使用 id 从ingredientList 获取成分。

我做了这样的事情:

public abstract class Ingredient implements Comparable<Ingredient>{
        protected int id;
        @Override
    public int compareTo(Ingredient o) {
        // TODO Auto-generated method stub
        if (this.id > o.id){
            return 1;
        }
        return 0;
    }
    } 

还是不行

【问题讨论】:

  • 还有,是什么阻止你这样做?你的问题是什么?
  • 这样搜索的预期结果是什么?
  • 我过去使用我认为“比较器”或“可比较”的东西来做这件事,我现在正在寻找这样做但无法做到,它只是实现了一个“可比较”的接口并且正在做东西,我没听懂
  • 您可以实现 'comparable' 并使用 compareTo() 方法中的 id 比较实例。是这个意思吗?
  • 也许你应该用自然语言添加一些解释

标签: java search collections


【解决方案1】:

如果您需要执行常规查找,Map 可能是在这里使用的更好集合:

Map<Integer, Ingredient> ingredientMap = new HashMap<>();

【讨论】:

    【解决方案2】:
    for (Ingredient ingredient : IngredientList) {
      if (ingredient.getId() == id) {
        System.out.println("found");
      }
    }
    System.out.println("not found");
    

    【讨论】:

      【解决方案3】:

      如果你使用Eclipse Collections,你可以使用detect方法。

      final int idToFind = ...;
      ListIterable<Ingredient> ingredientList = FastList.newListWith(...);
      Ingredient ingredient = ingredientList.detect(new Predicate<Ingredient>()
      {
          public boolean accept(Ingredient eachIngredient)
          {
              return eachIngredient.getId() == idToFind;
          }
      });
      

      如果你不能改变成分列表的类型,你仍然可以使用静态工具形式的检测。

      Ingredient ingredient = ListIterate.detect(ingredientList, new Predicate<Ingredient>()
      {
          public boolean accept(Ingredient eachIngredient)
          {
              return eachIngredient.getId() == idToFind;
          }
      });
      

      当 Java 8 发布 lambdas 时,您将能够将代码缩短为:

      Ingredient ingredient = ingredientList.detect(eachIngredient -> eachIngredient.getId() == idToFind);
      

      注意:我是 Eclipse 集合的提交者。

      【讨论】:

        【解决方案4】:

        你可以做(​​在你的班级里)

        interface Callback { public void handle(Indredient found); }
        
        public void search(List<Ingredient> ingredientList, int id, Callback callback) { 
          for(Ingredient i : ingredientList) if(i.id == id) callback.handle(i)
        } 
        

        然后

        ingredients.search ( 10, new Callback() { 
               public void handle(Ingredient found) {
                    System.out.println(found);
               }
           });
        

        或类似的东西......

        ps:在你改变你的问题之前我已经回答了;)

        【讨论】:

        • 为什么要费心处理回调,而不是仅仅返回?
        • 返回什么?他没有具体说明,什么..全部或一个匹配
        【解决方案5】:

        只是一个猜测,但会不会是你的意思:

        Best way to use contains in an ArrayList in Java?

        list的contains()方法使用equals()和hashCode()

        【讨论】:

          【解决方案6】:

          当您使用 List.contains() 查找具有 id 的成分时,请覆盖 equals()hashCode() { return id};

          在 equals() 中:在相等时将 this.id 与 other.id 进行比较。

          【讨论】:

            【解决方案7】:
            public Ingredient getIngredientById(int id) {
               for (Ingredient ingredient : ingredientList) {
                  if (ingredient.id == id) {
                     return ingredient;
                  }
               }
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-01-25
              • 2023-03-31
              • 1970-01-01
              • 2013-09-13
              • 1970-01-01
              相关资源
              最近更新 更多