Java 集合机制学习总结


前言

  与C++STL一样,Java集合类库提供了一个强大而全面的数据结构类库——List、Set、Queue、Hash、Map等。但同时,Java 的设计者不希望集合像STL那样复杂,却具备STL那样强大的能力和执行效率,因此集合与STL在很多时候形相近而意不同。本文是对《Java核心技术卷Ⅰ》第九章(p344~p381)的学习总结。

Collection

  Java集合框架为不同类型的集合定义了大量接口,如图所示:
在这里插入图片描述
  其中,Collection是一个接口,一般而言不提倡直接实现,我们使用其扩展类的情形多一点。Java主张集合的接口与实现分离,Collection定义了其框架内的类都具有哪些基本功能,而至于说这些功能如何实现,则视具体抽象类情况而定。 下面列举一些通用方法——

	int size();
	boolean isEmpty();
	boolean contains(Obeject obj);
	boolean equals(Object other);
	boolean remove(Object obj);
	Object[] toArray();
	boolean addAll(Collection<? extends E> from);
	...

  Java库中具体集合实现类如下:

序号 集合类型 描述
1 ArrayList 一种可以动态增长和缩减的索引序列
2 LinkedList 一种可以在任何位置进行高效插入和删除的有序序列
3 ArrayQueue 一种用循环数组实现的双端队列
4 HashSet 一种没有重复元素的无序集
5 TreeSet 一种有序集
6 EnumSet 一种枚举类型的集
7 LinkedHashSet 一种可以记录元素插入次序的集
8 PriorityQueue 一种允许高效删除最小元素的队列(优先级队列)
9 HashMap 一种存储键值的映射表
10 TreeMap 一种键值有序排列的映射表
11 EnumMap 键值是枚举型的映射表
12 LinkedHashMap 一种可以记录键值插入顺序的映射表
13 WeakHashMap 一种可以不被使用之后可以被垃圾回收的映射表
14 IdentityHashMap 一种使用== 而不是equals比较键值的映射表

  

LinkedList

  链表(linked list)结构,内部是一个双向链表(double linked)结构,由于链表是一种顺序表表结构,优势在于结点的删除与插入,劣势在于元素的访问不支持随机访问。使用add方法添加元素,iterator迭代器或者next方法进行元素的访问,举例如下——

import java.util.*;

public class UseCollection {
    public static void main(String args[]){
        
        LinkedList<String> list = new LinkedList<>();
        list.add("Jack");
        list.add("Joe");
        list.add("King");
        list.add("Jobs");

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

        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}

在这里插入图片描述

ArrayList

  ArrayList是一种支持随机访问的线性表,有两种方式可以访问内部元素:第一种是通过迭代器(通用方式),第二种是通过get/set方法随机访问每个元素(个性特征)。ArrayList内部封装了一个动态数组,可以自动扩容。与Vector相比,ArrayList具有几乎完全相同的性能,不同的是,ArrayList不具备方法同步的性能,因此,在单线程操作时,具有更好的性能和安全性(不会触发某些并发修改的异常)。在这里插入图片描述

import java.util.*;

class ArrayListTest{
    public static void main(String[] args){
        ArrayList<String> arrayList = new ArrayList<>();
        char a = 'A';
        for(int i=0;i<26;i++){
            arrayList.add(String.valueOf((char)(i + a)));
        }
		//迭代器访问
        Iterator<String> iterator = arrayList.iterator();
        System.out.println(iterator.next());
		//get方法访问
        for(int i=0;i<arrayList.size();i++){
            System.out.print(arrayList.get(i)+" ");
        }
    }
}

在这里插入图片描述

HashSet

  散列表(hash table)为每个对象计算一个整数,称之为散列码(hash code),散列码是由hashCode产生的一个整数,可以由用户自己实现,散列表在查找方面具有极高的性能。
  Java内部散列表是由链表数组组成的,每个列表被称之为一个桶(bucket),要查找表中对象的位置,先计算散列码,然后与桶数取余数,得到所在桶的索引。如果该桶存放满,则发生散列冲突的情况。在Java 8以后,桶满时会从链表变成平衡二叉树,以解决满桶所带来的问题。

  Java集合提供了一个HashSet类,它实现了基于散列表的集,下面是一个统计单词例子

import java.util.*;

class SetTest{
    public static void main(String[] args){
        Set<String> words = new HashSet<>();
        long totalTime = 0;
        try(Scanner in = new Scanner(System.in)){
            while(!in.hasNext("##")){
                String word = in.next();
                long callTime = System.currentTimeMillis();
                words.add(word);
                callTime = System.currentTimeMillis()-callTime;
                totalTime+=callTime;
            }
        }
        Iterator<String> iter = words.iterator();
        for(int i=0;i<=20&&iter.hasNext();i++){
            System.out.println(iter.next());
        }
        System.out.println(" ... ");
        System.out.println(words.size()+" distinct words. "+totalTime+" milliseconds");
    }
}

  输入海明威的《真实的高贵》

True nobility 
 
In a calm sea every man is a pilot.
 
But all sunshine without shade, all pleasure without pain, is not life at all.Take the lot of the happiest - it is a tangled yarn.Bereavements and blessings,one following another, make us sad and blessed by turns. Even death itself makes life more loving. Men come closest to their true selves in the sober moments of life, under the shadows of sorrow and loss.
 
In the affairs of life or of business, it is not intellect that tells so much as character, not brains so much as heart, not genius so much as self-control, patience, and discipline, regulated by judgment.
 
I have always believed that the man who has begun to live more seriously within begins to live more simply without. In an age of extravagance and waste, I wish I could show to the world how few the real wants of humanity are.
 
To regret one’s errors to the point of not repeating them is true repentance.There is nothing noble in being superior to some other man. The true nobility is in being superior to your previous self.

  统计出现频率前20的单词——

在这里插入图片描述

  
  
  
  
  
  
  
  
  
  
  
  

发布了222 篇原创文章 · 获赞 558 · 访问量 38万+

猜你喜欢

转载自blog.csdn.net/CV_Jason/article/details/98513758
今日推荐