/* * 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.CharCollection; import bak.pcj.map.IntKeyCharMap; import bak.pcj.map.IntKeyCharOpenHashMap; import bak.pcj.map.IntKeyCharMapIterator; import bak.pcj.set.IntSet; /** * This examplifies the operations of a map from int keys to char values. * * @author Søren Bak * @version 1.1 2003/5/3 */ public class IntKeyCharMapExample { public static void main(String[] args) { IntKeyCharMap s = new IntKeyCharOpenHashMap(); // Adding mappings s.put(1,'a'); // s={(1,'a')} s.put(2,'b'); // s={(1,'a'),(2,'b')} s.put(3,'c'); // s={(1,'a'),(2,'b'),(3,'c')} // Mapping keys to values char v; v = s.get(1); // v='a', s={(1,'a'),(2,'b'),(3,'c')} v = s.get(3); // v='c', s={(1,'a'),(2,'b'),(3,'c')} v = s.get(4); // v='\0', s={(1,'a'),(2,'b'),(3,'c')} // Testing for a key mapping boolean c; c = s.containsKey(1); // c=true, s={(1,'a'),(2,'b'),(3,'c')} c = s.containsKey(4); // c=false, s={(1,'a'),(2,'b'),(3,'c')} // Testing for a value c = s.containsValue('a'); // c=true, s={(1,'a'),(2,'b'),(3,'c')} c = s.containsValue('d'); // c=false, s={(1,'a'),(2,'b'),(3,'c')} // Adding elements from maps IntKeyCharMap t = new IntKeyCharOpenHashMap(); t.put(2, 'x'); t.put(3, 'c'); t.put(4, 'd'); // t={(2,'x'),(3,'c'),(4,'d')}, s={(1,'a'),(2,'b'),(3,'c')} s.putAll(t); // s={(1,'a'),(2,'x'),(3,'c'),(4,'d')} // Replacing mappings s.put(2, 'b'); // s={(1,'a'),(2,'b'),(3,'c'),(4,'d')} // Iterating over entries IntKeyCharMapIterator i = s.entries(); while (i.hasNext()) { i.next(); int key = i.getKey(); char value = i.getValue(); // Print or do something else with entry here } // Retrieving keys (view of map) IntSet keys = s.keySet(); // keys={1,2,3,4} keys.remove(4); // s={(1,'a'),(2,'b'),(3,'c')} // Retrieving values (view of map) CharCollection values = s.values(); // values={'a','b','c'} // Iterate over or do something else with values here // Finding size of map int size = s.size(); // size=3, s={(1,'a'),(2,'b'),(3,'c')} // Comparing maps t = new IntKeyCharOpenHashMap(s); c = s.equals(t); // c=true, s={(1,'a'),(2,'b'),(3,'c')}, t={(1,'a'),(2,'b'),(3,'c')} t.remove(1); // s={(1,'a'),(2,'b'),(3,'c')}, t={(2,'b'),(3,'c')} c = s.equals(t); // c=false // Clearing s.clear(); // s={} c = s.isEmpty(); // c=true } }