1. 当向ArrayList添加一个对象时,实际上就是将该对象放置到了ArrayList底层所维护的数组当中;当向LinkedList中添加一个对象时,实际上LinkedList内部会生成一个Node对象,该Node对象的结构为:
1void linkLast(E e) { 2 final Node<E> l = last; 3 final Node<E> newNode = new Node<>(l, e, null); 4 last = newNode; 5 if (l == null) 6 first = newNode; 7 else 8 l.next = newNode; 9 size++; 10 modCount++; 11 } 12 13private static class Node<E> { 14 E item; 15 Node<E> next; 16 Node<E> prev; 17 18 Node(Node<E> prev, E element, Node<E> next) { 19 this.item = element; 20 this.next = next; 21 this.prev = prev; 22 } 23 }
其中的Object类型的元素element就是我们向LinkedList中所添加的元素,然后Node又构造好了向前与向后的引用previous、next,最后将生成的这个Node对象加入到了链表当中。换句话说,LinkedList中所维护的是一个个的Node对象。
2. 关于Object类的equals方法的特点
a) 自反性:x.equals(x)应该返回true
b) 对称性:x.equals(y)为true,那么y.equals(x)也为true。
c) 传递性:x.equals(y)为 true并且y.equals(z)为true,那么x.equals(z)也应该为true。
d) 一致性:x.equals(y)的第一次调用为true,那么x.equals(y)的第二次、第三次、第n次调用也应该为true,前提条件是在比较之间没有修改x也没有修改y。
e) 对于非空引用x,x.equals(null)返回false。
3. 关于Object类的hashCode()方法的特点:
a) 在Java应用的一次执行过程当中,对于同一个对象的hashCode方法的多次调用,他们应该返回同样的值(前提是该对象的信息没有发生变化)。
b) 对于两个对象来说,如果使用equals方法比较返回true,那么这两个对象的hashCode值一定是相同的。
c) 对于两个对象来说,如果使用equals方法比较返回false,那么这两个对象hashCode值不要求一定不同(可以相同,可以不同),但是如果不同则可以提高应用的性能。
d) 对于Object类来说,不同的Object对象的hashCode值是不同的(Object类的hashCode值表示的是对象的地址)。
4. 当使用HashSet时,hashCode()方法就会得到调用,判断已经存储在集合中的对象的hash code值是否与增加的对象的hash code值一致;如果不一致,直接加进去;如果一致,再进行equals方法的比较,equals方法如果返回true,表示对象已经加进去了,就不会再增加新的对象,否则加进去。
1HashSet set = new HashSet(); 2 3// set.add(new People("zhangsan")); 4// set.add(new People("lisi")); 5// set.add(new People("zhangsan")); 6 7// People p1 = new People("zhangsan"); 8// 9// set.add(p1); 10// set.add(p1); 11 12 String s1 = new String("a"); 13 String s2 = new String("a"); 14 15 System.out.println("hash code: " + (s1.hashCode() == s2.hashCode())); 16 17 set.add(s1); 18 set.add(s2); 19 20 System.out.println(set)
String的HashCode方法:
1/** 2 * Returns a hash code for this string. The hash code for a 3 * {@code String} object is computed as 4 * <blockquote><pre> 5 * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] String类型的HasCode计算法 6 * </pre></blockquote> 7 * using {@code int} arithmetic, where {@code s[i]} is the 8 * <i>i</i>th character of the string, {@code n} is the length of 9 * the string, and {@code ^} indicates exponentiation. 10 * (The hash value of the empty string is zero.) 11 * 12 * @return a hash code value for this object. 13 */ 14 public int hashCode() { 15 int h = hash; 16 if (h == 0 && value.length > 0) { 17 char val[] = value; 18 19 for (int i = 0; i < value.length; i++) { 20 h = 31 * h + val[i]; 21 } 22 hash = h; 23 } 24 return h; 25 }
5. 如果我们重写一个类的equals方法,那么也要重写hashCode方法,反之亦然。
重写HashCode是为了在集合中应用。
Eclipse 自动重写HashCode()和equals();Source->Generate....
迭代器:(类似于GIS里面的cursor)
通常希望循环通过类集中的元素。例如,可能会希望显示每一个元素。到目前为止,处理这个问题的最简单方法是使用iterator,iterator是一个或者实现Iterator或者实现ListIterator接口的对象。Iterator可以完成循环通过类集,从而获得或删除元素。ListIterator扩展Iterator,允许双向遍历列表,并可以修改单元.
**在通过迭代函数访问类集之前,必须得到一个迭代函数。每一个Collection类都提供一个iterator()函数,该函数返回一个对类集头的迭代函数。通过使用这个迭代函数对象,可以访问类集中的每一个元素,一次一个元素。通常,使用迭代函数循环通过类集的内容,步骤如下
–1. 通过调用类集的iterator( )方法获得对类集头的迭代函数。
–2. 建立一个调用hasNext( )方法的循环,只要hasNext( )返回true,就进行循环迭代。
–3. 在循环内部,通过调用next( )方法来得到每一个元素
**
1public static void main(String[] args) 2 { 3 HashSet hash = new HashSet(); 4 hash.add("I"); 5 hash.add(" Miss "); 6 hash.add(" You "); 7 hash.add(" WangBingJia"); 8 Iterator iterator = hash.iterator(); 9 while (iterator.hasNext()) 10 { 11 System.out.println(iterator.next().toString()); 12 } 13 }
TreeSet 有顺序的集合,使用前通常需要定义 Comparator,即实现Comparator接口中Compare方法
1public class TreeSetTest 2{ 3 4 public static void main(String[] args) 5 { 6 Compare compare=new Compare(); 7 TreeSet ts=new TreeSet(compare); 8 ts.add((new Student(10))); 9 ts.add(new Student(20)); 10 System.out.println(ts); 11 12 } 13 14 15} 16class Compare implements Comparator 17{ 18 19 @Override 20 public int compare(Object o1, Object o2) 21 { 22 // TODO Auto-generated method stub 23 Student s1=(Student) o1; 24 Student s2=(Student) o2; 25 if (s1 .socore<s2.socore) 26 { 27 return -1; 28 } 29 else if (s1 .socore==s2.socore) 30 { 31 return 0; 32 } 33 else { 34 return 1; 35 } 36 37 } 38 } 39class Student 40{ 41 int socore; 42 Student(int socore){ 43 this.socore=socore; 44 } 45 @Override 46 public String toString() 47 { 48 // TODO Auto-generated method stub 49 return Integer.toString(socore); 50 } 51 }
Collections类的一些静态方法
reverseOrder() 反序排列
shuffle 打乱顺序
1public static void main(String[] args) 2 { 3 LinkedList list = new LinkedList(); 4 5 list.add(new Integer(-8)); 6 list.add(new Integer(20)); 7 list.add(new Integer(-20)); 8 list.add(new Integer(8)); 9 10 Comparator r = Collections.reverseOrder(); 11 12 Collections.sort(list, r); 13 14 for(Iterator iter = list.iterator(); iter.hasNext();) 15 { 16 System.out.println(iter.next() + " "); 17 } 18 19 System.out.println(); 20 21 Collections.shuffle(list); 22 23 for(Iterator iter = list.iterator(); iter.hasNext();) 24 { 25 System.out.println(iter.next() + " "); 26 } 27 28 System.out.println("minimum value: " + Collections.min(list)); 29 System.out.println("maximum value: " + Collections.max(list)); 30 }
6. Map(映射):实现类HashMap
Map的keySet()方法会返回key的集合,因为Map的键是不能重复的,因此keySet()方法的返回类型是Set;而Map的值是可以重复的,因此values()方法的返回类型是Collection,可以容纳重复的元素。
1HashMap hashMap=new HashMap(); 2hashMap.put("a","Bingjia"); 3hashMap.put("a","I"); 4hashMap.put("b","Bingjia"); 5System.out.println(hashMap);
通过hashMap的KeySet方法,实现对hashMap的遍历
1Set set=hMap.keySet(); 2 Iterator iterator=set.iterator(); 3 while (iterator.hasNext()) 4 { 5 System.out.println(hMap.get(iterator.next())); 6 7 }
HashMap中储存的是一个一个Map.Entry对象,每一个Map.Entry维护了一对key和value
所以对HashMap的遍历可以遍历取出Map.Entry
1hMap.put("1","I"); 2 hMap.put("2","Miss"); 3 hMap.put("3","You"); 4 hMap.put("4","WangBingjia"); 5 6 7 Set set =hMap.entrySet(); 8 Iterator iterator=set.iterator(); 9 while (iterator.hasNext()) 10 { 11 Map.Entry entry=(Entry) iterator.next(); 12 System.out.println("key "+entry.getKey()+" values "+ entry.getValue()); 13 14 }
利用hashMap统计数字
HashMap hMap=new HashMap();
for(int i=0;i<args.length;i++)
{
if (hMap.containsKey(args[i]))
{
Object o1= hMap.get(args[i]);
Integer integer=(Integer )o1;
integer ++;
hMap.put(args[i],integer);
}
else {
hMap.put(args[i],1);
}
}
System.out.println(hMap);