Comparableインタフェースの実装を検討
実装を書き換える
compareToは自分のニーズを実現します.
どうして書き直しますか.
従来のcompareToは空方法体であり,多くのクラスがこのインタフェースを実現しており,ソートのために何をソートするかは,自分のビジネスロジックに基づいている.
例:
public final class PhoneNumber implements Cloneable, Comparable {...
public static void main(String[] args) {...
TreeSet自身がComparableインタフェースを実現
s.addはTreeSertを呼び出すJAva内部メソッド
m.putはTreeMapを呼び出します.JAva内部putメソッド
なぜなら
----------------------------------------------------------------------------------------------------------------------------------------------------->api--------------------------------------
このインタフェースは何ですか?
メソッドの詳細
compareToは自分のニーズを実現します.
どうして書き直しますか.
従来のcompareToは空方法体であり,多くのクラスがこのインタフェースを実現しており,ソートのために何をソートするかは,自分のビジネスロジックに基づいている.
例:
public final class PhoneNumber implements Cloneable, Comparable {...
public static void main(String[] args) {...
NavigableSet s = new TreeSet();
for (int i = 0; i < 10; i++)
s.add(randomPhoneNumber());
System.out.println(s);
... TreeSet自身がComparableインタフェースを実現
s.addはTreeSertを呼び出すJAva内部メソッド
return m.put(e, PRESENT)==null;
m.putはTreeMapを呼び出します.JAva内部putメソッド
/**
* Associates the specified value with the specified key in this map.
* If the map previously contained a mapping for the key, the old
* value is replaced.
*
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
*
* @return the previous value associated with key, or
* null if there was no mapping for key.
* (A null return can also indicate that the map
* previously associated null with key.)
* @throws ClassCastException if the specified key cannot be compared
* with the keys currently in the map
* @throws NullPointerException if the specified key is null
* and this map uses natural ordering, or its comparator
* does not permit null keys
*/
public V put(K key, V value) {
Entry t = root;
if (t == null) {
// TBD:
// 5045147: (coll) Adding null to an empty TreeSet should
// throw NullPointerException
//
// compare(key, key); // type check
root = new Entry(key, value, null);
size = 1;
modCount++;
return null;
}
int cmp;
Entry parent;
// split comparator and comparable paths
Comparator super K> cpr = comparator;
if (cpr != null) {
do {
parent = t;
cmp = cpr.compare(key, t.key);
if (cmp < 0)
t = t.left;
else if (cmp > 0)
t = t.right;
else
return t.setValue(value);
} while (t != null);
}
else {
if (key == null)
throw new NullPointerException();
Comparable super K> k = (Comparable super K>) key;
do {
parent = t;
cmp = k.compareTo(t.key);
if (cmp < 0)
t = t.left;
else if (cmp > 0)
t = t.right;
else
return t.setValue(value);
} while (t != null);
}
Entry e = new Entry(key, value, parent);
if (cmp < 0)
parent.left = e;
else
parent.right = e;
fixAfterInsertion(e);
size++;
modCount++;
return null;
}
なぜなら
cmp = k.compareTo(t.key);
compareTo , , Comparable , 。
----------------------------------------------------------------------------------------------------------------------------------------------------->api--------------------------------------
このインタフェースは何ですか?
java.lang
Comparable
:
T -
:
Delayed, Name, RunnableScheduledFuture, ScheduledFuture
:
Authenticator.RequestorType, BigDecimal, BigInteger, Boolean, Byte, ByteBuffer, Calendar, Character, CharBuffer, Charset, ClientInfoStatus, CollationKey, Component.BaselineResizeBehavior, CompositeName, CompoundName, Date, Date, Desktop.Action, Diagnostic.Kind, Dialog.ModalExclusionType, Dialog.ModalityType, Double, DoubleBuffer, DropMode, ElementKind, ElementType, Enum, File, Float, FloatBuffer, Formatter.BigDecimalLayoutForm, FormSubmitEvent.MethodType, GregorianCalendar, GroupLayout.Alignment, IntBuffer, Integer, JavaFileObject.Kind, JTable.PrintMode, KeyRep.Type, LayoutStyle.ComponentPlacement, LdapName, Long, LongBuffer, MappedByteBuffer, MemoryType, MessageContext.Scope, Modifier, MultipleGradientPaint.ColorSpaceType, MultipleGradientPaint.CycleMethod, NestingKind, Normalizer.Form, ObjectName, ObjectStreamField, Proxy.Type, Rdn, Resource.AuthenticationType, RetentionPolicy, RoundingMode, RowFilter.ComparisonType, RowIdLifetime, RowSorterEvent.Type, Service.Mode, Short, ShortBuffer, SOAPBinding.ParameterStyle, SOAPBinding.Style, SOAPBinding.Use, SortOrder, SourceVersion, SSLEngineResult.HandshakeStatus, SSLEngineResult.Status, StandardLocation, String, SwingWorker.StateValue, Thread.State, Time, Timestamp, TimeUnit, TrayIcon.MessageType, TypeKind, URI, UUID, WebParam.Mode, XmlAccessOrder, XmlAccessType, XmlNsForm
public interface Comparable
。 , compareTo 。
( ) Collections.sort( Arrays.sort) 。 , 。
C e1 e2 , e1.compareTo(e2) == 0 e1.equals(e2) boolean , C equals 。 ,null , e.equals(null) false,e.compareTo(null) NullPointerException。
( ) equals 。 equals ( ) , ( ) “ ”。 , ( ) equals ( ) 。
, a b , (!a.equals(b) && a.compareTo(b) == 0), add false( ), ,a b 。
, Comparable Java equals 。java.math.BigDecimal , BigDecimal ( 4.0 4.00) 。
, C :
{(x, y)|x.compareTo(y) <= 0}。
:
{(x, y)|x.compareTo(y) == 0}。
compareTo , C , C 。 equals , equals(Object) 。
{(x, y)|x.equals(y)}。 Java Collections Framework 。
メソッドの詳細
compareTo
int compareTo(T o) 。 、 , 、 。
x y sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) 。( y.compareTo(x) , x.compareTo(y) 。)
:(x.compareTo(y)>0 && y.compareTo(z)>0) x.compareTo(z)>0。
, x.compareTo(y)==0 z, sgn(x.compareTo(z)) == sgn(y.compareTo(z))。 (x.compareTo(y)==0) == (x.equals(y)) , 。 , Comparable 。 :“ : equals 。”
, sgn(expression) signum , expression 、 , -1、0 1 。
:
o - 。
:
、 , 、 。
:
ClassCastException - 。