title: How can I iterate through an array list of pairs to retrieve the value of a specified key?
body: <p>I have been trying to write a function Retrieve in Java that accepts a key and returns a value from an array list of Pairs. Any suggestions?      </p>
  
  <pre><code>  ArrayList&lt;Pair&lt;K,V&gt;&gt; set_of_pairs = new ArrayList&lt;Pair&lt;K,V&gt;&gt;();
    public void insert(K key, V value) {
      Pair&lt;K,V&gt; pair = new Pair&lt;K,V&gt;(key,value);
      set_of_pairs.add(pair); 
    }
  
    public void traverse(Visitor&lt;K,V&gt; visitor) {       
    }
  
    public V retrieve(K key) {
     int i = 0;
     if (set_of_pairs.containsKeys(key) == false) {
        return null;
     }
     else {
        for(Pair&lt;K,V&gt; set_of_pairs: pair) {
           if (pair.getKey() == key) {
              return pair.getValue();
           }
        }      
      }
    }
  </code></pre>