2017-09-19 72 views
0

//使用Offer 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 
    } 

此對象用於映射服務器JSON響應,

+1

在嘗試尋找最佳方式之前,您是否至少嘗試過**方式?你爲什麼不尊重Java命名約定? –

+0

並且此offerId存在於哪裏? –

+0

'lOffers'應該是一個列表?你有沒有考慮使用其他數據結構? –

回答

0

您可以使用地圖保存ID爲重點,對象爲值,然後用列表,像這樣:

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); 
相關問題