Java容器

本文深入探讨了Java CollectionFramework中的Collections类和Map的基本实现,包括如何使用Collections类进行集合操作,以及标准类库中包含的几种Map基本实现如HashMap、TreeMap等。文章通过实例演示了Collections类的nCopies和fill方法的应用,以及如何在实际编程中灵活运用Map进行数据存储和检索。

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

1. 填充容器

Collection Framework中有2个工具类,分别是Collections和Arrays类,这2个类有很多静态方法,可以完成集合的操作。

Collections 类:
static <T> List<T> nCopies(int n, T o):
Returns an immutable list consisting of n copies of the specified object

static <T> void fill(List<? super T> list, T obj)

Replaces all of the elements of the specified list with the specified element.

ArrayList<Airtest> list = new ArrayList(Collections.nCopies(4, new Airtest("airtest")));
System.out.println(list);
Collections.fill(list, new Airtest("testair"));
System.out.println(list);



1. Map:

标准java类库中包含Map的几种基本实现,包括:HashMap,TreeMap,LinkedHashMap,WeakHashMap,ConcurrentHashMap,IdentityHashMap。
HashMap使用散列码来进行搜索。散列码是相对唯一的、用以代表对象的int值,通过将该对象的某些信息进行转换而生成。hashCode()是根类Object中的方法,所有java对象都可以产生散列码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值