Java, searching within a list of objects?

后端 未结 6 1909
名媛妹妹
名媛妹妹 2020-12-09 10:49

I\'m a bit lost on the way to make this happen the fastest. I have a large list of objects that have basic variable attributes (with getters / setters) and I need to do a se

6条回答
  •  情深已故
    2020-12-09 11:41

    If the objects are immutable (or you at least know their names won't change) you could create an index using a HashMap.

    You would have to fill the Map and keep it updated.

    Map map = new HashMap();
    
    map.put(myObject.getName(), myObject);
    ... repeat for each object ...
    

    Then you can use map.get("Some name"); to do lookup using your index.

提交回复
热议问题