Java学习日记-10集合

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

集合(Collection)

1. 什么是集合?

集合是一组对象组成的一个整体,又称为容器,集合类属于java.util包。集合不同于数组的地方在于,一是它的容量是可变的,二是集合中只能存储对象,不能存储基本数据类型

Java的集合类库有一大一小两派,一大由Collection接口带领,一小由Map接口带领。

2. Collection接口

Collection接口下有两大分支,分别为List接口和Set接口。注意,Collection是接口,而Collections则是java.util包下的一个具体类。

2.1 List接口

List集合中允许有重复对象。

2.1.1 ArrayList类

ArrayList分配了一个动态再分配的对象数组数组(说白了就是长度可变的数组),可以通过索引进行随机访问,但是插入删除比较麻烦,这与数组的特性是一致的。

List list = new ArrayList();

注:ArrayList是线程不安全的,即它的方法没有同步;而Vector是线程安全的,它的所有方法都是同步的。

2.1.2 LinkedList类

LinkedList采用链表结构保存对象,插入删除方便,但是访问慢,这和链表的特性是一致的。

List list2 = new LinkedList();

1importjava.util.ArrayList;2importjava.util.List;34publicclassGather{5publicstaticvoidmain(String[] args) {6List list =newArrayList<>();7for(charch = 'a'; ch <= 'z'; ch++){8list.add(ch);9}1011intran = (int)(Math.random()*(list.size()-1));12System.out.PRintln("随机获取list中的元素:"+list.get(ran));1314list.remove(2);15for(inti = 0; i < list.size(); i++){16System.out.print(list.get(i)+" ");17}18}19}

ArrayList示例

2.2 Set接口

Set是没有重复元素的集合,遍历Set集合的顺序和插入顺序并不总是一致,Set集合中的元素可以看作是无序的。

2.2.1 HashSet类

HashSet基于散列表

举例:

1publicclassPeople {2privateString name;3privatelongid_card;4publicPeople(String name,longid_card){5this.name =name;6this.id_card =id_card;7}89publiclonggetId_card(){10returnid_card;11}1213publicvoidsetId_card(longid_card){14this.id_card =id_card;15}1617publicString getName(){18returnname;19}2021publicvoidsetName(String name){22this.name =name;23}24}25——————————华丽的分割线————————————26importjava.util.HashSet;27importjava.util.Iterator;28importjava.util.Set;2930publicclassCollectionDemo {3132publicstaticvoidmain(String[] args) {33Set<People> hashSet =newHashSet<People >();34hashSet.add(newPeople("张三", 2000));35hashSet.add(newPeople("李四", 2001));36hashSet.add(newPeople("王五", 2002));3738Iterator<People> it =hashSet.iterator();39System.out.println("集合中的元素是:");40while(it.hasNext()){41People person =it.next();42System.out.println(person.getName()+" "+person.getId_card());43}44}45}

HashSet示例

2.2.2 TreeSet类

TreeSet树集是一个有序集合,可以以任意顺序将元素插入到集合中。遍历时,按照排序后的顺序呈现,排序是用红黑树完成的。

TreeSet如何知道元素的大小呢,在默认情况下,TreeSet假定插入的元素实现了Comparable接口。这个接口定义了一个方法:

Public interface Comparable<T>{

Int compareTo(T other);

}

通过Comparable接口定义排列顺序有局限性。对于一个给定的类,只能实现这个接口一次,如果想要使用不同规则,多次对类对象进行排序,就要用到Comparator对象传递给TreeSet构造器来定义排序规则。Comparator接口声明了一个带有两个显式参数的compare方法:

Public interface Comparator<T>{

Int compare(T a, T b);

}

与compareTo方法一样,如果a位于b之前compare方法则返回负值,相等返回0,否则返回正值。

如果按照描述信息进行排序,就直接定义一个实现Comparator接口的类:

