Java--Working with Colections--Using the list Interface

1.ArrayListjava

是一个可改变大小的数组.当更多的元素加入到ArrayList中时,其大小将会动态地增加.内部的元素能够直接经过get与set方法进行访问,由于ArrayList本质上就是一个数组.不一样步(就是线程不安全)数组

 

      ArrayList是一个动态数组,也是咱们最经常使用的集合。它容许任何符合规则的元素插入甚至包括null。每个ArrayList都有一个初始容量(10),该容量表明了数组的大小。随着容器中的元素不断增长,容器的大小也会随着增长。在每次向容器中增长元素的同时都会进行容量检查,当快溢出时,就会进行扩容操做。因此若是咱们明确所插入元素的多少,最好指定一个初始容量值,避免过多的进行扩容操做而浪费时间、效率。安全

      size、isEmpty、get、set、iterator 和 listIterator 操做都以固定时间运行。add 操做以分摊的固定时间运行,也就是说,添加 n 个元素须要 O(n) 时间(因为要考虑到扩容,因此这不仅是添加元素会带来分摊固定时间开销那样简单)。ide

      ArrayList擅长于随机访问。同时ArrayList是非同步的。函数

ArrayList :性能

 1 package test;
 2 //package com.algorithm.java.Niit2.Learning;
 3 
 4 //import com.sun.xml.internal.bind.v2.runtime.reflect.ListIterator;
 5 
 6 import java.util.ArrayList;
 7 import java.util.ListIterator;
 8 
 9 public class ArrayListDemo {
10     public static void main(String[] args){
11         ArrayList<String> obj=new ArrayList<String>();
12         String sobj1=new String("Element 1");
13         String sobj2=new String("Element 2");
14         String sobj3=new String("Element 3");
15         String sobj4=new String("Element 4");
16 
17         System.out.println("Size of ArrayList is :"+obj.size());
18         obj.add(sobj1);
19         obj.add(sobj2);
20         obj.add(sobj3);
21         obj.add(sobj4);
22         obj.add(sobj1);
23 
24         System.out.println("\nArrayList after adding the objects:"+obj);
25         System.out.println("\nSize of ArrayList after adding object "+obj.size());
26 
27         obj.remove(2);
28         obj.remove(sobj4);
29 
30         System.out.println("\nArrayList after removing the objects"+obj);
31         System.out.println("Size of ArrayList after removing objects:"+obj.size());
32 
33         System.out.println("\nThe final ArrayList :");
34         ListIterator i= (ListIterator) obj.listIterator();
35         while(i.hasNext()){
36             System.out.println(i.next());
37 
38         }
39 
40     }
41 
42 }
View Code

 

 

2.LinkedListspa

是一个双链表,在添加和删除元素时具备比ArrayList更好的性能.但在get与set方面弱于ArrayList.不一样步(就是线程不安全)线程

 

      一样实现List接口的LinkedList与ArrayList不一样,ArrayList是一个动态数组,而LinkedList是一个双向链表。因此它除了有ArrayList的基本操做方法外还额外提供了get,remove,insert方法在LinkedList的首部或尾部。code

      因为实现的方式不一样,LinkedList不能随机访问,它全部的操做都是要按照双重链表的须要执行。在列表中索引的操做将从开头或结尾遍历列表(从靠近指定索引的一端)。这样作的好处就是能够经过较低的代价在List中进行插入和删除操做。xml

      与ArrayList同样,LinkedList也是非同步的。若是多个线程同时访问一个List,则必须本身实现访问同步。一种解决方法是在建立List时构造一个同步的List: 
List list = Collections.synchronizedList(new LinkedList(...));

LinkedList:

 1 package test;
 2 //package com.algorithm.java.Niit2.Learning;
 3 
 4 //import com.sun.xml.internal.bind.v2.runtime.reflect.ListIterator;
 5 
 6 import java.util.ArrayList;
 7 import java.util.LinkedList;
 8 import java.util.ListIterator;
 9 
