-2

I am studying collections in Java, however, some collections are quite confusing.

According to my understanding, I have prepared the Override chart.Need your help to correct it and understand it properly.

Collections table Please correct if wrong.

One more question about TreeMap. I read about TreeMap, it sorts elements by KEYS using natural or custom order specified by Comparator. However, in some tutorials, it is specified that it can be sorted by values instead of keys. Is it correct or should we sort by keys only?

Update : Sorry if my question feels bit ambiguous. I am assuming I am putting my custom object of type Customer(java class) in each collection. In key value pair collections, I am using Customer as key.

swapyonubuntu
  • 1,952
  • 3
  • 25
  • 34
  • 1
    There are no "compulsory" overrides necessary when extending any concrete class, as all methods already have implementations. You would need to override some methods if you were _extending_ a collection class, but which methods would depend on why you were extending it and how you were planning to use it. However, in general you should not be extending collection classes, but using instances of collections in your own classes. Your question is not really answerable as it seems to be based on a flawed premise. – Jim Garrison Nov 23 '16 at 05:55
  • If I have a custom object, that I use as a key, value or element of a collection is the question valid at that point? – swapyonubuntu Nov 23 '16 at 05:59
  • NO. That is why the collections are generic. If you are unaware of generics it is time to stop and study some online Java tutorials in depth. – Jim Garrison Nov 23 '16 at 06:01
  • I am totally aware about generics. I am asking question because, eg: If i want to use hashmap of Customer class, as specified in book its required to override hashcode and equals methods to work properly. Accordingly , I dont know what overrides are required for other collections – swapyonubuntu Nov 23 '16 at 06:04
  • I hope i am clear, Its not at all about generics. – swapyonubuntu Nov 23 '16 at 06:05
  • The overrides are required on the classes that you will be using as _keys_, not the collections themselves. And even then, only if the existing `hashCode()` and `equals()` are not appropriate for the class. Again, I strongly recommend you go back through some tutorials on collections as your understanding seems to be flawed. – Jim Garrison Nov 23 '16 at 06:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128780/discussion-between-swapyonubuntu-and-jim-garrison). – swapyonubuntu Nov 23 '16 at 07:20
  • After digging through the documentation, I have come upto this conclusion. https://s21.postimg.org/rxpjm47kn/collections.png. Note : Colours are just to distingush text, they do not have any specific meaning. – swapyonubuntu Nov 23 '16 at 17:11

1 Answers1

1

Which methods are compulsory to override for a collection with custom Objects as key, value or element ?

There is no compulsion to override the methods of the collection classes. Collection API provides many classes like List (preserves the order of elements), Set (eliminates duplicates), Map (stores elements as key value pairs), etc.. which you can choose and use them directly depending upon your actual use case.

For TreeMap, Is it correct or should we sort by keys only ?

TreepMap sorts the elements by ordering the keys. If you are looking for ordering the elements by value, you can do that in many ways for which you can look here.

Again, there is no compulsion that you need do the ordering of the Map by key or value, it depends upon your project/business requirement.

UPDATE:

If I want to use hashmap of Customer class, as specified in book its required to override hashcode and equals methods to work properly.

Yes, for custom classes like Customer, you need to override equals() and hashcode() so that the collection classes know how two Customer objects are equal. For example, when you are working with Set, to eliminate duplicates, Set needs to find how two Customer objects are equal so that it can preserve only one object.

Also, on the other side, if you want to order the elements (to be stored inside a collection), you need to override compare from Comparator or compareTo() from Comparable.

To summarise, refer the below basics:

override equals() and hashcode() whenever there is a need to find the object equality

override compare() or compareTo() for ordering requirements

Community
  • 1
  • 1
Vasu
  • 21,832
  • 11
  • 51
  • 67
  • great explanation. I want same for all Collections. Eg: I use Customer class in TreeSet, do I need to override equals as well as compare? As, equals to eliminate duplicates and compare() to sort? – swapyonubuntu Nov 23 '16 at 06:30
  • Yes, you are right, if your key is custom class unlike a plain String object. – Vasu Nov 23 '16 at 06:43
  • Are you clear now ? – Vasu Nov 23 '16 at 06:52
  • Yes. For other collections, ill figure out . Thanks. – swapyonubuntu Nov 23 '16 at 06:55
  • I do not need to override equals, internally TreeSet and TreeMap use compare and compareTo to check equality. TreeSet and TreeMap fails to obey the general contract of the Set interface. https://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html – swapyonubuntu Nov 23 '16 at 17:15
  • It is not must, but it is a best practice to do that, look here http://stackoverflow.com/questions/7229977/java-why-it-is-implied-that-objects-are-equal-if-compareto-returns-0 – Vasu Nov 23 '16 at 17:36