Class ItemComparator implements Comparator<Item>{

Public int compare(Item a, Item b){

String descrA = a.getDescription();

String descrB = b.getDescription();

Return descrA.compareTo(descrB);

}

}

然后将这个类的对象传递给树集的构造器:

ItemComparator comp = new ItemComparator();

SortedSet<Item> sortByDescrition = new TreeSet<>(comp);

(总是就是,在构造TreeSet时,传给它一个对象参数,这个对象是实现了Comparator接口的类的一个实例,它定义了一种比较TreeSet集中元素的规则,就酱紫(⊙o⊙)哦)

注:在Java中,散列表用链表数组实现,每个链表被称为桶(bucket)。要想查找表中对象的位置,就要先计算它的散列码(也就是哈希码hashCode),然后与桶的总数取余,所得到的结果就是保存这个元素的桶的索引。例如,散列码76268,并且有128个桶,则对象应该保存在第108号桶中。

桶数是指用于收集具有相同散列值的桶的数目。标准类库使用的桶数是2的幂,默认值为16(为表大小提供的任何值都将被自动转换为2的下一个幂)。

散列表可以用于实现几个重要的数据结构,其中最简单的就是set类型。Set的add方法首先在集中查找要添加的对象,如果不存在,就将这个对象添加进去。

3.Map接口

Map提供了将键值映射到值的对象,键不能重复,一个键映射到一个值。

3.1 HashMap类

HashMap也叫散列映射表,它对键进行散列,散列或比较函数只能作用于键,与键关联的值不能进行散列或比较。

HashMap是基于哈希表的Map接口的实现,增删的效率更高。允许null值和null键。

3.2 TreeMap类

TreeMap也叫树映射表,用键的整体顺序对元素进行排序。

TreeMap类不仅实现了Map类,还实现了java.util.SortedMap接口,因此集合中元素具有一定的顺序。增删效率比HashMap差。由于映射关系是根据键对象按照一定的顺序排列的,所以不允许键值为null

