Java集合器

来源:互联网 发布:时间碎片交友软件 编辑:程序博客网 时间:2024/06/07 14:54

 Java集合类(容器)

常用的集合有List,Set,Map,其中List和Set实现了Collection接口。


1.  与数组的区别

 数组长度是固定的,集合的长度是可变的

 数组存放基本类型数据,集合存放对象的引用

2. Collection 和Collections的区别:

Collection是集合类的上级接口,继承与他的接口主要有Set 和List. 

Collections是针对集合类的一个帮助类,他提供一系列静态方法实现对各种集合的搜索、排序、线程安全化等操作。 

   1)List

  •        特点

       List集合以线性方式存储对象,可以通过对象的索引来操作对象。

       List集合允许包含重复对象

  •        常用方法例子:

         add(Object ) ,set ,remove ,indexOf, lastIndexOf等方法

                public classListTest {

                     publicstatic void main(String[] args){

                            LinkedList<String>list = new LinkedList<String>();

                            //1. add(Objectobj)

                            list.add("a");

                            list.add("b");

                            list.add("a");

                            Iterator<String>addIterator = list.iterator();

                            while(addIterator.hasNext()){

                                   System.out.println(addIterator.next()+"");

                            }

             

                            //2. set(intindex, Object obj)

                            list.set(2,"c");

                            Iterator<String>setIterator = list.iterator();

                            while(setIterator.hasNext()){

                                   System.out.println(setIterator.next()+"");

                            }

             

                            //3. add(intindex, Object obj)

                            list.add(1,"d");

                            Iterator<String>add2Iterator = list.iterator();

                            while(add2Iterator.hasNext()){

                                   System.out.println(add2Iterator.next()+"");

                            }

             

                            list.add("a");

                            //4. indexOf(Objectobj),lastIndexOf(Object obj)

                            System.out.println(list.indexOf("a"));

                            System.out.println(list.lastIndexOf("a"));

             

                            //5. remove(intindex)

                            list.remove(0);

                            Iterator<String>removeIterator = list.iterator();

                            while(removeIterator.hasNext()){

                                   System.out.println(removeIterator.next()+"");

                            }

                    

                     //6. get(intindex)

                     System.out.println(list.get(0));

                     }

}

  • add和set 方法的区别:

add(Object obj)是在尾部添加对象

add(int index, Object obj)在指定位置添加对象,指定索引位置之后的对象后移

set是替换给定索引位置的对象为新对象,索引不能越界(在已有的对象索引范围内)

  • List实现类

ArrayList

List<String>list = new ArrayList<String>();

ArrayList内部实现是基于对象数组的

        优点:

  能够根据索引位置对集合进行快速的随机访问(比LinkedList快)。

缺点:

向指定的索引位置插入对象或删除对象速度较慢(比LinkedList慢)。

列表的结尾预留一定的容量空间,某种程度上造成空间浪费(ArrayList使用一个内置的数组来存储元素,这个数组的起始容量是10.当数组需要增长时,新的容量按如           下公式获得:新容量=(旧容量*3)/2+1,也就是说每一次容量大概会增长50%。这就意味着,如果你有一个包含大量元素的ArrayList对象,那么最终将有很大的空间会被浪            费掉,这个浪费是由ArrayList的工作方式本身造成的。如果没有足够的空间来存放新的元素,数组将不得不被重新进行分配以便能够增加新的元素)。 

        LinkedList

List<String>list = new LinkedList<String>();

内部是基于链表实现的

优点:插入数据比较快,在LinkedList的中间插入或删除一个元素的开销是固定的。

缺点:LinkedList不支持高效的随机元素访问。

    LinkedList的空间花费则体现在它的每一个元素都需要消耗相当的空间。

         LinkedList和ArrayList区别

                ArrayList和LinkedList在性能上各有优缺点,都有各自所适用的地方,总的说来可以描述如下:对ArrayList和LinkedList而言,在列表末尾增加一个元素所花的开销都          是固定的。对ArrayList而言,主要是在内部数组中增加一项,指向所添加的元素,偶尔可能会导致对数组重新进行分配;而对LinkedList而言,这个开销是统一的。在                  ArrayList的中间插入或删除一个元素意味着这个列表中剩余的元素都会被移动;而在LinkedList的中间插入或删除一个元素的开销是固定的。LinkedList不支持高效的随机元        素访问ArrayList的空间浪费主要体现在在list列表的结尾预留一定的容量空间,而LinkedList的空间花费则体现在它的每一个元素都需要消耗相当的空间。

        总之:当操作是在一列数据的后面添加数据而不是在前面或中间,并且需要随机地访问其中的元素时,使用ArrayList会提供比较好的性能;当你的操作是在一列数据的前面或中间添加或删除数据,并且按照顺序访问其中的元素时,就应该使用LinkedList了。

