Package tools.dictionary

Source Code of tools.dictionary.Collection

package tools.dictionary;

import tools.dictionary.Dictionary;
import tools.dictionary.Pair;
import java.util.*;

public class Collection implements Dictionary {
  private ArrayList <Pair> list=new ArrayList<Pair>();

  public ArrayList <String> clear() {
    ArrayList <String> keyList = new ArrayList <String>();
    while (list.size()>0) {
      keyList.add(list.get(0).key);
      list.remove(0);
    }
    return keyList;
  }

  public void update(String key, double n) {
    for (int i=0; i<list.size(); ++i)
      if (list.get(i).key.equals(key)) {
        list.get(i).setValue(n);
        return ;
      }
    list.add(new Pair(key,n));
    //throw new Exception("Element with the given key not found");
  }

  public int size() {
    return list.size();
  }

  public double find(String key) throws Exception{
    for (int i=0; i<list.size(); ++i)
      if (list.get(i).key.equals(key))
        return list.get(i).getValue();
    throw new Exception("Element with the given key not found");
  }

  public void delete(String key) throws Exception{
    for (int i=0; i<list.size(); ++i)
      if (list.get(i).key.equals(key)) {
        list.remove(i);
        return ;
      }
    throw new Exception("Element with the given key not found");
  }

  public void insert(Pair pair) throws Exception{
    for (int i=0; i<list.size(); ++i)
      if (list.get(i).key.equals(pair.key))
        throw new Exception("Element with the given key is already in the collection");
    list.add(pair);
  }
}
TOP

Related Classes of tools.dictionary.Collection

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.