• 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

Sorting empty

 
Ranch Hand
Posts: 353
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am reading up on TreeSet. Java documentation has the following description for one of the constructors:

TreeSet()
Constructs a new, empty tree set, sorted according to the natural ordering of its elements.


Questions:
1. With this type of constructor, the instance is empty. Does empty not mean nothing/blank/no elements? So, what are in the tree set that can sorted at this point?
2.  The TreeSet itself does not have sort() method.  I tried to check the documentation for the inheritance path of the TreeSet. I did not find the sort() method in any of the superclasses.  I did not find the sort() method in any of the interfaces the TreeSet implements. So, how does TreeSet perform sorting?  Are there things about classes and interfaces that the documentation does not mention?
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The TreeSet sorts elements automatically as they are added. There is no need to specifically call a method to do this.
 
Marshal
Posts: 79151
377
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Biniman Idugboe wrote:. . . sorted according to the natural ordering of its elements. . . .

That tells you how all elements will be sorted when they are added, rather than any special meaning for an empty set. It is actually grammatically ambiguous English, but there is more explanation if you click the blue text.
 
Biniman Idugboe
Ranch Hand
Posts: 353
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't forget that I am a newbie, just trying to have understanding of how things work in Java.  Please confirm, do these constructors have codes in them that perform these automatic sorting?
 
Saloon Keeper
Posts: 15484
363
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. The constructor just initializes the internal data structure of the TreeSet. The sorting is performed while you modify the tree, that is, inside methods such as add() and remove().
 
Biniman Idugboe
Ranch Hand
Posts: 353
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. That brings it closer to the level I can understand it.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Biniman Idugboe wrote:. . .  Please confirm, do these constructors have codes in them . . .

Look in your Java® installation folder and you will find a file called src.zip. Unzip that into a new folder and navigate its Java subfolder, then util, the open the TreeSet.java file with a text editor; that will confirm what code is in its constructor.
 
Biniman Idugboe
Ranch Hand
Posts: 353
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't punch me on the nose.  I still need some more clarifications concerning this treeset.

TreeSet(Collection<? extends E> c)
Constructs a new tree set containing the elements in the specified collection, sorted according to the natural ordering of its elements.
TreeSet(SortedSet<E> s)
Constructs a new tree set containing the same elements and using the same ordering as the specified sorted set.



But at the time of construction, the add() method could not have been called yet.  So, what's happening in these two cases?

 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you read the fuller documentation for those two constructors? Have you looked at the source? Something must be called from that constructor that adds the contents of the other Set to the current set.
 
Biniman Idugboe
Ranch Hand
Posts: 353
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bear with Sir!
The fuller documentation for public TreeSet(SortedSet<E> s) is the same as the one I pasted before unless there is another fuller documentation somewhere else I do not know how to locate.

public TreeSet(Collection<? extends E> c)
Constructs a new tree set containing the elements in the specified collection, sorted according to the natural ordering of its elements. All elements inserted into the set must implement the Comparable interface. Furthermore, all such elements must be mutually comparable: e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the set.

So, the CompareTo( ) method does the comparison between two objects at a time. Are there codes in the constructor that handle the iteration through the collection supplied? Having a mental picture of what happens at what point will help me a lot in this learning curve, but then, having the metal picture that somethings happen in the constructor  when in fact they happen outside the constructor can very well me a false sense of understanding. I just want to be clear about the constructor's role in the sorting.  The supplied collection does not have sort( ) method.  What does the sorting during construction?
 
Biniman Idugboe
Ranch Hand
Posts: 353
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apology, I meant to say "Bear with me Sir!"
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code in the constructor is very easy to read. I have already told you how to find it.
 
Biniman Idugboe
Ranch Hand
Posts: 353
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again, apology.  I cannot find the src.zip file in my installation.  I found javafx-src.zip but it does not contain anything like TreeSet.java.
I am using Java 8 installed on Windows 10 x64 in case this matters.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not having a Windows® installation to hand I cannot verify that, but I thought that src.zip was downloaded with all Java® installations.
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had forgotten about grepcode.
 
Biniman Idugboe
Ranch Hand
Posts: 353
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Took me to another world, but it will be a good world for me ultimately. Suddenly, things that appeared mysterious seem to have a humble beginning. You have no idea what this means to me.  I am just wondering if I could get my hand on an offline copy of this other world of Java.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use the search functionality and look inside C\Program Files for the src.zip file. Alternatively download a Linux Java® installation, unzip it (it is zipped, but a different algorithm, I think tar.gz) and delete everything but the src.zip file. If it doesn't work on Windows®, you can simply delete the download.
 
Biniman Idugboe
Ranch Hand
Posts: 353
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone who contributed.  I was able to download the linux version of Java installation and extracted the src folder. The java files are quite heavy on the eyes when open in Notepad. I used JEdit to open them, very nice layout. For navigability feature, the online version of the folder is still the best option.
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quick little note:
On my Windows 10 Pro system I have C:\Program Files\Java\jdk1.8.0_131, which is the root directory for Java on my system.
In that directory I have both src.zip and javafx-src.zip. I know javafx-src is more for the GUI programs and src is what was mentioned earlier in this thread.

Looks like you were really close Campbell Ritchie. Now that is what I call good, considering you are not using a Windows system and you don't have a Windows system readily available.
 
Biniman Idugboe
Ranch Hand
Posts: 353
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my C:\Program Files\Java\jdk1.8.0_131 I have the folders and files shown below.  Perhaps the 64bit version of Java installation has something to do with it or is it the case that there is something missing in my Java installation?
 
Biniman Idugboe
Ranch Hand
Posts: 353
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, page refusing to attach file.
Installation_Folder.PNG
[Thumbnail for Installation_Folder.PNG]
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hum, well I'm use 64 bit as well on my 64 bit system. From the command line I get the following:

java -version
java version "1.8.0_141"
Java(TM) SE Runtime Environment (build 1.8.0_141-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.141-b15, mixed mode)


That being said, I've been known to "play" around with my system and I would be considered more of a power user then an average user.

If you don't like how Windows searches for files (I don't) then you could look for the file by entering the following commands in the command prompt:

Line 1: Brings you to the root drive e.g. C:\
Line 2: Looks for all the files name src.zip in all sub directories from the current location.
Please note that you will get anything named src.zip including files that you may not be interested in.
Also note that this could take a few moments or more, depending on how fast and powerful your system is and how many files names it needs to look at.
 
Biniman Idugboe
Ranch Hand
Posts: 353
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is what I have in my system:

C:\Users\xxxx>java -version
java version "1.8.0_141"
Java(TM) SE Runtime Environment (build 1.8.0_141-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.141-b15, mixed mode)

C:\Users\xxxx>cd\

C:\>dir src.zip /b /s
C:\$Recycle.Bin\S-1-5-21-1889553753-4065283468-4012672277-1001\$R2N58J7.tar\jdk-8u144-linux-x64\jdk1.8.0_144\src.zip
That one you can see in the Recycle bin is the linux copy I downloaded a few hours ago.  So, it looks like the src.zip was not installed along with Java in my system.
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Biniman Idugboe wrote:So, it looks like the src.zip was not installed along with Java in my system.


I'd have to agree with you on that.
It does look like you are in the correct location with the screenshot provided as javafx-src is there.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When installing the JDK, there is a page in the installer that asks it you want to install the source code or not (indirectly: it give you the option of installing the JRE too).
 
reply
    Bookmark Topic Watch Topic
  • New Topic