How to search in a List of Java object

前端 未结 5 1345
耶瑟儿~
耶瑟儿~ 2020-11-27 16:21

I have a List of object and the list is very big. The object is

class Sample {
    String value1;
    String value2;
    String value3;
    String value4;
          


        
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 17:00

    If you always search based on value3, you could store the objects in a Map:

    Map> map = new HashMap <>();
    

    You can then populate the map with key = value3 and value = list of Sample objects with that same value3 property.

    You can then query the map:

    List allSamplesWhereValue3IsDog = map.get("Dog");
    

    Note: if no 2 Sample instances can have the same value3, you can simply use a Map.

提交回复
热议问题