Java集合的接口和类层次结构图

1. Collection vs Collections

首先,“Collection”和“Collections”是两个不同的概念。正如你从下面结构图看到的,“Collection”是集合层次结构中的根接口,而“Collections”是一个类,它提供了一系列静态方法来操作集合。

CollectionVsCollections.jpeg

2. Collection层次结构

下图展示了Collection的类层次结构。

java-collection-hierarchy.jpeg

3.Map层次结构

 

以下是Map的类层次结构。

MapClassHierarchy-600x354.jpg

4.总结

collection-summary.png

5.代码示例

下面是一个简单的例子来说明一些集合类型:

List <String > a1 = new ArrayList <String >();
a1 . add ( "Program" );
a1 . add ( "Creek" );
a1 . add ( "Java" );
a1 . add ( "Java" );
System . out . println ( "ArrayList Elements" );
System . out . print ( "\t" + a1 + "\n" );

List <String > l1 = new LinkedList <String >();
l1 . add ( "Program" );
l1 . add ( "Creek" );
l1 . add ( "Java" );
l1 . add ( "Java" );
System . out . println ( "LinkedList Elements" );
System . out . print ( "\t" + l1 + "\n" );

Set <String > s1 = new HashSet <String >(); // or new TreeSet() will order the elements;
s1 . add ( "Program" );
s1 . add ( "Creek" );
s1 . add ( "Java" );
s1 . add ( "Java" );
s1 . add ( "tutorial" );
System . out . println ( "Set Elements" );
System . out . print ( "\t" + s1 + "\n" );

Map <String , String > m1 = new HashMap <String , String >(); // or new TreeMap() will order based on keys
m1 . put ( "Windows" , "2000" );
m1 . put ( "Windows" , "XP" );
m1 . put ( "Language" , "Java" );
m1 . put ( "Website" , "programcreek.com" );
System . out . println ( "Map Elements" );
System . out . print ( "\t" + m1 );

输出:

ArrayList Elements
[Program , Creek , Java , Java ]
LinkedList Elements
[Program , Creek , Java , Java ]
Set Elements
[tutorial , Creek , Program , Java ]
Map Elements
{Windows =XP , Website =programcreek . com , Language =Java }

猜你喜欢

转载自blog.csdn.net/qq_33873431/article/details/80347497