Results 1 to 6 of 6

Thread: TreeMap vs Map

  1. #1
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default TreeMap vs Map

    So I have a TreeMap class that is working fine in that class I have a add method:

    Java Code:
    public class DefaultIndexImpl extends TreeMap<String, List<Integer>> implements
    		Index {
    
    	@Override
    	public void add(final String word, final int line) {
    		
    		if(containsKey(word)){
    			get(word).add(line);
    		}
    		else{
    			List<Integer> list = new LinkedList<Integer>();
    			list.add(line);
    			put(word,list);
    		}
    }
    	}
    Now I have to use this same method in a regular Map class:

    Java Code:
    public class DefaultIndexImpl implements Index {
    @Override
    	public void add(final String word, final int line) {
    		// TODO your job
    		if(contains(word)){
    			get(word).add(line);
    		}
    		else{
    			List<Integer> list = new LinkedList<Integer>();
    			list.add(line);
    			put(word,list);
    		}
    	}
    }
    The issue I am having is that Eclipse is telling me that I need to add a get and put method but in my original TreeMap class I did not have to do this. For regular Map class do I have to instantiate get() and put() or should this function be in that type?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    If you're talking about the core Map, it is not a class but an interface and would require that you implement ALL of its declared methods,... many more than just put and get. But a question for you, what is the Index interface? Does it require that you have get and put methods?

  3. #3
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Here is my Index class:

    package occurences;

    import java.util.List;
    import java.util.Map;

    public interface Index extends Iterable<Map.Entry<String, List<Integer>>> {

    void add(String word, int line);

    boolean contains(String word);

    int frequency(String word);

    List<Integer> lines(String word);

    void clear();

    int size();
    }

    So would I add put() and get() in this class?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by alpdog14 View Post
    Here is my Index class:

    Java Code:
    package occurences;
    
    import java.util.List;
    import java.util.Map;
    
    public interface Index extends Iterable<Map.Entry<String, List<Integer>>> {
    
    	void add(String word, int line);
    
    	boolean contains(String word);
    
    	int frequency(String word);
    
    	List<Integer> lines(String word);
    
    	void clear();
    
    	int size();
    }
    So would I add put() and get() in this class?
    Any class that implements this interface will have to implement all of the methods of this interface including put, get, frequency, clear, size, lines, contains,... etc.

  5. #5
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    so would i add the put and get to the interface class or the DefaultIndexImpl

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. [JAVA] TreeMap
    By watle in forum Advanced Java
    Replies: 3
    Last Post: 03-16-2011, 06:39 PM
  2. Treemap question
    By dave120 in forum New To Java
    Replies: 0
    Last Post: 10-13-2009, 03:31 AM
  3. Replies: 8
    Last Post: 07-22-2009, 02:16 AM
  4. Sorting Elements in a TreeMap
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 09:47 PM
  5. How to use treemap
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 09:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •