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.
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!