File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Some wild manipulation with Collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Some wild manipulation with Collections" Watch "Some wild manipulation with Collections" New topic
Author

Some wild manipulation with Collections

Robert Raps
Ranch Hand

Joined: Mar 13, 2013
Posts: 30
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.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1020
    
    5

Why not simply use a SortedSet?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Mar 13, 2013
Posts: 30
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Mar 13, 2013
Posts: 30
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

Joined: Nov 09, 2011
Posts: 1020
    
    5

You could use:
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Some wild manipulation with Collections