博客
关于我
Java SE 第七章泛型和合集--集合类--List接口、Set接口、Map接口
阅读量:368 次
发布时间:2019-03-04

本文共 5143 字,大约阅读时间需要 17 分钟。

一、List接口

说明:
1)该接口继承Collection接口
2)元素允许重复
3)以元素添加的次序来放置元素,不会重新排列
4)支持位置索引随机操作
List接口的具体实现类常用的有ArrayList和LinkList。
1、ArrayList支持可随需要而调整的动态数组。
其内部封装了一个动态可再分配的Object[]数组。
每个ArrayList对象都有一个capacity,表示存储列表中元素的数组的容量。
常用方法:
ArrayList()
Array List(Collection<?extends E>c)
ArrayList(int capacity)
void ensureCapacity(int minCapacity)
void trimToSize()
示例:基于String类型演示泛型ArrayList的常用操作,包括增加、删除、修改、遍历。

package ch07;import java.util.*;public class ArrayListDemo {   	public static ArrayList 
arrayList; //初始化链表 public static void init(){ arrayList=new ArrayList
(4); System.out.println("初始化长度:"+arrayList.size()); arrayList.add("First"); arrayList.add("second"); arrayList.add("third"); arrayList.add("Forth"); } //打印链表信息 public static void printInfo(){ System.out.println("增加元素后的长度:"+arrayList.size()); ArrayList
arrayList2=new ArrayList
(arrayList); System.out.println("arrayList:"+arrayList); System.out.println("arrayList2"+arrayList2); } //对链表实现修改、删除操作 public static void modify(){ arrayList.add(1,"insert data"); System.out.println("增加元素后的长度:"+arrayList.size()); arrayList.remove("second"); System.out.println("删除‘second’后的长度:"+arrayList.size()); arrayList.remove(2); System.out.println("删除第3个元素后的长度:"+arrayList.size()); arrayList.remove("nothing"); System.out.println("删除‘nothing’后的长度:"+arrayList.size()); } public static void toArray(){ Object[]arr=arrayList.toArray(); for(int i=0;i

在这里插入图片描述

2.LinkList常用方法列表
LinkedList()
LinkedList(Collection<?extends E>c)
void addFirst(E obj)
E getFirst()
E removeFirst()
void addLast((E ob)
E getLast()
E removeLast()
3.List的遍历
示例:演示使用Iterator遍历集合的功能。

public static void travel(){   		System.out.println("遍历前的长度:"+arrayList.size());		Iterator
iterator=arrayList.iterator(); int i=0; while(iterator.hasNext()){ String str=iterator.next(); i++; System.out.println(str); if(i%3==0){ iterator.remove(); } } System.out.println("删除后的长度:"+arrayList.size());

在这里插入图片描述

4.for-each

for(string str:arrayList){   System.out.println(str);}

二、Set接口

说明:
1)该接口继承Collection接口
2)允许重复
3)没有引入新方法,所以Set就是一个Collection,只是其行为不同
具体实现类:HashSet、TreeSet
1、HashSet(散列)
能快速定位一个元素,一般需要重写hashCode()方法。
构造方法列表:
HashSet()
HashSet(Collection<?extends E>c)
HashSet(int capacity)
HashSet(int capacity,float fill)
示例:基于String类型演示泛型HashSet的使用

