-
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.
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
-
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 [code] above your pasted code and the tag [/code] below your pasted code like so:
Code:
[code]
// your code goes here
// notice how the top and bottom tags are different
[/code]
Best of luck, and again, welcome!
-
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)