Associates keys with values : HashTable Map « Collections Data Structure « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Collections Data Structure » HashTable MapScreenshots 
Associates keys with values
Associates keys with values
     
// : c10:AssociativeArray.java
// Associates keys with values.
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

public class AssociativeArray {

  private Object[][] pairs;

  private int index;

  public AssociativeArray(int length) {
    pairs = new Object[length][2];
  }

  public void put(Object key, Object value) {
    if (index >= pairs.length)
      throw new ArrayIndexOutOfBoundsException();
    pairs[index++new Object[] { key, value };
  }

  public Object get(Object key) {
    for (int i = 0; i < index; i++)
      if (key.equals(pairs[i][0]))
        return pairs[i][1];
    throw new RuntimeException("Failed to find key");
  }

  public String toString() {
    String result = "";
    for (int i = 0; i < index; i++) {
      result += pairs[i][0" : " + pairs[i][1];
      if (i < index - 1)
        result += "\n";
    }
    return result;
  }

  public static void main(String[] args) {
    AssociativeArray map = new AssociativeArray(6);
    map.put("sky""blue");
    map.put("grass""green");
    map.put("ocean""dancing");
    map.put("tree""tall");
    map.put("earth""brown");
    map.put("sun""warm");
    try {
      map.put("extra""object")// Past the end
    catch (ArrayIndexOutOfBoundsException e) {
      System.out.println("Too many objects!");
    }
    System.out.println(map);
    System.out.println(map.get("ocean"));

  }
///:~


           
         
    
    
    
    
  
Related examples in the same category
1. Check if a particular key exists in Java Hashtable
2. Check if a particular value exists in Java Hashtable
3. Get Collection of Values from Java Hashtable
4. Get Set view of Keys from Java Hashtable
5. Get Size of Java Hashtable
6. Iterate through keys of Java Hashtable
7. Remove all values from Java Hashtable
8. Scan the content of a hashtable
9. Remove value from Java Hashtable
10. Sort keys in an Hashtable
11. Iterate through values of Java Hashtable
12. A simple Map implementationA simple Map implementation
13. Hash table with separate chaining
14. Hash table with linear probingHash table with linear probing
15. Hash table with double hashingHash table with double hashing
16. Working with Key-Value Pairs in a Hashtable
17. Demonstrate the Hashtable class, and an Enumeration
18. Demonstrate the HashMap class, and an IteratorDemonstrate the HashMap class, and an Iterator
19. Soft HashMap
20. MultiMap extends AbstractMap
21. Array Map extends AbstractMapArray Map extends AbstractMap
22. Demonstrating the WeakHashMapDemonstrating the WeakHashMap
23. Use treemapUse treemap
24. Sorting Elements in a TreeMapSorting Elements in a TreeMap
25. What you can do with a TreeMapWhat you can do with a TreeMap
26. A Map implemented with ArrayLists
27. Simple demonstration of HashMapSimple demonstration of HashMap
28. HashMap
29. Caching Hashtable
30. Hashtable that supports mostly-concurrent reading, but exclusive writing.
31. Lru Hashtable
32. Bucketized Hashtable
33. Custom hash table based on customized array
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.