集合类的框架如下:
Collection(接口)
List(接口):允许重复。
ArrayList
Vector
LinkedList
Set(接口):不允许重复
HashSet
TreeSet
Collection:由于collection是一个接口,不能实例化。
- collection中的方法(list继承他的方法,因此,以下方法都可以用)
-
- add(E e) :往集合里新增一个元素 return boolean
- addAll(Collection c) 往一个集合中新增一个集合的所有元素
- clear() 移除所有元素
- contains(Object o) 如果此 collection 包含指定的元素,则返回 true。
- containsAll(Collection c) 如果此 collection 包含指定 collection 中的所有元素,则返回 true。
- isEmpty() 如果此 collection 不包含元素,则返回 true。
- remove(Object o) 从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。
- removeAll(Collection c) 移除此 collection 中那些也包含在参数 collection 中的所有元素
- retainAll(Collection c) 交集
- size() 返回此 collection 中的元素数
- Object[] toArray() 返回包含此 collection 中所有元素的数组。
- Iterator<E> iterator() 返回在此 collection 的元素上进行迭代的迭代器
-
-
-
//迭代器的几种使用方法 Iterator<String> iterator = collection.iterator(); //第一种,使用iterator.next(); String next = iterator.next(); System.out.println("TestMain.main()X"+next); iterator.remove(); System.out.println("TestMain.main()X"+next); String next2 = iterator.next(); System.out.println("TestMain.main()X"+next2); //第二种,iterator.hasnext() while (iterator.hasNext()) { String string = (String) iterator.next(); System.out.println("TestMain.main()"+string); }
//第三种,for循环遍历
for (Iterator iterator2 = collection2.iterator(); iterator2.hasNext();) {
String string = (String) iterator2.next();
}
-
-
-
2,list,是collection的子类
-
- 遍历list中的元素,可以用iterator迭代器,也可以用list自己的迭代器ListIterator(listiterator是iterator的一个子接口)
-
//list接口 List list=new ArrayList(); list.add("hello"); list.add("world"); list.add("!"); //list特有的遍历方式 //第一种,不适用迭代器 for(int i=0;i<list.size();i++){ Object object = list.get(i); System.out.println("TestMain.main()"+object); } //第二种,使用List特有的迭代器 int i=0; //ListIterator listIterator = list.listIterator(); while(listIterator.hasNext()){ Object next = listIterator.next(); System.out.println("ListDemo.main()"+next); listIterator.set("student"+i++); } //listIterator while(listIterator.hasPrevious()){ Object previous = listIterator.previous(); System.out.println("TestMain.main()"+previous); } for(ListIterator listIterator=list.listIterator();listIterator.hasNext();){ Object next = listIterator.next(); System.out.println("TestMain.main()"+next); }
3,ArrayList(上面的都可以实现,因为ArrayList是他们的子类)
-
- ArrayList特有的方法(增强型for循环JDK1.5之后才有的)
-
for (Object object : arrayList) { System.out.println("TestMain.main()"+object); }
- 需要注意的是:增强性for循环中不能同时去增加或删除集合里面的元素。不然会报java.util.ConcurrentModificationException的错误。
- 正确的说法应该是,要删除list里面的元素,有两种方式,一种是通过list.size()做for循环,另一种是通过iterator迭代器删除(迭代器删除的时候不能通过list.remove删除,只能通过iterator.remove删除)如下所示:
-
for (Iterator iterator = list.iterator(); iterator.hasNext();) { Object object = (Object) iterator.next(); //list.remove(object);运行时会报错 iterator.remove();//运行成功 }
4,set
-
- set不允许重复。
- 实现原理:实际上用的是hashmap函数,而hashmap底层依赖的是哈希表。set的添加功能底层依赖hashcode与equals两个方法。
- 具体代码实现如下
-
hashset的底层实现是一个hashmap 哈希表 map = new HashMap<>(); 映射(key,value) 一一映射 set.add("hello"); (“hello” present) set.add("world"); set.add("cskaoyan"); set.add("java"); set.add("hello"); map.put(“hello” present) // Student s4 = new Student("zhaoliu",23); // Student s5 = new Student("zhagnsan",23); public V put(K key, V value) { //如果哈希表为空,新建一张哈希表 if (table == EMPTY_TABLE) { inflateTable(threshold); } //如果为null,会插入一个key为null的映射(null, present) if (key == null) return putForNullKey(value); //通过这个key去计算其哈希值 int hash = hash(key); "student" "student" 字符串的哈希值一样 //从哈希表里找对应的hash值的位置。如果没有返回0 int i = indexFor(hash, table.length); for (Entry<K,V> e = table[i]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || key.equals(k))) { V oldValue = e.value; e.value = value; e.recordAccess(this); return oldValue; } } modCount++; addEntry(hash, key, value, i); return null; } final int hash(Object k) { int h = hashSeed; if (0 != h && k instanceof String) { return sun.misc.Hashing.stringHash32((String) k); } h ^= k.hashCode(); 100 // This function ensures that hashCodes that differ only by // constant multiples at each bit position have a bounded // number of collisions (approximately 8 at default load factor). h ^= (h >>> 20) ^ (h >>> 12); return h ^ (h >>> 7) ^ (h >>> 4); } 字符串的hashcode :结论是两个字符串存储的字符一样,hash值一样。 public int hashCode() { int h = hash; if (h == 0 && value.length > 0) { char val[] = value; for (int i = 0; i < value.length; i++) { h = 31 * h + val[i]; } hash = h; } return h; }
- hashset的用法如下:
-
HashSet<Object> hashSet = new HashSet<>(); //插入的是基本类型 /*hashSet.add("student"); hashSet.add("add"); hashSet.add("student");*/ //如果插入的是自己写的类,如student类中,name,age.如何判断是否为同一个 Student student1=new Student("zhangsan",11); Student student2=new Student("lisi",22); Student student3=new Student("zhangsan",11); hashSet.add(student1); hashSet.add(student2); hashSet.add(student3); //此时需要重写student的equals方法和hashcode方法 for (Object object : hashSet) { System.out.println("TestMain.main()"+object); }
5,map
-
- map接口(映射),Map<K,V>,
K
- 此映射所维护的键的类型,V
- 映射值的类型 - map的成员方法如下:
- V put(K key,V value)
- V remove(Object key)
- void clear()
- boolean containsKey(Object key)
- boolean containsValue(Object value)
- boolean isEmpty()
- int size()
- V get(Object key)
- Set<K> keySet()
- Collection<V> values()
- Set<Map.Entry<K,V>> entrySet()
- hashmap案例如下
-
HashMap<String,String> HashMap<Integer,String> HashMap<String,Student> HashMap<Student,String>
- map接口(映射),Map<K,V>,
6,map和collection的区别
-
- Map是双列的,Collection是单列的
- Map的键唯一,Collection的子体系Set是唯一的
- Map集合的数据结构值针对键有效,跟值无关
- Collection集合的数据结构是针对元素有效