Collections and Arrays in Java

Source: Internet
Author: User

数组Array和集合的区别:

(1)数组是大小固定的,并且同一个数组只能存放类型一样的数据(基本类型/引用类型) (2)JAVA集合可以存储和操作数目不固定的一组数据。 (3)若程序时不知道究竟需要多少对象,需要在空间不足时自动扩增容量,则需要使用容器类库,array不适用。 联系:使用相应的toArray()和Arrays.asList()方法可以回想转换。 一.集合的体系结构: List、Set、Map是这个集合体系中最主要的三个接口。 List和Set继承自Collection接口。 Map也属于集合系统,但和Collection接口不同。 Set不允许元素重复。HashSet和TreeSet是两个主要的实现类。Set 只能通过游标来取值,并且值是不能重复的。 List有序且允许元素重复。ArrayList、LinkedList和Vector是三个主要的实现类。 ArrayList 是线程不安全的, Vector 是线程安全的,这两个类底层都是由数组实现的 LinkedList 是线程不安全的,底层是由链表实现的 Map 是键值对集合。其中key列就是一个集合,key不能重复,但是value可以重复。 HashMap、TreeMap和Hashtable是Map的三个主要的实现类。 HashTable 是线程安全的,不能存储  null 值 HashMap 不是线程安全的,可以存储  null 二.List和ArrayList的区别   1.List是接口,List特性就是有序,会确保以一定的顺序保存元素.   ArrayList是它的实现类,是一个用数组实现的List.   Map是接口,Map特性就是根据一个对象查找对象.   HashMap是它的实现类,HashMap用hash表实现的Map,就是利用对象的hashcode(hashcode()是Object的方法)进行快速散列查找.(关于散列查找,可以参看<<数据结构>>)   2.一般情况下,如果没有必要,推荐代码只同List,Map接口打交道.   比如:List list =  new ArrayList();   这样做的原因是list就相当于是一个泛型的实现,如果想改变list的类型,只需要:   List list =  new LinkedList(); //LinkedList也是List的实现类,也是ArrayList的兄弟类   这样,就不需要修改其它代码,这就是接口编程的优雅之处.   另外的例子就是,在类的方法中,如下声明:    private void doMyAction(List list){}   这样这个方法能处理所有实现了List接口的类,一定程度上实现了泛型函数.   3.如果开发的时候觉得ArrayList,HashMap的性能不能满足你的需要,可以通过实现List,Map(或者Collection)来定制你的自定义类.

Three. Diagram Description:

Note: The http://www.cnblogs.com/xiaoqv/archive/2011/11/24/2262142.html of the drawing parameters

Int[] m = {1, 2, 3};
String[] strings = {"AAA", "BBB"};
list<string> list = new arraylist<string> ();
list<integer> lists = new arraylist<integer> ();
list<map<string, object>> list2 = new arraylist<map<string,object>> ();
list<city> listcity = new arraylist<city> ();

Collections and Arrays in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.