1publicclassEmp {2privateString e_id;3privateString e_name;4publicEmp(String e_id,String e_name){5this.e_id =e_id;6this.e_name =e_name;7}8publicString getE_id() {9returne_id;10}11publicvoidsetE_id(String e_id) {12this.e_id =e_id;13}14publicString getE_name() {15returne_name;16}17publicvoidsetE_name(String e_name) {18this.e_name =e_name;19}20}21—————————华丽的分割线———————————22importjava.util.HashMap;23importjava.util.Iterator;24importjava.util.Map;25importjava.util.Set;26importjava.util.TreeMap;2728publicclassMapText {29publicstaticvoidmain(String[] args) {30Map map =newHashMap();31Emp emp =newEmp("001","zhangsan");32Emp emp2 =newEmp("015","lisi");33Emp emp3 =newEmp("009","wangyi");34Emp emp4 =newEmp("011","tangbao");35map.put(emp.getE_id(), emp.getE_name());36map.put(emp2.getE_id(),emp2.getE_name());37map.put(emp3.getE_id(), emp3.getE_name());38map.put(emp4.getE_id(), emp4.getE_name());3940Set set =map.keySet();41Iterator it =set.iterator();42//HashMap的无序是指既不是按照添加顺序,也不是按照键值大小顺序43/*HashMap中哈希码的定义44* public final int hashCode() {45return Objects.hashCode(key) ^ Objects.hashCode(value);46}47*Objects类中哈希码的定义(Objects继承自Object)48*public static int hashCode(Object o) {49return o != null ? o.hashCode() : 0;50}51*/52System.out.println("HashMap类实现的Map集合,无序:");53while(it.hasNext()){54String str =(String)it.next();55String name =(String)map.get(str);56System.out.println(str+":"+name);57}5859TreeMap treemap =newTreeMap();60treemap.putAll(map);61Iterator it2 =treemap.keySet().iterator();62System.out.println("TreeMap 类实现的Map集合,键对象升序:");63while(it2.hasNext()){64String str =(String)it2.next();65String name =(String)map.get(str);66System.out.println(str+""+name);67}68}69}

TreeMap和HashMap

3.3其它

集合框架并没有将映射表视为一个集合,但是可以获得映射表的视图,这时一组实现了Collection接口的对象,或者它的子接口的视图。

有3个视图,分别是:键集、值集合(不是集)和键/值对集。键与键/值对形成了一个集,这是因为在映射表中一个键只能有一个副本。

Set<K> keySet()

Collection<K> values()

Set<Map.Entry<K,V>> entrySet()

注意:keySet既不是HashSet,也不是TreeSet,而是实现了Set接口的某个其他类的对象。Set接口扩展了Collection接口。

4.Iterator接口

通过迭代器Iterator接口可以遍历集合。

Iterator接口位于java.util包下,它有3个方法:

hasNext(): 如果还有元素可迭代,返回true

next(): 返回迭代的下一个元素

remove(): 删除上次调用next()方法时返回的元素(注意:对next()和remove()方法的调用具有相互依赖性,调用remove()之前没有调用next()是不合法的。如果想要删除后面的元素,必须通过调用next()越过前面的元素才行)

反复调用next()方法可以遍历集合,但是当到达了集合末尾,会抛出NoSuchElementException。

1Collection<String> c =...;2Iterator<String> iter =c.iterator();3while(iter.hasNext()){4String element =iter.next();5Do sth with element6}7//从Java SE 5.0 起,可以用for each循环来遍历8for(String element : c){9Do sth with element10}

Iterator接口

4.1迭代顺序

元素被访问的顺序取决于集合类型,如果对ArrayList进行迭代,从索引为0的位置开始,没迭代一次,索引加1。如果访问的是HashSet,元素将按随机顺序出现,虽然可以保证遍历,但无法预知访问次序。

4.2 ListIterator接口

1importjava.util.ArrayList;2importjava.util.Collections;3importjava.util.Iterator;4importjava.util.ListIterator;56publicclassListIteratorDemo {7publicstaticvoidmain(String[] args) {8ArrayList<Integer> array =newArrayList<>();9Collections.addAll(array, 1,2,3,4,5,6);10System.out.println("集合中的元素:"+array);11ListIterator<Integer> iterator =array.listIterator();1213booleanhasNext =iterator.hasNext();14System.out.println("集合是否还有下一个元素:"+hasNext);1516booleanhasprevious =iterator.hasPrevious();17System.out.println("集合是否还有前一个元素:"+hasPrevious);18System.out.println("nextIndex:"+iterator.nextIndex());19System.out.println("next:"+iterator.next());20System.out.println("nextIndex:"+iterator.nextIndex());21System.out.println("next:"+iterator.next());22System.out.println("previousIndex:"+iterator.previousIndex());23System.out.println("previous:"+iterator.previous());2425System.out.println("nextIndex:"+iterator.nextIndex());26System.out.println("next:"+iterator.next());2728//add将元素插入到nextIndex所指的位置29System.out.println("nextIndex:"+iterator.nextIndex());30iterator.add(999);31System.out.println("向集合中添加999后:"+array);3233//set将next()返回的元素替换为10034iterator.next();35iterator.set(100);36System.out.println("集合中的元素:"+array);3738//remove将100删除39iterator.remove();40System.out.println("集合中的元素:"+array);41}42}

ListIterator

5.Deque接口

Java SE 6中引入了Deque接口,并由ArrayDeque和LinkedList类实现。这两个类都提供了双端队列。在必要时可增加队列长度。

public interface Deque<E> extends Queue<E>

6.集合框架

框架(framework)是一个类的集,它奠定了创建高级功能的基础。框架包含很多超类,这些超类拥有非常有用的功能、策略和机制。框架使用者创建的子类可以扩展超类的功能,而不必重新创建这些基本的机制。例如,Swing就是一种用户界面的机制。

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航