• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

questions on Generic

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.can i add objects of class say A to NavigableSet,SortedSet without extending comparator interface ?
2.is the same true when objects are used as keys in NavigableMap,SortedMap ?
3.is the same true when objects are added in PriorityQueue ?



4.If i add objects of some class(say A) and Integer to a NavigableSet,SortedSet,then would by making class extend comparator interface,these two can be inserted to Set ?
5.

KB Book


Sorting with Comparator

Using the PriorityQueue Class

why is the PQsort class not following the same syntax as used in the GenreSort class ?





6.is there any way to view elements added to HashMap or HashSet or LinkedHashSet or LinkedHashMap in sorted order ?
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To add any objects to any sorted collection like SortedSet or PriorityQueue, your objects must implement Comparable not Comparator. If your objects don't implement Comparable, then you can use a Comparator object to sort the collection...
 
Mohit G Gupta
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks ankit.



but what about question 4,5,6

 
Mohit G Gupta
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone answer my queries
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
4. To be able to add elements to a sorted collection, the elements must be comparable to each other. If they are not, then you'll have to use a Comparator instance to sort the collection (again the elements we add to the collection need not implement Comparator, we'll use an additional Comparator instance to sort the collection).

5. In the compare method, you have to return an int value. You can use anything inside of the compare method to calculate which of the two objects is greater than the other.

6. There is no direct way to sort Maps or Sets. HashMap and HashSet are not even ordered (they don't maintain any order of elements), so you can't sort them...
 
reply
    Bookmark Topic Watch Topic
  • New Topic