Go Back   Gaming Gutter > Non-Gaming > Programming > Tutorials


Tutorials - Looking for programming tutorials to increase your knowledge? Do so here.

» Site Navigation
» Home
» FAQ
» Log in
User Name:

Password:

Not a member yet?
Register Now!
» Advertisement
» GG Stuff

Follow us on Twitter!

Get the GG toolbar today (for firefox only)
» Recent Threads
Go to first new post sup guise
Today 01:03 AM
by Sxc
Last post by tealeaf
Today 06:57 AM
2 Replies, 17 Views
Go to first new post What Movie Made you Cry?
04-06-2008 07:41 PM
Last post by Dr. Ogrish
Today 02:16 AM
122 Replies, 1,030 Views
Go to first new post What are you currently...
09-12-2008 03:16 PM
by Ocean
Last post by Dr. Ogrish
Today 02:15 AM
2,961 Replies, 23,947 Views
Go to first new post Sway my opinion, ladies...
Yesterday 07:55 PM
Last post by Dr. Ogrish
Today 02:12 AM
4 Replies, 26 Views
Go to first new post +rep for helping me get...
Yesterday 11:26 PM
by Kraik
Last post by Dr. Ogrish
Today 02:11 AM
2 Replies, 13 Views
Reply
 
LinkBack Thread Tools Display Modes

 [JAVA] TreeSet/TreeMap
Old 08-23-2009, 04:22 PM   #1 (permalink)
Junior Member

Undisclosed xpatx is offline
 
Join Date: Aug 2009
Posts: 6
GPoints: 339
iTrader: 0 / 0%
xpatx Is a New Face in Town
Rep Power: 0
[JAVA] TreeSet/TreeMap

Welcome to a lovely tutorial about Tree Set's and Tree Maps. This really isn't an overused utility used in Java programming. I am just posting it if you ever come by it, and are confused you can read about it!

By the way, if you didn't realize I setup my tutorials using colors to classify certain things so let me explain my color coding.

  • Orange - Method
  • Red - Function
  • Teal - topic


What is a Tree Set/Tree Map?

A Tree Map/Tree Set is basically a utility used in java.util to order a pair of keys. Unlike the Hash Map, Tree Set puts everything in ascending order either being integer-wised or alphabetical wised. A Tree Map/Tree Set can use two arguments just like a Hash Map does.

Honestly, I find Tree Sets/Tree Maps to be useless, but why not teach about them anyway? They are useful if you have plenty of data that is messy, but yet want to put everything in order by highest integer, first character, something like that.

Tree Set

Alright, let's get started with our lovely Tree Set. I will be explaining all the functions that a Tree Set can possibly use, and how to output data accordingly due to alphabetical-wise. Let's create our class for demonstration.

Code:
package jav.pat.lrn;

import jav.pat.lrn.util.Logger;

public class Main extends Logger {

    Main() {

    }

    /**
     * Instance main
     */
    public static void main(String[] args) {
        new Main();
    }
    
}
I have setup my main class, now I must instance the TreeSet with a argument of a String. In our Main() method we should add something close to this

Code:
java.util.TreeSet<String> t = new java.util.TreeSet<String>();
We have imported our TreeSet in our class, and we have gaven our TreeSet an argument of a String. This String will be used to store data, then read it. Let's start adding data, which will be using our functions from the TreeSet.

Code:
t.add("");
Well, what you see is we instanced our TreeSet with the character t from when we created our TreeSet. The add function means to add data towards the TreeSet for future use. We can use this data for storing names, or something else?

Now, let's see. We have our data added into the TreeSet, but we want to print a couple of things out. Let's view how we could possibly do something like that?

Code:
t.add("Hi");
t.add("MITB");
t.add("xpatx");
t.add("supah_<3333");
Let's say we want to print out the String with the letter that begins in the Alphabet. Well, you see in our data, the word 'Hi' is the closest one to character A. So, if we use our first() method, it will print out Hi. If we use our last() method, it will print out xpatx (also my old username on here; but got it changed). Let's view the code shall we?

Code:
t.add("Hi");
t.add("MITB");
t.add("xpatx");
t.add("supah_<3333");
        
out(t.first()); //Hi
out(t.last()); //This will be xpatx
See, our output data would be Hi, and xpatx due to the functions being used properly. Now, let's use our Tree Set used an iterator.

Code:
public static void main(String[] args) {
    TreeSet<String> t = new TreeSet<String>();
    t.add("MITB");
    t.add("coolieguy");
    t.add("xpatx");
        
    Iterator<String> i = t.iterator();
            
    while(i.hasNext()) {
        Object o = i.next();
        out(o);
    }
}
This will take our data and place it in order. I realized when I put this in my Main() method it wouldn't put the data in ascendant form, but when I declared it in main(String[] args), it worked. By the way, our data would be: coolieguy, MITB, xpatx.

Tree Map

Tree Map's are almost alike as Tree Sets. The difference is that Tree Maps allow you to get values using the key value, while Tree Sets only have a list of values.

Another thing about Tree Maps, is when you are adding data to the Map, you MUST use two arguments, which would be String and Integer. Let's take an example.

Code:
    public static void main(String[] args) {
        TreeMap<String, Integer> t = new TreeMap<String, Integer>();
            t.put("MITB", 300); //300/month
            t.put("blakeman8192", 250); //250/month
            t.put("super_", 200); //200/month
            
            Set s = t.entrySet();
            Iterator i = s.iterator();
            
            while(i.hasNext()) {
                Map.Entry me = (Map.Entry)i.next();
                System.out.print(me.getKey() + ": ");
                System.out.println(me.getValue()); 
        } 
    }
This Tree Map basically tells us in order how much super_, blakeman8192, and MITB make per month and output that data. Let's see what would their money be for next month? :o

Code:
    public static void main(String[] args) {
        TreeMap<String, Double> t = new TreeMap<String, Double>();
            t.put("MITB", new Double(300.0)); //300/month
            t.put("blakeman8192", new Double(250.0)); //250/month
            t.put("super_", new Double(200.0)); //200/month
            
            Set<?> s = t.entrySet();
            Iterator<?> i = s.iterator();
            
            while(i.hasNext()) {
                Map.Entry me = (Map.Entry)i.next();
                System.out.print(me.getKey() + ": ");
                System.out.println(me.getValue()); 
            } 
            double balance = ((Double)t.get("MITB")).doubleValue();
            t.put("MITB", new Double(balance + 300));
            System.out.println("MITB's next months balance will be: " + t.get("MITB"));             
    }
}
This gives us our data. Firstly, it tells us their current balance which is MITB: 300, Blakeman8192: 250, and Super_: 200. Then, it adds +300 towards MITB's for next month. Prints out his next balance which is 600.0.

Credits

Credits toward a great community, and some fine staff. I also thank every programming who tries to make a difference towards the 'RSPS' community. I credit myself for creating this tutorial for learning purposes.

Didn't get it? Click here to learn about TreeSets!
Didn't get it? Click here to learn about TreeMaps!

Thanks for reading!
  Reply With Quote
Reply

Bookmarks



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Powered by vBadvanced CMPS v3.1.0

All times are GMT -7. The time now is 07:00 AM.


vBulletin skin developed by: eXtremepixels
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The contents of this webpage are copyright © 2006-2008 GamingGutter.com. All Rights Reserved.

Page generated in 0.18240809 seconds (100.00% PHP - 0% MySQL) with 21 queries