list嵌套 list java,将嵌套的ArrayList转换为List Java

本文讨论了在Java中如何将嵌套的ArrayList转换为List。建议的方法是通过更改方法签名,使用通配符或者直接创建新的List实例。详细介绍了如何在不进行实际转换的情况下实现这一目标,同时强调了正确处理类型的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I have this method

public List> ConvertReportCases (ArrayList> stats)

It is an interface implementation where the return type is a List>. In general how do I convert from a nested ArrayList to List in Java?

Thanks

Edit

Thanks for the suggestions I could make my parameter passed as List but then it would be wrong since the caller will have to pass it as List, whereas in my particular case I could have different versions (polymorphism) to check this issue.

解决方案

Really, there is no converting needed.

If you can't touch the declaration of the list, you should change the method signature to this. It uses wildcards.

public List extends List> convertReportCases(List extends List> stats);

However, the most clean way would be to change the method signature to

public List> convertReportCases(List> stats);

And declare the list as

List> list = new ArrayList>();

for (List sublist : list) {

sublist = new ArrayList();

}

If you use List methods, you will be ok, there will be no converting needed. And since ArrayList is-a List and is practically only useful for its List methods, you should be ok!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值