import java.io.Serializable;
import java.util.Comparator;
import java.util.Map;
import java.util.Objects;
/**
* Create by ~JH~ on2018/4/11
*/
/**
*这个是Map的内部接口,Map.entrySet()方法返回一个map的集合视图其中的元素就是Entry。
* 这是唯一的方式获得map entry引用对集合视图进行迭代,而且对象的有效期仅仅是迭代期。
*在entry迭代返回之后,如果map被修改,,这个map entry是undefined的,
* 除非通过setValue操作这个map entry
* @see Map#entrySet()
* @since1.2
*/
interface Entry<K,V> {
/**
* Returns the key corresponding to this entry.
*
* @returnthe key corresponding to this entry返回与k这个entry一致的key
* @throws IllegalStateException implementations may, but are not
* required to, throw this exception ifthe entry has been
* removed fromthe backing map.
*/
K getKey();
/**
*如果在迭代返回之后的map entry 中映射被移除了,这个方法的执行结果就是undefined
* @returnthe value corresponding to this entry返回这个Entry的value
* @throws IllegalStateException implementations may, but are not
* required to, throw this exception ifthe entry has been
* removed fromthe backing map.
*/
V getValue();
/**
*用指定的值代替这个Entry的值如果这个映射关系已经被移除了这个方法就会是undefined的(通过iterator的remove操作)
* @param value new value to be stored in this entry
* @return old value corresponding tothe entry返回就得value
* @throws UnsupportedOperationException ifthe <tt>put</tt> operation
* isnot supported bythe backing map
* @throws ClassCastException iftheclassofthe specified value
* prevents itfrom being stored inthe backing map
* @throws NullPointerException ifthe backing map doesnot permit
* null values, andthe specified value is null
* @throws IllegalArgumentException ifsomepropertyof this value
* prevents itfrom being stored inthe backing map
* @throws IllegalStateException implementations may, but are not
* required to, throw this exception ifthe entry has been
* removed fromthe backing map.
*/
V setValue(V value);
/**
* 如果参数也是一个map entry 且这两个entry代表同一段映射就返回true
* if<pre>
* (e1.getKey()==null ?
* e2.getKey()==null : e1.getKey().equals(e2.getKey())) &&
* (e1.getValue()==null ?
* e2.getValue()==null : e1.getValue().equals(e2.getValue()))
* </pre>
* This ensures thatthe <tt>equals</tt> method works properly across
* different implementations ofthe <tt>Map.Entry</tt> interface.
*
* @param o object to be compared for equality with this map entry
* @return <tt>true</tt> ifthe specified object isequalto this map
* entry
*/
booleanequals(Object o);
/**
* Returns the hash code value for this map entry. The hash code
* of a map entry <tt>e</tt> is defined to be: <pre>
* (e.getKey()==null ? 0 : e.getKey().hashCode()) ^
* (e.getValue()==null ? 0 : e.getValue().hashCode())
* </pre>
* This ensures that <tt>e1.equals(e2)</tt> implies that
* <tt>e1.hashCode()==e2.hashCode()</tt> for any two Entries
* <tt>e1</tt> and <tt>e2</tt>, as required bythe general
* contract of <tt>Object.hashCode</tt>.
*如果两个map entry相等,那么他们的hashcode相等
* @returnthe hash code value for this map entry
* @see Object#hashCode()
* @see Object#equals(Object)
* @see #equals(Object)
*/
int hashCode();
/**
* Returns a comparator that compares {@link Map.Entry} in natural order on key.
*
* <p>The returned comparator is serializable and throws {@link
* NullPointerException} when comparing an entry with a null key.
*
* @param <K> the {@link Comparable} type ofthen map keys
* @param <V> the type ofthe map values
* @return a comparator that compares {@link Map.Entry} in natural order on key.
* @see Comparable
* @since1.8
*/
public static <K extends Comparable<? super K>, V> Comparator<Map.Entry<K,V>> comparingByKey() {
return (Comparator<Map.Entry<K, V>> & Serializable)
(c1, c2) -> c1.getKey().compareTo(c2.getKey());
}
/**
* Returns a comparator that compares {@link Map.Entry} in natural order on value.
*
* <p>The returned comparator is serializable and throws {@link
* NullPointerException} when comparing an entry with null values.
*
* @param <K> the type ofthe map keys
* @param <V> the {@link Comparable} type ofthe map values
* @return a comparator that compares {@link Map.Entry} in natural order on value.
* @see Comparable
* @since1.8
*/
public static <K, V extends Comparable<? super V>> Comparator<Map.Entry<K,V>> comparingByValue() {
return (Comparator<Map.Entry<K, V>> & Serializable)
(c1, c2) -> c1.getValue().compareTo(c2.getValue());
}
/**
* Returns a comparator that compares {@link Map.Entry} by key using thegiven
* {@link Comparator}.
*
* <p>The returned comparator is serializable ifthe specified comparator
* is also serializable.
*
* @param <K> the type ofthe map keys
* @param <V> the type ofthe map values
* @param cmp the key {@link Comparator}
* @return a comparator that compares {@link Map.Entry} bythe key.
* @since1.8
*/
public static <K, V> Comparator<Map.Entry<K, V>> comparingByKey(Comparator<? super K> cmp) {
Objects.requireNonNull(cmp);
return (Comparator<Map.Entry<K, V>> & Serializable)
(c1, c2) -> cmp.compare(c1.getKey(), c2.getKey());
}
/**
* Returns a comparator that compares {@link Map.Entry} by value using thegiven
* {@link Comparator}.
*
* <p>The returned comparator is serializable ifthe specified comparator
* is also serializable.
*
* @param <K> the type ofthe map keys
* @param <V> the type ofthe map values
* @param cmp the value {@link Comparator}
* @return a comparator that compares {@link Map.Entry} bythe value.
* @since1.8
*/
public static <K, V> Comparator<Map.Entry<K, V>> comparingByValue(Comparator<? super V> cmp) {
Objects.requireNonNull(cmp);
return (Comparator<Map.Entry<K, V>> & Serializable)
(c1, c2) -> cmp.compare(c1.getValue(), c2.getValue());
}
}