Java常用的集合类型有
List
、
Set
、
Map
,容器一般也有线程安全与非线程安全之分,
java.util
包下的线程安全类一般都是废弃不用的,应该使用
concurrent
包下面的线程安全类。
1.1.
容器的一般使用
A、
声明容器时要指定泛型的类型,可避免编译器警告和强类型转换。
B、
如果一个方法的返回一个容器,不要返回null
,应该返回空的容器以避免空指针异常。
Collections
工具类定义了一些静态的字段:EMPTY_LIST
、
EMPTY_MAP
、
EMPTY_SET
,分别表示不可变的空的
list
、
map
、
set,可使用这些静态字段,而不是自己实例化一个。
C、
声明容器类型时尽量使用接口:List
、
Map
、
Set
,而不是具体的实现类。
这样可以方便更改具体的实现类,特别是方法的参数类型和返回类型,如果使用具体的实现类,则几乎是不可能进行更改的,因为每个调用的地方都需要进行修改。如果使用接口,则只需在方法内部或方法调用的地方更改就行了,把改动的范围最小化。所谓接口隔离原则也就是这个意思。
D、
如果容器接受初始容量,都应该指定一个尽可能准确的初始容量,特别是容器是以数组作为底层存储结构的。
1.2.
Collections工具类
Collections工具类提供了很多使用的方法。可完成的功能有:查找、拷贝、排序、查找最大最小元素、反转列表、轮换列表元素、将容器封装为线程安全的或不可变的。务必要记住这个类,对容器进行操作时,现在
API
里查找此类是否已经有满足要求的方法。
1.3.
集合的交、差、并集
Collection接口定义了
retainAll
、
removeAll
、
addAll
方法用于生成两个集合的交、差、并集。
1.4.
数组与列表的互转换
数组到列表的转换可以使用:Arrays.asList
方法。
列表到数组的转换可以直接调用:toArray
方法。
public class InterfaceSep {
public static void main(String[] args) {
List<String> source = Arrays.asList("123", "456", "789", "abc");
List<String> list = new LinkedList<String>(Arrays.asList("789", "xyz"));
list.retainAll(source);
System.out.println("retainAll :" + list); // 交集
list.addAll(source); // 并集
System.out.println("" + list);
list.add("rest");
list.removeAll(source); // 差集
System.out.println(list);
ArrayList<String> arrayList = new ArrayList<String>(source);
LinkedList<String> linkedList = new LinkedList<String>(source);
concrete(arrayList);
// concrete(linkedList); // compile error
abstrac(arrayList);
abstrac(linkedList);
}
public static void concrete(ArrayList<String> list) {
for (String string : list) {
System.out.println(string);
}
}
public static void abstrac(List<String> list) {
for (String string : list) {
System.out.println(string);
}
}
}
1.5.
Map
JDK里
Map
是一种
key
、
value
的映射结构,提供根据
key
快速查找
value
的能力,一般以
Map.Entry
的结构存储。常用的
Map
实现有
HashMap
、
LinkedHashMap
。
HashMap的底层存储结构是用数组(槽)
+
链表的形式,存储时根据
key
的值映射到数组的某个位置(槽位),如果有多个
key
映射到相同的槽位,则这些元素以链表的形式存放。
LinkedHashMap是采用双向链表,具有可预知的迭代顺序,一般就是插入顺序。
使用:
A、
在需要特定的迭代顺序,使用LinkedHashMap
;否则使用
HashMap
;
B、
迭代Map
时,如果通过
keySet
时,会有一个根据
key
查找
value
的开销。应该使用
entrySet
。
C、
使用Map
时,应该显式指定
key
、
value
的类型,避免强类型转换。
D、
使用HashMap
,因为它底层使用了数组,如果可能,应该尽量给数组预分配合适的大小。
Map<String, Integer> map = new HashMap<String, Integer>(8);
for (Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + "->" + entry.getValue());
}
for (String key : map.keySet()) {
Integer value = map.get(key);
System.out.println(key + "->" + value);
}
对于容器这种标准的类库,要熟悉其API
,避免自己蹩脚地实现已有的功能。
分享到:
相关推荐
useEnglish 资源E nglish Phrasal Verb & Idiom
四字成语mysql数据30810条
好的翻译软件,能用于有效的翻译,是一个非常不错的软件。本人经过测试,完全好用。
iom` (`id`, `guid`, `idiom`, `full_pinyin`, `pinyin`, `az`, `description`, `source`, `idiom_traditional`, `spinyin`, `idiom_phonetic`, `idiom_correct_pronunciation`, `idiom_distinguish`, `idiom_...
成语Idiom 是 AngularJS 的新翻译系统,它允许您在当前范围内阅读以提供基于上下文的翻译 - 没有更多的东西,他/她,您实际上知道您在写什么。 #用法向您的项目添加一个文件夹,其中包含每种语言的一个 json 文件...
Idiom WorldServer Desktop Workbench 8.1.0.208 使用指南
This is a common I/O programming idiom. while((bytes_read = from.read(buffer)) != -1) // Read until EOF to.write(buffer, 0, bytes_read); // write } // Always close the streams, even if exceptions...
AI-Algorithm-Idiom-in-Prolog-Lisp-Java 《Prolog、Lisp 和 Java 中的 AI、Alogirhtm、数据结构和习语》一书的代码
猜成语小程序数据库表ims_yf_chengyu_idiom
实现类 接口和实现的分离 采用pimpl idiom方式
java 8 lambda Concise syntax – More succinct and clear than anonymous inner classes • Deficiencies with anonymous inner classes – Bulky, confusion re “this” and naming in general, no nonfinal var...
资源来自pypi官网。 资源全名:china_idiom-0.0.2-py3-none-any.whl
Emotional Classification of Chinese Idioms Based on Chinese Idiom Knowledge Base
自然语言处理相关的分词数据
语言:English idiom有助于您阅读外语网站。 点击扩展,然后选择任何单词以说话和翻译。 阅读外国新闻,文学等。
英语习语idiomPPT课件.pptx
American Idioms and Some Phrases Just For Fun
Your first Java program ....... 52 Compiling and running ............... 54 Comments and embedded documentation ..................... 55 Comment documentation ............ 55 Syntax .....................
Chinese language idiom is one of the most distinctive categories, which is the focus of the senior stage of the foreign students. This paper conducts a comprehensive investigation based on idiom in ...