将List <t>转换为我自己的对象

时间:2017-05-20 17:47:43

标签: java casting

我有一个名为getBillingCodes的方法,它返回List个名为BillingCodes的对象。

@Service
public class BillingCodeService {

    private static final Logger log = LoggerFactory.getLogger(BillingCodeService.class);

    String sql;

    @Autowired
    private BillingCodeRowMapper billingCodeRowMapper;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<BillingCode> getBillingCodes(int billingCodeId, int limit) {

        sql = [SQL_STATEMENT]";

    List<BillingCode> billingCodeList;

    billingCodeList = jdbcTemplate.query(sql, new Object[]{billingCodeId, limit}, billingCodeRowMapper);

        return billingCodeList;
    }
}

我想更改getBillingCodes方法,以便它仍然返回List个对象,但我想在List中指定class个对象,就像这样

@Component
public class BillingCodeList {

    private List<BillingCode> billingCodeList;

    Getter and Setters removed,,.       
}

然后将getBillingCodes方法的更改更改为:

@Service
public class BillingCodeService {

    private static final Logger log = LoggerFactory.getLogger(BillingCodeService.class);

    String sql;

    @Autowired
    private BillingCodeRowMapper billingCodeRowMapper;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private BillingCodeList billingCodeList;

    public BillingCodeList getBillingCodes(int billingCodeId, int limit) {

        sql = "[SQL_STATEMENT]";

        billingCodeList = jdbcTemplate.query(sql, new Object[]{billingCodeId, limit}, billingCodeRowMapper);

        return billingCodeList;
    }
}

设置billingCodeList = jdbcTemplate.query,,.会给我一个错误no instance(s) of variable(s) T exist so that List<T> conforms to BillingCodeList。因此,我试图像这样将List<T>投射到BillingCodeList

billingCodeList = (BillingCodeList)jdbcTemplate.query(sql, new Object[]{billingCodeId, limit}, billingCodeRowMapper);

但是这给了我一个投射错误java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.test.model.BillingCodeList。如何将java.util.ArrayList投射到com.test.model.BillingCodeList

4 个答案:

答案 0 :(得分:0)

你不能将List强制转换为类对象。你可以做的一件事就是你可以通过构造函数将BillingCodeList对象传递给BillingCodeRowMapper,并在创建BillingCode后填充BillingCodeList。

答案 1 :(得分:0)

你不想做你所要求的(实际上,你有一个XY问题时刻)。

为了解释,我看到BillingCodeList注释了@Component,我猜这意味着它是一个Spring组件 - 你需要做一些非常花哨的步法才能使这个工作(和它会像地狱一样低效。

我猜你不想每次重新读取所有的帐单代码,而是想要将它们缓存在内存中。在这种情况下,这样的事情会更好:

@Component
public class BillingCodeListCache {
    @Autowired
    private BillingCodeService billingCodeService;

    private List<BillingCode> billingCodeList;

    public List<BillingCode> getBillingCodes() {
       if(billingCodeList == null) {
           billingCodeList = billingCodeService.getBillingCodes();
       }
       return billingCodeList; // -- or a sublist.
    }
}

这样你也可以控制缓存驱逐。

答案 2 :(得分:0)

如果你这样做的唯一原因是有一个方法来打印出列表的值,那么只需扩展ArrayList或在你的类中实现List接口。然后你可以覆盖toString方法来做你想要的。

答案 3 :(得分:0)

我选择了Lew Bloch建议的解决方案。在List构造函数中将BillingCodes作为参数传递。

@Autowired
    private BillingCodes billingCodesToString(List<BillingCode> billingCodes) {
        return new BillingCodes(billingCodes);
    }

然后覆盖toString()课程中的BillingCodes方法,让我有能力获得String这样的内容:

billingCodesToString(billingCodes).toString()