Results 1 to 3 of 3
  1. #1
    Domo230 is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default Understanding Iterators

    Hi hows it going..

    I'm setting up a hashmap as part of an assignment and could use some help. Theres a small aprt of it that is based around iterators. Ive been looking it up but still dont get what an iterator is?

    I am doing a chaining method hashmap. It is an array of lists. Im almost done but need to do 3 iterator methods, one that returns keys, one for values and one for entries. I dont know how to access the values I need in these LinkedLists.

    Ill include the code (minus the 2 interfaces and the main method that tests it). I would appreciate any help I can get with this.

    Java Code:
    package dsa.core;
    
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.List;
    
    public class BasicHashMap implements Map<Integer, String> {
    	List[] array;
    	int size =0;
    	int array_size=0;
    	int index;
    	public int HashFunction(Integer value){
    		int hash = value%array_size; 
    		return hash;
    	}
    	public BasicHashMap(){
    		array= new LinkedList[13];	
    		array_size=13;
    	}
    	public BasicHashMap(Integer number){
    		array =new LinkedList[number];
    		array_size=number;
    	}
    
    	class Data implements Entry<Integer, String>{
    		int datakey=0;
    		String dataString=null;
    		@Override
    		public Integer key() {
    
    			return datakey;
    		}
    
    		@Override
    		public String value() {
    
    			return dataString;
    		}
    		public Data(Integer key, String value){
    			datakey=key;
    			dataString = value;
    		}
    
    	}
    
    
    	@Override
    	public int size() {
    
    		return size;
    	}
    
    	@Override
    	public boolean isEmpty() {
    		if(size==0){
    			return true;
    		}
    		else{
    			return false;
    		}
    	}
    
    	@Override
    	public String get(Integer k) {
    		int h=HashFunction(k);
    		if(array[h]==null){
    			return null;
    		}
    		Data data = find(array[h], k);
    		if(data==null){
    			return null;
    		}
    
    		return data.value();
    	}
    	public Object getNext(LinkedList L){
    		if(L.size()!=0){
    		    
    			
    			return L.toArray();
    		}
    			return null;
    	}
    	
    	@Override
    	public String put(Integer k, String v) {
    		int h=HashFunction(k);
    		String temp = null;
    		if(array[h]==null){
    			array[h]=new LinkedList<Data>();			
    			array[h].add(new Data(k, v));
    			size++;
    		}
    		else{
    			Data data = find(array[h], k);
    			if(data==null){
    				array[h].add(new Data(k, v));
    			}
    			else{
    				temp = data.value();
    				size=size++;
    			}
    		}
    		return temp;
    	}
    
    	@Override
    	public String remove(Integer k) {
    		int h=HashFunction(k);
    		if(array[h]==null){
    			return null;
    		}
    		Data data = find(array[h], k);
    		if(data==null){
    			return null;
    		}
    		array[h].remove(data);
    		size=size-1;
    		return data.value();
    	}
        
    	@Override
    	public Iterator<Integer> keys() {
    		for(int i=0;i<array_size;i++){
    			if(array[i]!=null){
    				return array[i].listIterator();
    			}
    		}
    		return null;
    	}
    
    	@Override
    	public Iterator<String> values() {
    		for(int i=0;i<array_size;i++){
    			if(array[i]!=null){
    				return array[i].listIterator();
    			}
    		}
    		return null;
    	}
    
    	@Override
    	public Iterator<Entry<Integer, String>> entries() {
    		for(int i=0;i<array_size;i++){
    			if(array[i]!=null){
    				return array[i].listIterator();
    			}
    		}
    		return null;
    	}
    	private Data find(List<Data> list, 	Integer key){
    		for (Data data : list) {
    			if(key==data.key()){
    				return data;
    			}
    
    		}
    		return null;
    
    	}
    	public String toString() {
    		String text = "";
    		for (int i=0; i<array.length; i++) {
    			text += i + ": ";
    			if (array[i]==null) {
    				text += "empty";
    			} else {
    				if (array[i].isEmpty()) {
    					text += "empty";
    				} else {
    					for (Data data:(List<Data>) array[i]) {
    						text += "{" + data.key() + "," + data.value() + "} ";
    					}
    				}
    			}
    			text += "\n";
    		}
    		return text;
    	}
    
    }
    Moderator Edit: Code tags added
    Last edited by Fubarable; 02-11-2011 at 11:04 PM. Reason: Moderator Edit: Code tags added

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

    Default

    Moderator edit: code tags added.

    Hello, and welcome to the forum. I have edited your code and added code tags which should help make your posted code retain its formatting and be more readable.

    To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    Best of luck, and again, welcome!

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Iterators are ways to move through a collection/array/etc...in your code above, you seem to be storing your Data as a Data class (don't see the code for this but I presume its a simple class), so in the case of the function keys(), you look to be returning the iterator for the List from a bucket (which contains Data objects). I believe you want the function to return all keys (same goes for the other functions), not just those of a bucket in a wrapper object. To most easily do this, you could keep a list (or set) of keys that are entered into the Map, and just return the Iterator of this List (same goes for the values)

Similar Threads

  1. Understanding this recursion
    By Yakg in forum New To Java
    Replies: 6
    Last Post: 01-05-2011, 10:39 PM
  2. Iterators - can you assign new values to them?
    By DerekRaimann in forum New To Java
    Replies: 2
    Last Post: 12-09-2010, 08:11 PM
  3. Iterator over Iterators
    By chawlakunal in forum New To Java
    Replies: 2
    Last Post: 05-22-2010, 10:16 PM
  4. Vector understanding
    By counterfox in forum New To Java
    Replies: 6
    Last Post: 05-04-2010, 11:59 AM
  5. Understanding Vectors
    By cbrown08 in forum New To Java
    Replies: 7
    Last Post: 11-05-2007, 07:56 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
  •