一 集合(collection)总接口
1.1集合总接口和实现类:
1.2总接口的方法:
二 列表
2.1 ArrayList 实现类
2.1.1 列表添加
// 1、生成一个新的列表
ArrayList<String> list01 = new ArrayList<String> ((Arrays.asList("1","2","3","4","5","6")));
// -------------------------------------------------------------------------------------------------
ArrayList<String> list02 = new ArrayList<String>();
// 2、向列表末尾添加元素
list02.add("haha");
// 3、向列表中指定角标位置添加元素
list02.add(0, "xixi");
// 4、向列表末尾添加列表中所有元素
ArrayList<String> list03 = new ArrayList<String>(list01);
list03.addAll(list01);
// 5、向列表指定角标位置添加列表中所有元素
list03.addAll(0,list02);
// 6、列表克隆
ArrayList list04= (ArrayList) list02.clone();
2.1.1 列表修改
ArrayList<String> list02 = new ArrayList<String>();
list02.add("haha");
list02.add(0, "xixi");
// 向列表中指定角标位置修改元素
list02.set(1,"lala");
2.1.1 列表删除
ArrayList<String> list02 = new ArrayList<String>();
list02.add("haha");
list02.add(0, "xixi");
// 1、向列表中删除元素
list02.remove("haha");
// 2、向列表中指定角标位置删除元素
list02.remove(0);
// 3、清空列表
list02.clear();
// 4、元素是否在列表中
list02.contains("7");
2.1.1 列表查找
ArrayList<String> list02 = new ArrayList<String>();
list02.add("haha");list02.add("haha");list02.add("haha");
list02.add(0, "xixi");
// 1、向列表中指定角标位置查找元素
list02.get(1);
// 2、查找元素在列表中第一次出现的角标位置
list02.indexOf("haha");
// 3、查找元素在列表中最后一次出现的角标位置
list02.lastIndexOf("haha");
// 4、查找在一定范围内的列表,前包后不包(此方法返回的列表影响原列表)
list02.subList(0,1);
// 5、查找按一定步长(最后一个参数)在一定范围内的列表(此方法返回的列表影响原列表,tempList影响list02)
List tempList=superSubList(list02,1,2,2);
// 6、查找与另一列表的交集的可重复列表(此方法影响list02列表)
list02.retainAll(list01);
private static List superSubList(List aList, int fromIndex, int toIndex, int step) {
int aListSize=aList.size();
if (fromIndex < 0)
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
if (toIndex > aListSize)
throw new IndexOutOfBoundsException("toIndex = " + toIndex);
if (fromIndex > toIndex)
throw new IllegalArgumentException("fromIndex(" + fromIndex +
") > toIndex(" + toIndex + ")");
for (int i = fromIndex; i < toIndex; i = i + step) {
aList.add(aList.get(i));
}
return aList.subList(aListSize,aList.size());}
2.1 LinkedList 实现类(理论上增删快)
LinkedList<String> list03 = new LinkedList<String>(list01);
// 查找列表第一个元素,没有返回null
list03.peekFirst();
// 查找列表最后一个元素,没有返回null
list03.peekLast();
// 删除列表第一个元素,没有返回null
list03.pollFirst();
// 删除列表最后一个元素,没有返回null
list03.pollLast();
三 Set(列表的无重复版本)
3.1 HashSet(不用排序,重写equals,hashCode方法)
重写目的在于可以指定元素是否相同
3.2 TreeSet(排序,重写compareTo)
重写的目的在于可以指定顺序排序(对象属性排序比较compareTo)
比较器是实现Comparator类的compare方法,在方法中用compareTo来对比(0相等,正数大于,负数小于)
TreeSet ts = new TreeSet(new Comparator());
四 Map总接口
4.1map总接口和实现类:
4.1map方法:
四 HashMap
// 创建HashMap
HashMap<Integer,String> hsMap=new HashMap<Integer,String>();
// 添加内容
hsMap.put(0,"xixi");
hsMap.putAll(hsMap03);
// 修改内容,值存在可修改
hsMap.put(0,"lala");
hsMap.putAll(hsMap03);
// 删除内容
hsMap.remove(0);
hsMap.clear();//清空hsmap
// 查找内容,不存在返回null
hsMap.get(0);
// -------------------------------------------
//成员关系判断
// hsMap是否为空
hsMap.isEmpty();
// key是否在hsMap
hsMap.containsKey(2);
// value是否在hsMap
hsMap.containsValue("lala");
// 返回set列表集合,里面有hsMap所有key
hsMap.keySet();
// 返回 Collection列表集合,里面有hsMap所有value
hsMap.values();
// 使hsMap变成set<Map.Entry<K,V>>集合,这样就可以使用集合的迭代器或者增强for,循环出来的是key
hsMap.entrySet();
五 Hashtable
不允许key和value是null
六 TreeMap(排序,重写compareTo)
有顺序,可做比较器构造
有getfirst*()获取最小值
有last*() 获取最大值
有floor*(K key)小于或等于此key的value
/// 使hsMap变成set<Map.Entry<K,V>>集合,这样就可以使用集合的迭代器或者增强for,循环出来的是Map.Entry<K,V>,可使用getKey()和getValue()方法获取key和value
trMap.entrySet()
//使用迭代器
iterator()