public static void main(String[] args) {   		HashSet
hashSet=new HashSet
(); hashSet.add("first"); hashSet.add("second"); hashSet.add("third"); hashSet.add("forth"); System.out.println(hashSet); for(String str:hashSet){ System.out.println(str); }

在这里插入图片描述

2、TreeSet(树结构)
对象按升序存储,访问检索速度快。
构造方法列表:
TreeSet()
TreeSet(Collection<?extends E>c)
TreeSet(Comparator<?super E>comp)
TreeSet(SortedSetsortSet)
示例:基于String类型演示泛型TreeSet的使用

public static void main(String[] args) {   		TreeSet
treeSet=new TreeSet
(); treeSet.add("first"); treeSet.add("second"); treeSet.add("third"); treeSet.add("forth"); System.out.println(treeSet); for(String str:treeSet) { System.out.println(str); } }

在这里插入图片描述

从运行结果来分析出:TreeSet元素按字符串顺序排序存储。这就要求放入其中的元素是可排序的。集合框架提供提供了用于排序的两个实用接口:Comparable和Comparator。一个可排序的类应该实现Comparable接口(通常情况下,在没有指明排序依据的时候,添加到TreeSet中对象都要实现Comparable)。
三、Map接口(键值对)
说明:
1)不允许重复
2)Map的entrySet()方法返回一个实现Map.Entry接口的对象集合,使得可以单独操作Map的项(“键/值”)。
3)Map.Entry的方法列表:
K getKey()
V getValue()
int hashCode()
boolean equals(Object obj)
V setValue(V v)
两种比较常用的实现:HashMap、TreeMap
1、HashMap类(散列表)
a、元素加入散列映射的顺序并不一定是它们被迭代方法读出的顺序
b、允许使用null值和null键
示例:基于String和Integer类型演示泛型HashMap的使用。

public static void main(String[] args) {   		HashMap
hashMap=new HashMap
(); hashMap.put("Tom", new Integer(23)); hashMap.put("Rose", new Integer(18)); hashMap.put("Jane", new Integer(26)); hashMap.put("Black", new Integer(24)); hashMap.put("Smith", new Integer(21)); Set
>set=hashMap.entrySet(); for(Map.Entry
entry:set){ System.out.println(entry.getKey()+":"+entry.getValue()); } System.out.println("---------------"); Set
KeySet=hashMap.keySet(); StringBuffer buffer=new StringBuffer(""); for(String str:KeySet){ buffer.append(str+','); } String str=buffer.substring(0,buffer.length()-1); System.out.println(str); }

在这里插入图片描述

2、TreeMap
1)快速检索
2)元素按升序排列
构造方法:
TreeMap()
TreeMap(Comparator<? super K>comp)
TreeMap(Map<?extends K,?extends V>m)
TreeMap(SortedMap<K,?extends V>m)
示例:基于String和Integer演示泛型TreeMap的使用。

public static void main(String[] args) {   		TreeMap
treeMap=new TreeMap
(); treeMap.put("Tom", new Integer(23)); treeMap.put("Rose", new Integer(18)); treeMap.put("Jane", new Integer(26)); treeMap.put("Black", new Integer(24)); treeMap.put("Smith", new Integer(21)); Set
>set=treeMap.entrySet(); for(Entry
entry:set){ System.out.println(entry.getKey()+":"+entry.getValue()); } System.out.println("-----------"); Set
KeySet=treeMap.keySet(); StringBuffer buffer=new StringBuffer(""); for(String str:KeySet) { buffer.append(str+","); } String str=buffer.substring(0,buffer.length()-1); System.out.println(str); }

在这里插入图片描述

转载地址:http://edyg.baihongyu.com/

你可能感兴趣的文章
mysql 默认事务隔离级别下锁分析
查看>>
Mysql--逻辑架构
查看>>
MySql-2019-4-21-复习
查看>>
mysql-5.6.17-win32免安装版配置
查看>>
mysql-5.7.18安装
查看>>
MySQL-8.0.16 的安装与配置
查看>>
MySQL-Buffer的应用
查看>>
mysql-cluster 安装篇(1)---简介
查看>>
mysql-connector-java.jar乱码,最新版mysql-connector-java-8.0.15.jar,如何愉快的进行JDBC操作...
查看>>
mysql-connector-java各种版本下载地址
查看>>
mysql-EXPLAIN
查看>>
MySQL-Explain的详解
查看>>
mysql-group_concat
查看>>
MySQL-redo日志
查看>>
MySQL-【1】配置
查看>>
MySQL-【4】基本操作
查看>>
Mysql-丢失更新
查看>>
Mysql-事务阻塞
查看>>
Mysql-存储引擎
查看>>
mysql-开启慢查询&所有操作记录日志
查看>>