10 public class LinkedListDemo {
11     public static void main(String[] args){
12         LinkedList<String> obj=new LinkedList<String>();
13         String sobj1=new String("Element 1");
14         String sobj2=new String("Element 2");
15         String sobj3=new String("Element 3");
16         String sobj4=new String("Element 4");
17 
18         System.out.println("Size of ArrayList is :"+obj.size());
19         obj.add(sobj1);
20         obj.add(sobj2);
21         obj.add(sobj3);
22         obj.add(sobj4);
23         obj.add(sobj1);
24 
25         System.out.println("\nArrayList after adding the objects:"+obj);
26         System.out.println("\nSize of ArrayList after adding object "+obj.size());
27 
28         obj.remove(2);
29         obj.remove(sobj4);
30 
31         System.out.println("\nArrayList after removing the objects"+obj);
32         System.out.println("Size of ArrayList after removing objects:"+obj.size());
33 
34         System.out.println("\nThe final ArrayList :");
35         ListIterator i= (ListIterator) obj.listIterator();
36         while(i.hasNext()){
37             System.out.println(i.next());
38 
39         }
40 
41     }
42 
43 }
View Code

 

 

3.Vector

和ArrayList相似,但属于强同步类。若是你的程序自己是线程安全的(thread-safe,没有在多个线程之间共享同一个集合/对象),那么使用ArrayList是更好的选择。.同步(线程安全)

有句话叫越安全,效率就越低。

Vector:

 1 package test;
 2 //package com.algorithm.java.Niit2.Learning;
 3 
 4 //import com.sun.xml.internal.bind.v2.runtime.reflect.ListIterator;
 5 
 6 import java.util.ArrayList;
 7 import java.util.LinkedList;
 8 import java.util.ListIterator;
 9 import java.util.Vector;
10 
11 public class LinkedListDemo {
12     public static void main(String[] args){
13        Vector<String> obj=new Vector<String>();
14         String sobj1=new String("Element 1");
15         String sobj2=new String("Element 2");
16         String sobj3=new String("Element 3");
17         String sobj4=new String("Element 4");
18 
19         System.out.println("Size of ArrayList is :"+obj.size());
20         obj.add(sobj1);
21         obj.add(sobj2);
22         obj.add(sobj3);
23         obj.add(sobj4);
24         obj.add(sobj1);
25 
26         System.out.println("\nArrayList after adding the objects:"+obj);
27         System.out.println("\nSize of ArrayList after adding object "+obj.size());
28 
29         obj.remove(2);
30         obj.remove(sobj4);
31 
32         System.out.println("\nArrayList after removing the objects"+obj);
33         System.out.println("Size of ArrayList after removing objects:"+obj.size());
34 
35         System.out.println("\nThe final ArrayList :");
36         ListIterator i= (ListIterator) obj.listIterator();
37         while(i.hasNext()){
38             System.out.println(i.next());
39 
40         }
41 
42     }
43 
44 }
View Code

 

 

Map接口

本词条缺乏信息栏、名片图,补充相关内容使词条更完整,还能快速升级,赶忙来 编辑吧!
Map接口储存一组成对的键-值对象,提供key(键)到value(值)的映射,Map中的key不要求有序,不容许重复。value一样不要求有序,但能够重复。最多见的Map实现类是HashMap,他的储存方式是哈希表,优势是查询指定元素效率高。
 
Map接口提供了将键映射到集合的对象,一个映射不能包含重复的键.
每一个键最多只能映射到一个值.Map接口中一样提供了集合的经常使用方法,如clear()方法,isEmpty()方法,Size()方法等.

 

 

Map接口有三个比较重要的实现类,分别是HashMap、TreeMap和HashTable:

1.HashMap:哈希表:冲突处理为单链表;无序

2.TreeMap:二叉树:有序

3.HashTable:哈希表:冲突处理为单链表

*************Hashtable的方法是同步的,HashMap的方法不是同步的

Hashtable是线程安全的,HashMap不是线程安全的。

HashMap效率较高,Hashtable效率较低。

 =========================================================================================

HashMap:

注:基于哈希表的 Map 接口的实现。此实现提供全部可选的映射操做,并容许使用 null 值和 null 键。(除了非同步和容许使用 null 以外,HashMap 类与 Hashtable 大体相同。)此类不保证映射的顺序,特别是它不保证该顺序恒久不变。 此实现假定哈希函数将元素适当地分布在各桶之间,可为基本操做(get 和 put)提供稳定的性能。迭代 collection 视图所需的时间与 HashMap 实例的“容量”(桶的数量)及其大小(键-值映射关系数)成比例。因此,若是迭代性能很重要,则不要将初始容量设置得过高(或将加载因子设置得过低)。

相关文章
相关标签/搜索