问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

vendor implementations

发布网友 发布时间:2022-08-11 20:17

我来回答

2个回答

热心网友 时间:2024-11-25 01:45

Convenience Implementations - Java Tutorial 5.0 英文版
The JavaTM Tutorial
Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Collections
Lesson: Implementations
Convenience Implementations
This section describes several mini-implementations that can be more convenient and more efficient than general-purpose implementations when you don't need their full power. All the implementations in this section are made available via static factory methods rather than public classes.
List View of an Array
The Arrays.asList method returns a List view of its array argument. Changes to the List write through to the array and vice versa. The size of the collection is that of the array and cannot be changed. If the add or the remove method is called on the List, an UnsupportedOperationException will result.
The normal use of this implementation is as a bridge between array-based and collection-based APIs. It allows you to pass an array to a method expecting a Collection or a List. However, this implementation also has another use. If you need a fixed-size List, it's more efficient than any general-purpose List implementation. This is the idiom.

List<String> list = Arrays.asList(new String[size]);

Note that a reference to the backing array is not retained.
Immutable Multiple-Copy List
Occasionally you'll need an immutable List consisting of multiple copies of the same element. The Collections.nCopies method returns such a list. This implementation has two main uses. The first is to initialize a newly created List; for example, suppose you want an ArrayList initially consisting of 1,000 null elements. The following incantation does the trick.
List<Type> list =
new ArrayList<Type>(Collections.nCopies(1000, (Type)null);

Of course, the initial value of each element need not be null. The second main use is to grow an existing List. For example, suppose you want to add 69 copies of the string "fruit bat" to the end of a List<String>. It's not clear why you'd want to do such a thing, but let's just suppose you did. The following is how you'd do it.
lovablePets.addAll(Collections.nCopies(69, "fruit bat"));

By using the form of addAll that takes both an index and a Collection, you can add the new elements to the middle of a List instead of to the end of it.
Immutable Singleton Set
Sometimes you'll need an immutable singleton Set, which consists of a single, specified element. The Collections.singleton method returns such a Set. One use of this implementation is to remove all occurrences of a specified element from a Collection.
c.removeAll(Collections.singleton(e));

A related idiom removes all elements that map to a specified value from a Map. For example, suppose you have a Map — job — that maps people to their line of work and suppose you want to eliminate all the lawyers. The following one-liner will do the deed.
job.values().removeAll(Collections.singleton(LAWYER));

One more use of this implementation is to provide a single input value to a method that is written to accept a collection of values.
Empty Set, List, and Map Constants
The Collections class provides methods to return the empty set, list, and map — emptySet, emptyList, and emptyMap. The main use of these constants is as input to methods that take a Collection of values when you don't want to provide any values at all, as in this example.
tourist.declarePurchases(Collections.emptySet());

热心网友 时间:2024-11-25 01:45

多指计算机的网络协议安装
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
Linux系统安装FTP服务器 Linux系统的网络文件共享 建筑的七盏明灯的内容简介 面向对象设计七大原则 简单说 交互设计七大定律 交互设计的“根”——七大定律 交互设计原则和理论2——七大定律 七大设计原则 附近的加油站有哪些 附近的加油站有哪些地方 安卓 用re管理器删除了一个系统文件 但是重启它还是会恢复 immutablemap.of里面只能放5个吗 君,鱼,大这三个字同时能加什么同样的部首变成另一个字? 问一个字 怎么读的 一个鱼加一个君 这个字怎么拼 一个鱼字家个君是什么字 左边一个鱼字右边一个君字念 ...里协助老师捐款、发言稿怎么写?我是第一次参加家委会。 比亚迪E9这款车适合多大年纪的人开?年轻人会选择购入吗? 成都哪里有定制皮鞋和西装的店? 姐妹们,Libresse薇尔卫生巾好用吗? Libresse薇尔卫生巾适合 敏感肌 用吗? BTV生活频道 健康生活栏目的联系方式是什么 5、钟楼读书仿写作文 北京BTV-7生活面对面栏目的咨询热线是多少?急急急 我18岁,我脸都黄了有半年了。而且眼睛里还带有黄斑,请问这是怎么回事... 四川省考申论职位性质是综合管理(县乡级)考ABC哪套卷子 09年宝马318有哪几种颜色 品颜MIRROR Pro智能化妆镜相比AMIRO,哪种续航能力更强? 最近两年脚上和手上长一种小疙瘩硬的,用指甲剪剪掉它又会长出来,怎么办... 推荐一些比较有哲理的小句子(可追加分) immutablelist在哪个包里 去留肝胆相照两昆仑,上一句是什么啊! ...嗯国破山河在什么什么恨别鸟惊心去留肝胆两仑昆的上一? 在终结之谷那战时,佐助左眼与右眼各有几个勾玉? 护士到家服务的护士是不是在职护士? ...古典讽刺小说《小兵张嘎》,作者用 的手法把严监生 的形象刻画... 老人行动不便医护到家护士上门可靠吗? 严监生是我国古典讽刺小说《》中的一个人物,作者用()的写法? 表现了严监生的什么特点?(句子体会) 医生上门服务医院会处罚吗 医护到家护士兼职违法吗 中国下半年货币政策的方向目标正式确立,下半年“钱”会流向哪里? 广晟有色东方财富最新资金流向 金边虎皮兰的叶子枯萎了怎么办 金边虎尾兰的叶子为何会枯黄? scala是世界上最难的语言么 我家金边虎皮兰放在凉台上,叶子由绿色变成浅黄色,是什么原因?如何养护... 金边虎皮兰颜色发黄,变成黄绿色,然后叶子变软,倾斜。根有点开始腐烂,到... Scala中调用函数或者方法为什么有时候需要括号,有时候不需要括号_百 ... 全自动洗衣机什么牌子好?2016热门品牌推荐