如何在Java中创建entrySet()方法?

时间:2017-07-05 21:58:31

标签: java

我试图找出如何在HashMapOpen类中实现entrySet()方法。 HashMapOpen类是一个表示哈希表使用开放寻址的类,它实现了Map接口。哈希表表示为Entry对象的数组,

Entry<K,V>[] data = new Entry[5]. 

Entry类实现

Map.Entry<K,V> and has 2 variables: K key and V value.

Entry类包含构造函数Entry(K键,V值),键和值的getter和setter方法,toString()方法,返回如下字符串

<19, Tarzan> or <key,value>

并覆盖equals(Object obj)方法。

HashMapOpen有3个私有变量:

private Entry<K, V>[] data = new Entry[5];
private final Entry<K, V> DELETED = new Entry(null, null);
private int size = 0;

HashMapOpen的方法如下:

// Get the value by using the key, return null if the key is not present  
     V get(K key);

// Check if the Map empty or not. Return True if the map contains no key-value set
     boolean isEmpty();

// Add the value to the map with its associated key, return the previous value, return null if there
// was no mapping for the key
     V put(K key, V value);

// Remove the mapping for this key, return the previous value, return null if there is no mapping for this key
     V remove(K key);

// Return the size of the key
     int size();

// toString method which returns a string represent the entry such as 

[0] - <15, Johnson>
[1] - null
[2] - <12, Carry>
...
     String toString();

之后我尝试添加entrySet()方法。方法entrySet在Map中创建条目的集合视图。这意味着方法entrySet返回一个实现Set接口的对象 - 即一个set。

例如,如果在主要方法中我说:

HashMapOpen<Integer,String> map = new HashMapOpen<>();
    map.put(24,"D");
    map.put(15,"A");
    map.put(46,"B");
    map.put(31,"C");

    Set<Map.Entry<Integer, String>> test = map.entrySet();
    System.out.print("Initial Value Key Set: " );
    for(Map.Entry<Integer, String> e: test) 
        System.out.println(e);

我希望输出结果如下:

Initial Value Key Set: <15, A> <46,B> <31, C> <24,D>

这是我的entrySet()方法的代码:

/ *这是我用来测试Java的一些功能的特殊代码段 entrySet方法返回Map到Set实现中的条目视图 * /

public Set<Map.Entry<K,V>> entrySet(){
        return new EntrySet(); // Return a class ??? Uhm! interesting...
                               // One explanation is that the EntrySet class implements iterator method
                               // which is almost the same way of use as toString method, which means that
                               // no need to use reference.method to run, only need reference.
    }

    // AbstractSet provides a complete implementation of the Set interface except for the size and iterator methods
    private class EntrySet extends AbstractSet<Map.Entry<K,V>>{

        public int size(){
            return size;
        }

        public Iterator<Map.Entry<K,V>> iterator(){
            return new SetIterator();
        }
    }

    private class SetIterator implements Iterator<Map.Entry<K, V>>{
        private Entry<K,V> next; // Next entry to return
        private int lastItemReturned; // Index of the current entry
        private Entry<K,V> current; // Current Entry

        public SetIterator(){
            if(size > 0){
                Entry<K,V>[] table = data;
                while(lastItemReturned < table.length && (next = data[lastItemReturned++]) == null)
                    ;
            }
        }
        @Override
        public boolean hasNext() {
            // TODO Auto-generated method stub
            return next != null;
        }

        @Override
        public java.util.Map.Entry<K, V> next() {
            // TODO Auto-generated method stub
            if(next == null)
                throw new NoSuchElementException();

            Entry<K,V>[] table = data;
            Entry<K,V> e = table[lastItemReturned + 1];

            if((next = e) == null){
                while(lastItemReturned < table.length && (next = table[lastItemReturned++]) == null)
                    ;
            }
            current = e;
            return e;
        }

        public void remove(){

        }
    }

当我运行程序或我的主要方法时,它会继续打印

<15, A>
<15, A>
<15, A>
<15, A>
...

0 个答案:

没有答案