Java SE 第七章泛型和合集--集合类--List接口、Set接口、Map接口

一、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 <String>arrayList;
	//初始化链表
	public static void init(){
    
    
		arrayList=new ArrayList<String>(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 <String> arrayList2=new ArrayList<String>(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<arr.length;i++){
    
    
			String str=(String)arr[i];
			System.out.println((i+1)+":"+str);
		}
	}
	public static void main(String[] args) {
    
    
		init();
		printInfo();
		modify();
		toArray();
	}

}

在这里插入图片描述
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<String>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<String>hashSet=new HashSet<String>();
		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<String>treeSet=new TreeSet<String>();
		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<String,Integer>hashMap=new HashMap<String,Integer>();
		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<Map.Entry<String,Integer>>set=hashMap.entrySet();
		for(Map.Entry<String, Integer>entry:set){
    
    
			System.out.println(entry.getKey()+":"+entry.getValue());
		}
		System.out.println("---------------");
		Set<String>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<String,Integer>treeMap=new TreeMap<String,Integer>();
		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<Map.Entry<String,Integer>>set=treeMap.entrySet();
		for(Entry<String,Integer>entry:set){
    
    
			System.out.println(entry.getKey()+":"+entry.getValue());
		}
		System.out.println("-----------");
		Set<String>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);
	}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_45618376/article/details/111406632