如何使用列表项中的属性查找arraylist项的索引?

时间:2017-09-19 06:36:01

标签: java apache-commons android-guava

//使用优惠ID找出特定事件索引的最佳方法是什么?         使用核心java或使用第三方库(Guava / Commons Collection)?

  public class Event{
        private String title;
        //other attributes
        private ArrayList<Offers> lOffers;
    }

    public class Offers{
        private Sring Id;
        private String offerName;
    }


    //Code
    List<PayPerViewTitles>  PayPerViewTitleList = someMethod();

/*

offerId -> Offers Object id property
**/
    private int findEventPositionByOfferId(List<Event> eventList, String offerId){
       // how to implement this method to find the Main Event using the 
       offer->id
    }

此Object用于映射服务器JSON Response,

1 个答案:

答案 0 :(得分:0)

您可以使用地图将id保存为键,将对象保存为值,然后使用list,如下所示:

List<Student> students = new ArrayList<>();
    Student student0 = Student.builder().id(1).name("wentjiang").age(23).build();
    Student student1 = Student.builder().id(2).name("wentjiang2").age(232).build();

    Map<Integer,Student> map = new HashMap<>();
    map.put(student0.getId(),student0);
    map.put(student1.getId(),student1);
    students.add(student0);
    students.add(student1);
    Integer findId = 2;
    Student student = map.get(findId);
    int index = students.indexOf(student);
    System.out.println(index);