View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.collections4;
19  
20  import java.util.Iterator;
21  
22  /**
23   * Defines an iterator that operates over a {@code Map}.
24   * <p>
25   * This iterator is a special version designed for maps. It can be more efficient to use this rather than an entry set iterator where the option is available,
26   * and it is certainly more convenient.
27   * </p>
28   * <p>
29   * A map that provides this interface may not hold the data internally using Map Entry objects, thus this interface can avoid lots of object creation.
30   * </p>
31   * <p>
32   * In use, this iterator iterates through the keys in the map. After each call to {@code next()}, the {@code getValue()} method provides direct access to the
33   * value. The value can also be set using {@code setValue()}.
34   * </p>
35   *
36   * <pre>{@code
37   * MapIterator<String, Integer> it = map.mapIterator();
38   * while (it.hasNext()) {
39   *     String key = it.next();
40   *     Integer value = it.getValue();
41   *     it.setValue(value + 1);
42   * }
43   * }</pre>
44   *
45   * @param <K> The type of the keys in the map.
46   * @param <V> The type of the values in the map.
47   * @since 3.0
48   */
49  public interface MapIterator<K, V> extends Iterator<K> {
50  
51      /**
52       * Gets the current key, which is the key returned by the last call to {@code next()}.
53       *
54       * @return The current key.
55       * @throws IllegalStateException if {@code next()} has not yet been called.
56       */
57      K getKey();
58  
59      /**
60       * Gets the current value, which is the value associated with the last key returned by {@code next()}.
61       *
62       * @return The current value.
63       * @throws IllegalStateException if {@code next()} has not yet been called.
64       */
65      V getValue();
66  
67      /**
68       * Checks to see if there are more entries still to be iterated.
69       *
70       * @return {@code true} if the iterator has more elements
71       */
72      @Override
73      boolean hasNext();
74  
75      /**
76       * Gets the next <em>key</em> from the {@code Map}.
77       *
78       * @return The next key in the iteration.
79       * @throws java.util.NoSuchElementException if the iteration is finished.
80       */
81      @Override
82      K next();
83  
84      /**
85       * Removes the last returned key from the underlying {@code Map} (optional operation).
86       * <p>
87       * This method can be called once per call to {@code next()}.
88       * </p>
89       *
90       * @throws UnsupportedOperationException if remove is not supported by the map.
91       * @throws IllegalStateException         if {@code next()} has not yet been called.
92       * @throws IllegalStateException         if {@code remove()} has already been called since the last call to {@code next()}.
93       */
94      @Override
95      void remove();
96  
97      /**
98       * Sets the value associated with the current key (optional operation).
99       *
100      * @param value The new value.
101      * @return The previous value.
102      * @throws UnsupportedOperationException if setValue is not supported by the map.
103      * @throws IllegalStateException         if {@code next()} has not yet been called.
104      * @throws IllegalStateException         if {@code remove()} has been called since the last call to {@code next()}.
105      */
106     V setValue(V value);
107 }