博客
关于我
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_real_connect 参数注意
查看>>
mysql_secure_installation初始化数据库报Access denied
查看>>
MySQL_西安11月销售昨日未上架的产品_20161212
查看>>
Mysql——深入浅出InnoDB底层原理
查看>>
MySQL“被动”性能优化汇总
查看>>
MySQL、HBase 和 Elasticsearch:特点与区别详解
查看>>
MySQL、Redis高频面试题汇总
查看>>
MYSQL、SQL Server、Oracle数据库排序空值null问题及其解决办法
查看>>
mysql一个字段为空时使用另一个字段排序
查看>>
MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
查看>>
MYSQL一直显示正在启动
查看>>
MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
查看>>
MySQL万字总结!超详细!
查看>>
Mysql下载以及安装(新手入门,超详细)
查看>>
MySQL不会性能调优?看看这份清华架构师编写的MySQL性能优化手册吧
查看>>
MySQL不同字符集及排序规则详解:业务场景下的最佳选
查看>>
Mysql不同官方版本对比
查看>>
MySQL与Informix数据库中的同义表创建:深入解析与比较
查看>>
mysql与mem_细说 MySQL 之 MEM_ROOT
查看>>
MySQL与Oracle的数据迁移注意事项,另附转换工具链接
查看>>