• 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

Some wild manipulation with Collections

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have on my mind one interesting (or stupid) question.

Is it allowed to make such operations:

1) class HashUpdate extends HashSet implements SortedSet{}
Inside this class i imlement such methods as comparator, first, tailset, subSet, tailSet, last.
Comparator, headset i really realize. All other are empty, something like this: subSet{}.

And now... For what??
Explain. I want possibility to sort my dates in Set- not every time, but only in the final, when, i added last element (for instance, i have read buffer completely)
When i works with ArrayList, add elements which are Comparable and then call Collections.sort(List) - sorting is done only in that moment.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not simply use a SortedSet?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll have a heck of a time trying to implement SortedSet by extending HashSet. How do you plan to preserve the order?

If you want to write your own SortedSet, either as an academic exercise or because you need certain behaviors, you can do that, but you'll have to manage the backing store yourself. Extending HashSet won't work. Note that there is already a SortedSet implementation--TreeSet. You can just use that one if it suits your needs.
 
Robert Raps
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

James Boswell wrote:You cannot sort a Set as there is no concept of order.



java.util.SortedSet
java.util.TreeSet


I have known yet about TreeSet. But i don't like that it completely sorts entry every time i add new elements.
I want hybrid. So far i add items- it is HashSet with constant time adding, deleting and so on. But after- i want to sort its' entry. Understand?))
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Robert Raps wrote:
I have known yet about TreeSet. But i don't like that it completely sorts entry every time i add new elements.
I want hybrid. So far i add items- it is HashSet with constant time adding, deleting and so on. But after- i want to sort its' entry. Understand?))



Okay, so what particular question do you have? It might be possible to do this by extending HashSet, but that would break HashSet's contract--you'd be claiming to be a HashSet but you wouldn't be. For that reason and others, it would be better to delegate, or to fully implement the backing store yourself.

Note that to meet the contract for SortedSet, you'll have to sort it every time an iteration takes place, if it's not already sorted.
 
Robert Raps
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

Robert Raps wrote:
I have known yet about TreeSet. But i don't like that it completely sorts entry every time i add new elements.
I want hybrid. So far i add items- it is HashSet with constant time adding, deleting and so on. But after- i want to sort its' entry. Understand?))



Okay, so what particular question do you have? It might be possible to do this by extending HashSet, but that would break HashSet's contract--you'd be claiming to be a HashSet but you wouldn't be. For that reason and others, it would be better to delegate, or to fully implement the backing store yourself.

Note that to meet the contract for SortedSet, you'll have to sort it every time an iteration takes place, if it's not already sorted.


Thank. But could you explain why it doesn't exist mechanism like Collections.sort(List) for HashSet??
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use:
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Robert Raps wrote:Thank. But could you explain why it doesn't exist mechanism like Collections.sort(List) for HashSet??



Because Set in general does not have a defined order. There is no inherent first and last.

For a list, finding an element doesn't depend on that element being in a certain location, since we search the whole list. For a HashSet, finding an element requires it to be in the proper location. That's how we can find them in O(1) .

In a list, there is a first element and a last element, and those positions are directly tied to the implementation of the backing store. We can rearrange where things are stored in the backing store without damaging the behavior of the list.

With a general Set, however, there is no inherent first and last. And in certain implementations--in particular in HashSet--if we rearrange the positions of elements in the backing store, we can break the functionality of the Set. There's a bunch of buckets, and which element goes in which bucket depends on its hashCode(). There's no publicly defined iteration order, but obviously the implementation has to pick some order, so it probably just goes through the bukets sequentially, and then within each bucket, goes through that bucket's elements sequentially before moving on to the next bucket. We certainly could rearrange the elements in the buckets such that that sequence would produce the sorted order we want, then methods that depend on hashCode to find the bucket won't find the elements--add(), remove(), contains() would all be broken.
 
Is that a spider in your hair? Here, threaten it with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic