如何将List <object>转换为List <t>?

时间:2018-08-29 07:04:36

标签: java android list object generics

这是用于获取改造响应作为对象的代码。下面的方法很好用,但是我需要一个通用的函数来执行上面的功能,即类名可能会有所不同。 (例如)机票,价格,代币,约会,例如:

processGETRequest(AppController.getApiHelper().searchTickets(from, to), new RetrofitListener() {
    @Override
    public void onSuccess(Object object) { }

    @Override
    public void onSuccess(List<Object> object) {

        // Here I'm getting retrofit response as a object //

        if (object != null) {

            // Below method is working fine //

            List<Ticket> ticketList = new ArrayList<>();
            for (Object result : object) {
                String json = new Gson().toJson(result);
                Ticket model = new Gson().fromJson(json, Ticket.class);
                ticketList.add(model);
            }

            // I need an one common function for performing above functionality
            // i.e the Class name may vary.. (e.g) Ticket, Price, Token, Appointment like this.
        }
    }

    @Override
    public void onError(String error) {
        Log.d("error: ", " " + error);
    }
}, false);

RetrofitListener界面很简单:

public interface RetrofitListener {
    void onSuccess(Object object);
    void onSuccess(List<Object> object);
    void onError(String error);
}

4 个答案:

答案 0 :(得分:0)

<T> List<T> getList(Class<T> type, List<Object> object) {
    return object.stream()
            .map(result -> new Gson().toJson(result))
            .map(new Gson().fromJson(json, type))
            .collect(Collectors.toList());
}

List<Ticket> ticketList = getList(Ticket.class, object);

这将完成您的for循环。

答案 1 :(得分:0)

您可以转换代码段

if (object != null) { 
                        List<Ticket> ticketList = new ArrayList<>();
                        for (Object result : object) {
                            String json = new Gson().toJson(result);
                            Ticket model = new Gson().fromJson(json, Ticket.class);
                            ticketList.add(model);
                        }    
                    }

具有这样的泛型

<T> void check(Class<T> type, List<Object> object) {
        List<T> ticketList = new ArrayList<>();
        for (Object result : object) {
            String json = new Gson().toJson(result);
            T model = new Gson().fromJson(json, type);
            ticketList.add(model);
        }
    }

答案 2 :(得分:0)

您可以使用类似于以下内容的静态函数

static <T> List<T> toList(List<Object> object, Class<T> desiredClass) {
    List<T> transformedList = new ArrayList<>();
    if (object != null) {
      for (Object result : object) {
          String json = new Gson().toJson(result);
          T model = new Gson().fromJson(json, desiredClass);
          transformedList.add(model);
      }
    }
    return transformedList;
}

基本上,您只需要确保提供所需的类型(例如desiredClass)并在fromJson中使用它即可。

样品用量:

List<Ticket> ticketList = toList(object, Ticket.class);
List<Price> priceList = toList(object, Price.class);

请注意,通过将object != null移至toList方法中,您无需关心传递给该方法的内容。作为回报,您至少会得到一个空列表。

答案 3 :(得分:-1)

根据您的问题,您希望通用代码获取可以为Any的List?类型。

List<Ticket> ticketList = new ArrayList<>();
                    for (Object result : object) {
                        String json = new Gson().toJson(result);
                        Ticket model = new Gson().fromJson(json, Ticket.class);
                        ticketList.add(model);
                    }

这是你想要的吗?

List<Ticket> ticketList // can be List<T>??

然后创建一个通用方法来获取列表并在以下任何地方使用它:

public <T> List<T> getObjectToList(Object obj, Class<T[]> ObjectArryaClass) {
    String json = new Gson().toJson(obj);
    return Arrays.asList(new Gson().fromJson(json, ObjectArryaClass));
}

调用上述方法为:

@Override
    public void onSuccess(List<Object> object) {

        // Here I'm getting retrofit response as a object //

        if (object != null) {

            // Below method is working fine //

            List<Ticket> ticketList = getObjectToList(object,Ticket[].class)

            // I need an one common function for performing above functionality
            // i.e the Class name may vary.. (e.g) Ticket, Price, Token, Appointment like this.
        }
    }