简体   繁体   English

如何在Java中将对象的ArrayList转换为Integer列表

[英]How to convert a ArrayList of Objects into List of Integer in java

I used this native query to fetch data. 我使用此本地查询来获取数据。 select c.id,c.name from customer c,information i where i.status=1 and c.id=i.customerId and i used the below code to convert the object list into integer list as select c.id,c.name from customer c,information i where i.status=1 and c.id=i.customerId并且我使用以下代码将对象列表转换为整数列表,如下所示:

public List<Integer> getAllIdsByGroupId(Integer groupId) throws Exception {
    List<Object[]> list = repository.getAllIdsByGroupId(groupId);
    List<Integer> ids = repository.mapIds(list);
    return ids;
}


public List<Object[]> getAllIdsByGroupId(Integer groupId) {  
    Query query = entityManager.createNativeQuery("select c.id,c.name from customer c,information i where i.status=1 and c.id=i.customerId and c.groupId=:groupId");
    query.setParameter("groupId", groupId);
    List<Object[]> list = query.getResultList();
    if (list == null || list.isEmpty()) {
        return null;
    }
    return list;
}


public List<Integer> mapIds(List<Object[]> list) {
    List<Integer> ids = new ArrayList<Integer>();
    if (list != null && !list.isEmpty()) {
        Integer id;
        for (Object[] object : list) {
            id = (object[0] != null ? (Integer) object[0] : null);
            ids.add(id);
        }
    }
    return ids;
}

The query retrieves a list with id and name.and the above code gives output without exception.But i require List with only ids. 该查询检索具有ID和名称的列表。上面的代码无例外地给出了输出。但是我要求仅具有ID的列表。 when i remove c.name from query as follows select c.id from customer c,information i where i.status=1 and c.id=i.customerId the above code fails and produce the exception 当我按如下方式从查询中删除c.name时, select c.id from customer c,information i where i.status=1 and c.id=i.customerId上面的代码失败并产生异常

java.lang.Integer cannot be cast to [Ljava.lang.Object;

Any helps? 有帮助吗? thanks in advance. 提前致谢。

If you're using JPA / Hibernate for fetching single column data then it returns List<Integer> implicitly (based on type of that column), so no need to convert it to Integer list. 如果您使用JPA / Hibernate来获取单列数据,则它隐式返回List<Integer> (基于该列的类型),因此无需将其转换为Integer列表。

General rule: 一般规则:

  • If you are selecting an entity, then result is list of that entity typed items 如果选择一个实体,则结果为该实体键入的项目的列表
  • If you are selecting primitive, then result is list of that primitive wrapper typed items 如果选择原始类型,则结果是该原始包装器键入的项目的列表
  • If you are selecting multiple expressions (id, name), then result is Object[] containing the corresponding primitives/entities 如果选择多个表达式(id,name),则结果为Object []包含相应的图元/实体

The issue is in the typecasting Change List<Object[]> list = query.getResultList(); 问题出在类型转换Change List<Object[]> list = query.getResultList();

to List<Object> list = query.getResultList() List<Object> list = query.getResultList()

It is list of object where as your code is trying to convert and integer into List of Array. 它是您的代码尝试转换的对象列表,并将整数转换为数组列表。

List<Integer> list = query.getResultList() will also suffice if you are just trying to get ids 如果您只是尝试获取ID,则List<Integer> list = query.getResultList()也将足够

try this one may not try in my editor but may i thing one of this solution help you. 试试这个可能不会在我的编辑器中尝试,但是我想这个解决方案之一可以帮助您。

public List<Integer> mapIds(List<Object[]>  list) {
    List<Integer> ids = new ArrayList<Integer>();
    if(list != null && !list.isEmpty()) {
        int id;
        for(Object[] object : list) {
            id = (int) (object[0] != null ?  object[0] : null);
            ids.add(id);
     }
 }
 return ids;
}

Although the question is lacking a lot of information, the most likely reason for the ClassCastException is that you're trying to cast objects that are not of type Integer to type Integer . 虽然问题是缺乏大量的信息,对最可能的原因ClassCastException是你想投未中键入对象Integer输入Integer I'm not sure what's in the list that you're passing into the method, but if the list does indeed contain some objects of type Integer , then what you could try is to check for that before converting element object[0] into Integer , 我不确定要传递给方法的列表中有什么,但是如果列表确实包含一些Integer类型的对象,那么您可以尝试在将element object[0]转换为Integer之前检查一下,

public List<Integer> mapIds(List<Object[]> list) {
    List<Integer> ids = new ArrayList<Integer>();
    if (list != null && !list.isEmpty()) {
        Integer id;
        for (Object[] object : list) {
            id = ((object[0] != null && object[0] instanceof Integer) ? (Integer) object[0] : null);
            ids.add(id);
            }
        }
    }
    return ids;
}

However, if the objects in list don't contain any Integer objects, then the only way to achieve what you're doing is to initialize an Integer object for each object[0] by somehow retrieving the int value in each. 但是,如果list的对象不包含任何Integer对象,则实现您正在执行的操作的唯一方法是通过某种方式检索每个int值来为每个object[0]初始化一个Integer对象。 Only you know how to do this. 只有您知道如何执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JAVA:如何将String ArrayList转换为Integer Arraylist? - JAVA: How to convert String ArrayList to Integer Arraylist? 转换ArrayList <ArrayList<Integer> &gt;列出 <List<Integer> &gt; - Convert ArrayList<ArrayList<Integer>> to List<List<Integer>> 如何转换可选<list<integer> &gt; 到可选<arraylist<integer> &gt; </arraylist<integer></list<integer> - How to convert Optional<List<Integer>> to Optional<ArrayList<Integer>> 如何转换列表<Integer>到java中的Integer[]? - How to convert List<Integer> to Integer[] in java? 如何在java中将ArrayList转换为String [],Arraylist包含VO对象 - How to convert ArrayList to String[] in java, Arraylist contains VO objects 如何转换ArrayList <ArrayList<String> &gt;列表 <List<String> &gt;在Java? - How to convert an ArrayList<ArrayList<String>> to List<List<String>> in Java? 带有ArrayList <List <Integer >>的Java OutOfMemoryError - Java OutOfMemoryError with ArrayList<List<Integer>> 如何将Scala List转换为Java ArrayList - How to convert Scala List to Java ArrayList 如何将 object 列表转换为 Java 中的 integer 列表? - How to convert a list of object to a list of integer in Java? 如何转换属性ArrayList <String> 或ArrayList <Integer> 或ArrayList <MYCLASS> 从JSON对象到Java对象? - How to convert an attribute ArrayList<String> or ArrayList<Integer> or ArrayList<MYCLASS> from a JSON object to a java object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM