常用collection框架
ArrayList:数组列表 linkedList:链表列表 Stack :栈 PriorityQueue :优先队列 HashMap:字典(非线程安全的) SortedMap:有序字典 HashTable:字典(线程安全的)
collection常用算法
#大多数算法都是用于操作List对象 #有两个(min和max)可用于任意集合对象 sort:排序算法 shuffle:洗牌算法 reverse:将一个List中的元素反向排列 fill:用指定的值覆写List中的每一个元素 copy:接受两个参数,目标List和源List binarySearch:二分法查找算法
Arrays类
java.util.Arrays
• 常用方法
▫ fill (type[] a, type val):给数组填充,就是简单地把一个数组全部或者某段数据填成一个特殊的值;
▫ equals (type[] a, type[] b):实现两个数组的比较,相等时返回true;
▫ sort (type[] a): 对数组排序;
▫ binarySearch ( ): 对数组元素进行二分法查找;
▫ asList(T... a): 实现数组到ArrayList的转换;
▫ toString(基本类型或object数组引用):实现数组到string的转换。
• Arraylist常用方法
▫ boolean add(E e)
▫ void add(int index, E element)
▫ boolean addAll(Collection extends E> c)
▫ boolean addAll(int index, Collection extends E> c)
▫ E get(int index)
▫ int indexOf(Object o)
▫ boolean isEmpty()
▫ E remove(int index)
▫ boolean remove(Object o)
▫ boolean removeAll(Collection> c)
▫ E set(int index, E element)
▫ int size()
#数组的填充和复制
import java.util.*;
public class CopyingArrays {
public static void main(String[] args) {
int[] i = new int[25];
int[] j = new int[25];
Arrays.fill(i, 47);
Arrays.fill(j, 99);
System.arraycopy(i, 0, j, 0, i.length);
int[] k = new int[10];
Arrays.fill(k, 103);
System.arraycopy(i, 0, k, 0, k.length);
Arrays.fill(k, 103);
System.arraycopy(k, 0, i, 0, k.length);
Integer[] u = new Integer[10];
Integer[] v = new Integer[5];
Arrays.fill(u, new Integer(47));
Arrays.fill(v, new Integer(99));
System.arraycopy(v, 0, u, u.length/2, v.length);
#数组的比较
import java.util.*;
public class ComparingArrays{
public static void main(String[] args) {
int[] a1 = new int[10];
int[] a2 = new int[10];
Arrays.fill(a1, 47);
Arrays.fill(a2, 47);
System.out.println(Arrays.equals(a1, a2)); //true
a2[3] = 11;
System.out.println(Arrays.equals(a1, a2)); //false
String[] s1 = new String[5];
Arrays.fill(s1, "Hi");
String[] s2 = {"Hi", "Hi", "Hi", "Hi", "Hi"};
System.out.println(Arrays.equals(s1, s2));//true
}
}
ArrayList构造方法
• ArrayList() ▫ 构造一个空表,默认容量为10。 • ArrayList(Collection extends E> c) ▫ 用参数集合元素为初始值构造一个表。 • ArrayList(int initialCapacity) • 构造一个空表,容量为initialCapacity 。
Map类
• 抽象方法主要有 ▫ 查询方法 ▫ 修改方法 • 两个主要实现类 ▫ HashTable ▫ HashMap #Map接口的查询方法: • int size() —— 返回Map中的元素个数 • boolean isEmpty() —— 返回Map中是否包含元素,如不包括任何元素,则返回true • boolean containsKey(Object key) —— 判断给定的参数是否是Map中的一个关键字(key) • boolean containsValue(Object val) —— 判断给定的参数是否是Map中的一个值(value) • Object get(Object key) —— 返回Map中与给定关键字相关联的值(value) • Collection values() —— 返回包含Map中所有值(value)的Collection对象 • Set keySet() ——返回包含Map中所有关键字(key)的Set对象 • Set entrySet() —— 返回包含Map中所有项的Set对象 #Map接口的修改方法 Object put(Object key, Object val) —— 将给定的关键字(key)/值(value)对加入到Map对象中。其中关键字(key)必须唯一,否则,新加入的值会取代Map对象中已有的值 ▫ void putAll(Map m) —— 将给定的参数Map中的所有项加入到接收者Map对象中 ▫ Object remove(Object key) —— 将关键字为给定参数的项从Map对象中删除 ▫ void clear() —— 从Map对象中删除所有的项
HashMap构造方法
• HashMap() ▫ 默认容量16,默认装填因字0.75。 • HashMap(int initialCapacity) ▫ 容量initialCapacity ,默认装填因字0.75 。 • HashMap(int initialCapacity, float loadFactor) ▫ 容量initialCapacity ,装填因字loadFactor。 • HashMap(Map extends K,? extends V> m)
遍历实现了Collection接口的集合
• 通过Enumeration及Iterator接口遍历集合;
• 增强for循环遍历集合,for ( Type a : 集合对象)
• 通过聚集操作遍历集合。
Iterator具有如下三个实例方法
▫ hasNext() —— 判断是否还有元素。
▫ next() —— 取得下一个元素。
▫ remove() —— 去除一个元素。注意是从集合中去除最后调用next()返回的元素,而不是从Iterator类中去除。
import java.util.Vector;
import java.util.Iterator;
public class IteratorTester {
public static void main(String args[]) {
String[] num = {"one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten"};
Vector aVector = new Vector (java.util.Arrays.asList(num));
System.out.println("Before Vector: " + aVector);
Iterator nums = aVector.iterator();
while(nums.hasNext()) {
String aString = (String)nums.next();
System.out.println(aString);
if (aString.length() > 4) nums.remove();
}
System.out.println("After Vector: " + aVector);
}
}
for (int[][] a2 : arr) {
for (int[] a1 : a2) {
for (int x : a1) {
System.out.println(x);
假设有一个实现了Collection接口的myShapesCollection集合对象,有getColor() 可以返回对象的颜色,getName()方法返回对象的名字,则遍历并输出红色对象的名字:
myShapesCollection.stream()
.filter(e -> e.getColor() == Color.RED)
.forEach(e -> System.out.println(e.getName()));



