/
the collectionsclusively of static methods that operate on the collectionsclusively of static methods that operate on

the collectionsclusively of static methods that operate on - PDF document

min-jolicoeur
min-jolicoeur . @min-jolicoeur
Follow
361 views
Uploaded On 2015-11-27

the collectionsclusively of static methods that operate on - PPT Presentation

1 ID: 206755

Share:

Link:

Embed:

Download Presentation from below link

Download Pdf The PPT/PDF document "the collectionsclusively of static metho..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

1 •The Collectionsclusively of static methods that operate on or return •The class contains –Polymorphic algorithms that operate on collections.–Wrappers that return a new collection backed by a specified collection.–Plus a a few other odds and ends. •All of the algorithms, provided by the Collectionsclass, take the form of static –Most of the algorithms operate on Listobjects.•maxand operate on arbitrary Collectionobjects •The sort operation uses a slightly optimized –Fast: This algorithm is guaranteed to run in nlog(n)time, and runs substantially faster on nearly sorted lists.–Stable: It does not reorder equal elements. –SortExample. •It is too bad that arrays are not collections.–You loose all of the power provided by the collection framework•The class –various methods for manipulating arrays (such as sorting and searching).–also contains methods that permit arrays to be viewed as lists.•SortExampleArrays 2 •Other algorithms provided by the Collections–Shuffling •can be used to implement games of chance or test –Data manipulation•reverse()•fill()•copy()–Searching –Finding extreme values•max()•min() •Wrapper implementations add some functionality in addition to the collection –Synchronization–Unmodifiable•Wrappers simply delegate all of their real work to a specified collection. •The unmodifiable wrappers take functionality –Any attempt to modify the collection generates UnsupportedOperationException.•The unmodifiable wrappers have two main –make a collection immutable once it is created.–Permit "second-class citizens" read-only access •keep a reference to the backing collection, but provide a reference to the wrapper. 3 public static Collection unmodifiableCollection(Collection c);public static Set unmodifiableSet(Set s);public static List unmodifiableList(List list);public static Map unmodifiableMap(Map m);public static SortedSet unmodifiableSortedSet(SortedSet s);public static SortedMap unmodifiableSortedMap(SortedMap m); •The Collections framework works with any Java class.•You must define–equals()–hashCode() –compareTo()•Do not use mutable objects for keys in a hashCode()•hashCode() returns distinct integers for –If two objects are equal according to the equals()method, then the hashCode()method applied to each of the two objects must produce the same integer–Invoking hashCode()on the same object more than once, must return the same integer. •Caveat: provided no information used in the equals comparisons has been modified. hashCode()–It is required that if two objects are unequal according to hashCode()distinct integer values. 4 Comparable•A class's natural ordering can be defined Comparableinterface.–a class's compareTo()method provides the natural comparison method–A class's natural ordering is equalsif and only if •(elemnt1.compareTo((Object)elemnt2)==0)same boolean value as: elemnt1.equals((Object)elemnt2)for every elemnt1and elemnt2of class •Name.java and TestName.java