/* * Primitive Collections for Java. * Copyright (C) 2003 Søren Bak * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ import bak.pcj.map.ObjectKeyIntMap; import bak.pcj.map.ObjectKeyIntOpenHashMap; import bak.pcj.map.ObjectKeyIntMapIterator; import bak.pcj.IntCollection; import java.util.Set; /** * This examplifies the operations of a map from object keys to int values. * * @author Søren Bak * @version 1.0 2003/5/3 */ public class ObjectKeyIntMapExample { public static void main(String[] args) { ObjectKeyIntMap s = new ObjectKeyIntOpenHashMap(); // Adding mappings s.put("a",1); // s={("a",1)} s.put("b",2); // s={("a",1),("b",2)} s.put("c",3); // s={("a",1),("b",2),("c",3)} // Mapping keys to values int v; v = s.get("a"); // v=1, s={("a",1),("b",2),("c",3)} v = s.get("c"); // v=3, s={("a",1),("b",2),("c",3)} v = s.get("d"); // v=null, s={("a",1),("b",2),("c",3)} // Testing for a key mapping boolean c; c = s.containsKey("a"); // c=true, s={("a",1),("b",2),("c",3)} c = s.containsKey("d"); // c=false, s={("a",1),("b",2),("c",3)} // Testing for a value c = s.containsValue(1); // c=true, s={("a",1),("b",2),("c",3)} c = s.containsValue(4); // c=false, s={("a",1),("b",2),("c",3)} // Adding elements from maps ObjectKeyIntMap t = new ObjectKeyIntOpenHashMap(); t.put("b", 100); t.put("c", 3); t.put("d", 4); // t={("b",100),("c",3),("d",3)}, s={("a",1),("b",2),("c",3)} s.putAll(t); // s={("a",1),("b",100),("c",3),("d",4)} // Replacing mappings s.put("b", 2); // s={("a",1),("b",2),("c",3),("d",4)} // Iterating over entries ObjectKeyIntMapIterator i = s.entries(); while (i.hasNext()) { i.next(); String key = (String)i.getKey(); int value = i.getValue(); // Print or do something else with entry here } // Retrieving keys (view of map) Set keys = s.keySet(); // keys={"a","b","c","d"} keys.remove("d"); // s={("a",1),("b",2),("c",3)} // Retrieving values (view of map) IntCollection values = s.values(); // values={1,2,3} // Iterate over or do something else with values here // Finding size of map int size = s.size(); // size=3, s={("a",1),("b",2),("c",3)} // Comparing maps t = new ObjectKeyIntOpenHashMap(s); c = s.equals(t); // c=true, s={("a",1),("b",2),("c",3)}, t={("a",1),("b",2),("c",3)} t.remove("a"); // s={("a",1),("b",2),("c",3)}, t={("b",2),("c",3)} c = s.equals(t); // c=false // Clearing s.clear(); // s={} c = s.isEmpty(); // c=true } }