2)Set及其实现类

Set<String>set1 = new HashSet<String>();

Set<String>set2 = new TreeSet<String>();

Set集合中的对象是无序的,遍历Set集合的结果与插入Set集合的顺序并不相同。

Set集合中不允许存在重复值,使用addAll()方法能够将Collection集合添加到set集合中,能够去掉去掉重复值。

实例1:

public class SetTest {

  public static voidmain(String[] args){

         List<String>list = new ArrayList<String>();

         list.add("a");

        list.add("a");

         list.add("a");

         list.add("b");

         Set<String> set= new HashSet<String>();

         set.addAll(list);

         Iterator<String>it = set.iterator();

         while(it.hasNext()){

                System.out.println(it.next()+"\t");

         }

}

}

运行结果:

b

a

实例2:

public class SetTest {

                     privateString name;

                     privatelong id;

                     publicSetTest(String name, long id){

                     this.name =name;

                     this.id =id;

              }

              public StringgetName() {

                     returnname;

              }

              public voidsetName(String name) {

                     this.name =name;

              }

              public longgetId() {

                     return id;

              }

              public voidsetId(long id) {

                     this.id =id;

              }

}

 

publicclass MainTest {

              public static voidmain(String[] args){

                     Set<SetTest>hashSet = new HashSet<SetTest>();

                     hashSet.add(newSetTest("a", 1));

                     hashSet.add(newSetTest("b", 2));

                     hashSet.add(newSetTest("c", 3));

                     Iterator<SetTest>it= hashSet.iterator();

                     while(it.hasNext()){

                            SetTestst = it.next();

                            System.out.println(st.getName()+st.getId());

                     }

              }

}

运行结果:

a1

b2

c3

3)Map总结

Map中的元素是通过key,value进行存储的,要获取集合中指定的key值或value值,需要先通过相应的方法获取key集合或value集合,再遍历key集合或value集合获取指定值。

实例:

public class MapTest {

publicstatic void main(String[] args){

        Map<String,String>map = new HashMap<String, String>();

        map.put("1","a");

        map.put("2","a");

        map.put("3","c");

        for(inti=1;i<=3;i++){

               System.out.println("第"+i+"个元素是:"+ map.get(""+i+""));

        }

}

}

 Map接口的实现类:

HashMap和TreeMap其中HashMap更加常用。

HashMap类:

               是基于哈希表实现的,通过哈希码对内部的映射关系进行快速查找,对于添加和删除映射关系效率更高。允许使用null值和null键,键值必须保证唯一性映射是无序的。

                TreeMap类:

               因为TreeMap实现了java.util.SortedMap接口,因此集合中的映射关系具有一定的顺序。不允许键对象是null添加,删除和定位映射关系时,TreeMap比HashMap性能差。

实例:

public class MapTest {

public static void main(String[] args){

               Map<String,String> map = newHashMap<String,String>();

               map.put("first","one");

               map.put("second","two");

               map.put("third","three");

       

               Set set = map.keySet();

               System.out.println("HashMap里元素无序!");

               Iterator it = set.iterator();

               while(it.hasNext()){

                      String str = (String)it.next();

                      System.out.println(str);

               }

       

               TreeMap treemap = new TreeMap();

               treemap.putAll(map);

               Iterator iter =treemap.keySet().iterator();

               System.out.println("TreeMap里元素有序!");

               while(iter.hasNext()){

                      String str = (String)iter.next();

                      System.out.println(str);

               }    

       }

}

运行结果:

HashMap里元素无序!

second

third

first

TreeMap里元素有序!

first

second

third

 

0 0
原创粉丝点击