相关文章:
Java 集合框架分析:Set
http://blog.youkuaiyun.com/youyou1543724847/article/details/52733723
Java 集合框架分析:LinkedList
http://blog.youkuaiyun.com/youyou1543724847/article/details/52734935
Java 集合框架分析:DelayQueue
http://blog.youkuaiyun.com/youyou1543724847/article/details/52176504
Java 集合框架分析:ArrayBlockingQueue
http://blog.youkuaiyun.com/youyou1543724847/article/details/52174308
Java 集合框架分析:ArrayDeque
http://blog.youkuaiyun.com/youyou1543724847/article/details/52170026
Java 集合框架分析:PriorityBlockingQueue
http://blog.youkuaiyun.com/youyou1543724847/article/details/52166985
Java 集合框架分析:JAVA Queue源码分析
http://blog.youkuaiyun.com/youyou1543724847/article/details/52164895
Java 集合框架分析:关于Set,Map集合中元素判等的方式
http://blog.youkuaiyun.com/youyou1543724847/article/details/52733766
Java 集合框架分析:ConcurrentModificationException
http://blog.youkuaiyun.com/youyou1543724847/article/details/52733780
Java 集合框架分析:线程安全的集合
http://blog.youkuaiyun.com/youyou1543724847/article/details/52734876
Java 集合框架分析:JAVA集合中的一些边边角角的知识
http://blog.youkuaiyun.com/youyou1543724847/article/details/52734918
关于Set,Map集合中元素判等的方式:
Hash*
一般是基于equals,hashcode方式来判定元素是否相等。如果你加入到Set中的对象没有重写这两个方法,则使用的Object中的equals,hashcode方法(即判定地址)。
如:
CompareTo接口的方法:
public int compareTo(Object arg0) {
// this 对象小,返回-1
return 0;
}
另外:关于HashMap的equals方法:
因为hashmap运行存放key=null的键值对(key=null的条目放在table[0]这个位置),在put(key,value)函数中,如果在对应的table[i]中不存在key=newkey的条目,则就是插入,否则就是更新值。所以,如果其他的key也映射到了table[0],就会调用equals()方法,传入的参数就是null,因此,在实现equals,需要考虑实参为null的情况,否则会报错:(当然如果把其他的key也映射到table[0]上,说明你的hashCode函数实现的随机性不好)
Tree*
是使用Comparable的CompareTo方法,即加入到TreeSet中的对象必须是Comparable的子类,实现了CompareTo方法,否则会报错。
这个比较好理解:你要把元素放入到一颗红黑树中,Tree*就需要调整元素的位置,如何调整,自然是比较大小,大的往右边走,小的往左边走。
jdk中的原文解释:Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface