Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: TreeMap:

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default TreeMap:

    I decided to learn a little more about TreeMap and I am having some issues and was hoping for some advice.

    package org.search.BFS;
     
    import java.util.Random;
    import java.util.TreeMap;
     
    public class Graph 
    {
    	protected class node
    	{
    		int number;
    		boolean visited;
    	}
     
    	protected TreeMap<node,Integer> tree = new TreeMap<node,Integer>();
     
    	Graph()
    	{
     
    	}
    	Graph(node s, int v) //constructor
    	{
    		tree.put(s,v);
    	}
     
    	void fillTree()
    	{
    	//fills the graph with 100 random numbers
    	Random random = new Random();
    	//create a new node in method
    	node n = new node();
     
    	for(int i=0;i<=100;i++)
    	{
    		n.number=random.nextInt(100); //add a random number to the node
    		n.visited = false;//set to false inside node
    		n=new node(); //create new node
    		tree.put(n, i);//place node into tree
    	}
     
    	}
     
    	void printTree()
    	{
    		int i =0;
    		while(!tree.isEmpty())
    		{
    			System.out.println(tree.get(i));
    			i++;
    		}
    	}
     
    	public static void main(String [] args)
    	{
    		Graph graph = new Graph();
     
    		graph.fillTree();
    		graph.printTree();
    	}
     
     
    }

     
    errors:
    Exception in thread "main" java.lang.ClassCastException: org.search.BFS.Graph$node cannot be cast to java.lang.Comparable
    	at java.util.TreeMap.compare(TreeMap.java:1188)
    	at java.util.TreeMap.put(TreeMap.java:531)
    	at org.search.BFS.Graph.fillTree(Graph.java:37)
    	at org.search.BFS.Graph.main(Graph.java:56)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: TreeMap:

    Exception in thread "main" java.lang.ClassCastException: org.search.BFS.Graph$node cannot be cast to java.lang.Comparable
    at java.util.TreeMap.compare(TreeMap.java:1188)
    at java.util.TreeMap.put(TreeMap.java:531)
    at org.search.BFS.Graph.fillTree(Graph.java:37)
    Read the API doc for the put() method. It says it will throw that exception if the node being added cannot be compared. That would mean the class needs to implement Comparable.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: TreeMap:

    for(int i=0;i<=100;i++)
    	{
    		n.number=random.nextInt(100); //add a random number to the node
    		n.visited = false;//set to false inside node
    		tree.put(n, i);//place node into tree
    		n=new node(); //create new node
    	}

    I seen that when I was reading through the errors. However I do not know how to compare them.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: TreeMap:

    I do not know how to compare them.
    You don't compare them. The TreeMap class code does that.

    Did you read the API doc for the TreeMap class?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: TreeMap:

    Yes... but the tone of your comment makes me think I should do it again. I read it with the preconceived notion that I had to do the comparing.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: TreeMap:

    Quote Originally Posted by jocdrew21 View Post
    I read it with the preconceived notion that I had to do the comparing.
    You must define how each 'node' object is compared to another - the TreeMap then uses this definition to maintain order. See post #2:

    Quote Originally Posted by Norm
    That would mean the class needs to implement Comparable.
    You implement this interface to define how each object should be ordered relative to another.

Similar Threads

  1. How TreeMap does what it does.
    By meathead in forum Java Theory & Questions
    Replies: 2
    Last Post: July 21st, 2011, 04:20 PM
  2. TreeMap vs HashMap
    By Kerr in forum Collections and Generics
    Replies: 7
    Last Post: March 10th, 2011, 10:12 AM
  3. [SOLVED] TreeSet to TreeMap Problem
    By MoniD in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 25th, 2011, 01:27 PM
  4. treemap Duplicates
    By debug in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 6th, 2010, 11:52 AM
  5. Student TreeMap
    By raphytaffy in forum Algorithms & Recursion
    Replies: 6
    Last Post: March 2nd, 2010, 12:11 AM