How to create a dictionary in Java
Get the Learn to Code Starter Pack
Break into tech with the logic & computer science skills you’d learn in a bootcamp or university — at a fraction of the cost. Educative's Learn to Code Starter Pack is perfect for new learners hoping to launch a career in tech.
A Java dictionary is an abstract class that stores key-value pairs. Given a key, its corresponding value can be stored and retrieved as needed; thus, a dictionary is a list of key-value pairs.
The Dictionary object classes are implemented in java.util .
Creating a dictionary
The first step to creating a dictionary in Java is to choose a class that implements a “key-value pair” interface; a few examples include HashTables , HashMap , and LinkedHashMap .
The declaration and initialization of a dictionary follows the syntax below:
The code snippet below illustrates how to create a dictionary in Java and the usage of a few of the class’s methods:
Learn in-demand tech skills in half the time
Learn to Code
Data Science & ML
GitHub Students Scholarship
Early Access Courses
Try for Free
Become an Author
Become an Affiliate
Earn Referral Credits
Frequently Asked Questions
Terms of Service
Business Terms of Service
Data Processing Agreement
Copyright © 2023 Educative, Inc. All rights reserved.
- Java Arrays
- Java Strings
- Java Collection
- Java 8 Tutorial
- Java Multithreading
- Java Exception Handling
- Java Programs
- Java Project
- Java Collections Interview
- Java Interview Questions
- Spring Boot
- Explore Our Geeks Community
- Java.util.GregorianCalendar Class in Java
- Java.util.Timer Class in Java
- Java.util Interface Spliterator in Java8
- Java.util.Arrays.parallelPrefix in Java 8
- Java.util.TimeZone Class (Set-2) | Example On TimeZone Class
- Java.util.Arrays.copyOfRange() in Java
- Java.util.Arrays.deepEquals() in Java
- Java.util.Arrays.equals() in Java with Examples
- Java.util.TimerTask class in Java
- Optional Class | isPresent() function
- Java.util.PropertyPermission class in Java
- Java.util.Observable class in Java
- Java.util.UUID class in Java
- Hashtable containsKey() Method in Java
- toDegrees() and toRadians() in Java
- Java.util.TreeMap.put() and putAll() in Java
- Java.util.TimeZone Class | Set 1
- Java.util.LongSummaryStatistics class with Examples
- Java.util.TreeMap.containskey() and containsValue() in Java
Java.util.Dictionary Class in Java
The java.util.Dictionary class in Java is an abstract class that represents a collection of key-value pairs, where keys are unique and are used to access the values. It was part of the Java Collections Framework introduced in Java 1.2 but has been largely replaced by the java.util.Map interface since Java 1.2.
The Dictionary class is an abstract class and cannot be instantiated directly. Instead, it provides the basic operations for accessing the key-value pairs stored in the collection, which are implemented by its concrete subclass java.util.Hashtable.
The Dictionary class defines the following methods:
- get(Object key): Returns the value associated with the specified key in the dictionary, or null if the key is not found.
- put(Object key, Object value): Inserts a key-value pair into the dictionary. If the key already exists, its corresponding value is
- replaced with the new value, and the old value is returned. If the key is new, null is returned.
- remove(Object key): Removes the key-value pair associated with the specified key from the dictionary, and returns its value. If the key is not found, null is returned.
- size(): Returns the number of key-value pairs stored in the dictionary.
- isEmpty(): Returns true if the dictionary is empty, and false otherwise. elements(): Returns an enumeration of the values stored in the dictionary.
- keys(): Returns an enumeration of the keys stored in the dictionary.
Here’s an example of using the Dictionary class:
util.Dictionary is an abstract class, representing a key-value relation and works similar to a map. Given a key you can store values and when needed can retrieve the value back using its key. Thus, it is a list of key-value pair.
Constructors: Dictionary() Sole constructor.
The java.util.Dictionary class is a class in Java that provides a key-value data structure, similar to the Map interface. It was part of the original Java Collections framework and was introduced in Java 1.0.
However, the Dictionary class has since been considered obsolete and its use is generally discouraged. This is because it was designed prior to the introduction of the Collections framework and does not implement the Map interface, which makes it difficult to use in conjunction with other parts of the framework.
In general, it’s recommended to use the Map interface or one of its implementations (such as HashMap or ConcurrentHashMap) instead of the Dictionary class.
Here’s an example of how to use the Dictionary class:
Methods of util.Dictionary Class :
1. put(K key, V value) : java.util.Dictionary.put(K key, V value) adds key-value pair to the dictionary.
2. elements() : java.util.Dictionary.elements() returns value representation in dictionary.
3. get(Object key) : java.util.Dictionary.get(Object key) returns the value that is mapped with the argumented key in the dictionary.
4. isEmpty() : java.util.Dictionary.isEmpty() checks whether the dictionary is empty or not.
5. keys() : java.util.Dictionary.keys() returns key representation in dictionary.
6. remove(Object key) : java.util.Dictionary.remove(Object key) removes the key-value pair mapped with the argumented key.
7. size() : java.util.Dictionary.size() returns the no. of key-value pairs in the Dictionary.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Advantages of Dictionary class:
- Legacy Support: The Dictionary class was part of the original Java Collections framework and has been part of Java since the beginning. This means that if you have legacy code that uses Dictionary, you can still use it in your new code.
- Simple to use: The Dictionary class is simple to use and provides basic key-value data structure functionality, which can be useful for simple cases.
Disadvantages of Dictionary class:
- Obsolete: The Dictionary class is considered obsolete and its use is generally discouraged. This is because it was designed prior to the introduction of the Collections framework and does not implement the Map interface, which makes it difficult to use in conjunction with other parts of the framework.
- Limited functionality: The Dictionary class provides basic key-value data structure functionality, but does not provide the full range of functionality that is available in the Map interface and its implementations.
- Not type-safe: The Dictionary class uses the Object class to represent both keys and values, which can lead to type mismatches and runtime errors.
- “Java Collections” by Maurice Naftalin and Philip Wadler. This book provides a comprehensive overview of the Java Collections framework, including the Dictionary class.
- “Java in a Nutshell” by David Flanagan. This book provides a quick reference for the core features of Java, including the Dictionary class.
- “Java Generics and Collections” by Maurice Naftalin and Philip Wadler. This book provides a comprehensive guide to generics and collections in Java, including the Dictionary class.
Please Login to comment...
- Java - util package
Please write us at contrib[email protected] to report any issue with the above content
Improve your Coding Skills with Practice
- All Articles List
- 12 January 2022
- 27964 views
How to create a dictionary in Java
Dictionary data structure, dictionary class and its extension, hashmap as a dictionary.
Introduction to Java
- What Is Java? A Beginner's Guide to Java and Its Evolution
- Why Java is a Popular Programming Language?
- Top 10 Reasons Why You Should Learn Java
- Why Java is a Secure language?
- What are the different Applications of Java?
- Java for Android: Know the importance of Java in Android
- What is the basic Structure of a Java Program?
- What is the difference between C, C++ and Java?
- Java 9 Features and Improvements
- Top 10 Java Frameworks You Should Know
- Netbeans Tutorial: What is NetBeans IDE and how to get started?
- How To Set Path in Java?
- How to Write Hello World Program in Java?
- How to Compile and Run your first Java Program?
- Learn How To Use Java Command Line Arguments With Examples
- What is for loop in java and how to implement it?
- What is a While Loop in Java and how to use it?
- What is for-each loop in Java?
- What is a Do while loop in Java and how to use it?
- What is a Switch Case In Java?
Java Core Concepts
- Java Tutorial For Beginners – Java Programming Made Easy!
- What are the components of Java Architecture?
- What are Comments in Java? – Know its Types
- What are Java Keywords and reserved words?
What is a Constructor in Java?
- What is the use of Destructor in Java?
- Know About Parameterized Constructor In Java With Examples
- What are Operators in Java and its Types?
- What Are Methods In Java? Know Java Methods From Scratch
- What is Conditional Operator in Java and how to write it?
- What is a Constant in Java and how to declare it?
- What is JIT in Java? – Understanding Java Fundamentals
What You Should Know About Java Virtual Machine?
- What is the role for a ClassLoader in Java?
- What is an Interpreter in Java?
- What is Bytecode in Java and how it works?
- What is a Scanner Class in Java?
- What is the Default Value of Char in Java?
- this Keyword In Java – All You Need To Know
- What is Protected in Java and How to Implement it?
- What is a Static Keyword in Java?
- What is an Array Class in Java and How to Implement it?
- What is Ternary Operator in Java and how can you use it?
- What is Modulus in Java and how does it work?
- What is the difference between Method Overloading And Overriding?
- Instance variable In Java: All you need to know
- Know All About the Various Data Types in Java
- What is Typecasting in Java and how does it work?
- How to Create a File in Java? – File Handling Concepts
- File Handling in Java – How To Work With Java Files?
What is a Comparator Interface in Java?
- Comparable in Java: All you need to know about Comparable & Comparator interfaces
- What is Iterator in Java and How to use it?
- Java Exception Handling – A Complete Reference to Java Exceptions
- All You Need to Know About Final, Finally and Finalize in Java
- How To Implement Volatile Keyword in Java?
- Garbage Collection in Java: All you need to know
- What is Math Class in Java and How to use it?
- What is a Java Thread Pool and why is it used?
- Synchronization in Java: What, How and Why?
- Top Data Structures & Algorithms in Java That You Need to Know
- Java EnumSet: How to use EnumSet in Java?
- How to Generate Random Numbers using Random Class in Java?
- Generics in Java – A Beginners Guide to Generics Fundamentals
- What is Enumeration in Java? A Beginners Guide
- Transient in Java : What, Why & How it works?
- What is Wait and Notify in Java?
- Swing In Java : Know How To Create GUI With Examples
- Java AWT Tutorial – One Stop Solution for Beginners
- Java Applet Tutorial – Know How to Create Applets in Java
- How To Implement Static Block In Java?
- What is Power function in Java? – Know its uses
- Java Array Tutorial – Single & Multi Dimensional Arrays In Java
- Access Modifiers in Java: All you need to know
- What is Aggregation in Java and why do you need it?
How to Convert Int to String in Java?
- What Is A Virtual Function In Java?
- Java Regex – What are Regular Expressions and How to Use it?
- What is PrintWriter in Java and how does it work?
- All You Need To Know About Wrapper Class In Java : Autoboxing And Unboxing
- How to get Date and Time in Java?
- What is Trim method in Java and How to Implement it?
- How do you exit a function in Java?
- What is AutoBoxing and unboxing in Java?
- What is Factory Method in Java and how to use it?
Threads in Java: Know Creating Threads and Multithreading in Java
Join method in java: how to join threads.
- What is EJB in Java and How to Implement it?
What is Dictionary in Java and How to Create it?
- Daemon Thread in Java: Know what are it's methods
- How To Implement Inner Class In Java?
- What is Stack Class in Java and how to use it?
- What is the concept of String Pool in java?
- Java String – String Functions In Java With Examples
- Substring in Java: Learn how to use substring() Method
- What are Immutable String in Java and how to use them?
- What is the difference between Mutable and Immutable In Java?
- BufferedReader in Java : How To Read Text From Input Stream
- What are the differences between String, StringBuffer and StringBuilder?
- Split Method in Java: How to Split a String in Java?
- Know How to Reverse A String In Java – A Beginners Guide
- What is Coupling in Java and its different types?
- Everything You Need to Know About Loose Coupling in Java
Objects and Classes
- Packages in Java: How to Create and Use Packages in Java?
- Java Objects and Classes – Learn how to Create & Implement
- What is Object in Java and How to use it?
- Singleton Class in Java – How to Use Singleton Class?
- What are the different types of Classes in Java?
- What is a Robot Class in Java?
- What is Integer class in java and how it works?
- What is System Class in Java and how to implement it?
- Char in Java: What is Character class in Java?
- What is the Boolean Class in Java and how to use it?
- Object Oriented Programming – Java OOPs Concepts With Examples
- Inheritance in Java – Mastering OOP Concepts
- Polymorphism in Java – How To Get Started With OOPs?
- How To Implement Multiple Inheritance In Java?
- Java Abstraction- Mastering OOP with Abstraction in Java
- Encapsulation in Java – How to master OOPs with Encapsulation?
- How to Implement Nested Class in Java?
- What is the Use of Abstract Method in Java?
What is Association in Java and why do you need it?
- What is the difference between Abstract Class and Interface in Java?
- What is Runnable Interface in Java and how to implement it?
- What is Cloning in Java and its Types?
- What is Semaphore in Java and its use?
- What is Dynamic Binding In Java And How To Use It?
- Java Collections – Interface, List, Queue, Sets in Java With Examples
- List in Java: One Stop Solution for Beginners
- Java ArrayList: A Complete Guide for Beginners
- Linked List in Java: How to Implement a Linked List in Java?
- What are Vector in Java and how do we use it?
- What is BlockingQueue in Java and how to implement it?
- How To Implement Priority Queue In Java?
- What is Deque in Java and how to implement its interface?
- What are the Legacy Classes in Java?
- Java HashMap – Know How to Implement HashMap in Java
- What is LinkedHashSet in Java? Understand with examples
- How to Implement Map Interface in Java?
- Trees in Java: How to Implement a Binary Tree?
- What is the Difference Between Extends and Implements in Java?
- How to Implement Shallow Copy and Deep Copy in Java
- How to Iterate Maps in Java?
- What is an append Method in Java?
- How To Implement Treeset In Java?
- Java HashMap vs Hashtable: What is the difference?
- How to Implement Method Hiding in Java
- How To Best Implement Concurrent Hash Map in Java?
- How To Implement Marker Interface In Java?
- Palindrome in Java: How to check a number is palindrome?
- How to check if a given number is an Armstrong number or not?
- How to Find the largest number in an Array in Java?
- How to find the Sum of Digits in Java?
- How To Convert String To Date In Java?
Ways For Swapping Two Numbers In Java
- How To Implement Addition Of Two Numbers In Java?
- How to implement Java program to check Leap Year?
- How to Calculate Square and Square Root in Java?
- How to implement Bubble Sort in Java?
- How to implement Perfect Number in Java?
- What is Binary Search in Java? How to Implement it?
- How to Perform Merge Sort in Java?
- Top 30 Patterns in Java: How to Print Star, Number and Character
- Know all about the Prime Number program in Java
- How To Display Fibonacci Series In Java?
- How to Sort Array, ArrayList, String, List, Map and Set in Java?
How To Create Library Management System Project in Java?
- How To Practice String Concatenation In Java?
- How To Convert Binary To Decimal In Java?
- How To Convert Double To Int in Java?
- How to convert Char to Int in Java?
- How To Convert Char To String In Java?
- How to Create JFrame in Java?
- What is Externalization in Java and when to use it?
- How to read and parse XML file in Java?
- How To Implement Matrix Multiplication In Java?
- How To Deal With Random Number and String Generator in Java?
- Java Programs for Practice: Know the Simple Java Programs for Beginners
- How To Connect To A Database in Java? – JDBC Tutorial
- Advanced Java Tutorial- A Complete Guide for Advanced Java
- Servlet and JSP Tutorial- How to Build Web Applications in Java?
- Introduction to Java Servlets – Servlets in a Nutshell
- What Is JSP In Java? Know All About Java Web Applications
- How to Implement MVC Architecture in Java?
- What is JavaBeans? Introduction to JavaBeans Concepts
- Know what are the types of Java Web Services?
- JavaFX Tutorial: How to create an application?
- What is Executor Framework in Java and how to use it?
- What is Remote Method Invocation in Java?
- Everything You Need To Know About Session In Java?
- Java Networking: What is Networking in Java?
- What is logger in Java and why do you use it?
- How To Handle Deadlock In Java?
- Know all about Socket Programming in Java
- Important Java Design Patterns You Need to Know About
- What is ExecutorService in Java and how to create it?
- Struts 2 Tutorial – One Stop Solution for Beginners
- What is Hibernate in Java and Why do we need it?
- What is Maven in Java and how do you use it?
- What is Machine Learning in Java and how to implement it?
- Java Developer Resume: How to Build an Impressive Resume?
- What is the Average Java Developer Salary?
- Java Interview Questions and Answers
- Top MVC Interview Questions and Answers You Need to Know in 2024
- Top 50 Java Collections Interview Questions You Need to Know in 2023
- Top 50 JSP Interview Questions You Need to Know in 2024
- Top 50 Hibernate Interview Questions That Are A Must in 2024
Programming & Frameworks
Dictionary in Java is the abstract class that is the parent of any class which uses the key-value pair relationship. In this blog, we will learn more about the Dictionary class in Java and get familiar with the different methods. Below are the topics covered in this blog-
What is Dictionary in Java?
- Methods of util.Dictionary Class
Check the size of the dictionary
Add/ put values in dictionary, return values present in the dictionary, get method to fetch the values mapped with the key.
- Check if the dictionary is empty
- Removing key value from the dictionary
Implementation of Dictionary in Java
Dictionary is an abstract class representing a key/value storage repository that operates like Map . You can store the value in a Dictionary object and once it is stored, you can retrieve it by using its key.
public abstract class Dictionary extends Object
Methods of util.Dictionary Class
Let us have a look at a few different methods of Dictionary Class.
size() : java.util.Dictionary.size() returns the number of key-value pairs in the Dictionary
Syntax: public abstract int size()
put(K key, V value) : java.util.Dictionary.put(K key, V value) adds key-value pair to the dictionary
Syntax: public abstract V put(K key, V value)
elements() : java.util.Dictionary.elements() returns value representation in dictionary
Syntax: public abstract Enumeration elements()
get(Object key) : java.util.Dictionary.get(Object key) returns the value that is mapped with the key in the dictionary
Syntax: public abstract V get(Object key)
Check if dictionary is empty
isEmpty() : java.util.Dictionary.isEmpty() checks whether the dictionary is empty or not.
Syntax: public abstract boolean isEmpty()
Return true, if there is no key-value relation in the dictionary; else return false.
Removing key value from dictionary in Java
remove(Object key) : java.util.Dictionary.remove(Object key) removes the key-value pair mapped with the key.
Syntax: public abstract V remove(Object key)
Value in Dictionary : Edureka Value in Dictionary : Platform Value at key = 3000 : null Value at key = 1000 : Platform There is no key-value pair : false Keys in Dictionary : 1000 Keys in Dictionary : 2000 Remove : Edureka Check the value of removed key : null Size of Dictionary : 1
With this, we come to the end of this blog on the Java Dictionary Class. If you wish to learn more, check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring .
Got a question for us? Please mention it in the comments section of this “Dictionary in Java” blog and we will get back to you as soon as possible.
Recommended videos for you
- 29k Enrolled Learners
Java Certification Training Course
- 73k Enrolled Learners
Flutter Application Development Course
- 11k Enrolled Learners
Python Scripting Certification Training
- 14k Enrolled Learners
Spring Framework Certification Course
- 12k Enrolled Learners
Node.js Certification Training Course
- 10k Enrolled Learners
Python Django Certification Training Course
- 9k Enrolled Learners
Advanced Java Certification Training
- 7k Enrolled Learners
PHP & MySQL with MVC Frameworks Certifica ...
- 5k Enrolled Learners
Data Structures and Algorithms using Java Int ...
- 31k Enrolled Learners
Subscribe to our newsletter, and get personalized recommendations..
Already have an account? Sign in .
20,00,000 learners love us! Get personalised resources in your inbox.
At least 1 upper-case and 1 lower-case letter
Minimum 8 characters and Maximum 50 characters
We have recieved your contact details.
You will recieve an email from us shortly.
Computer Science 245: Data Structures and Algorithms
- Lecture Notes
- Dictionary() Constructor that creates an empty dictionary.
- Dictionary(String filename) Constructor that reads in a list of words from a file. You can assume that the file contains one word per line. Feel free to use java.util.scanner to help out with this. I only needed to use the hasNext() and nextLine() methods.
- void add(String word) Add word to the dictionary.
- boolean check(String word) Check to see if word is in the dictionary. This check should be case-insensitive!
- checkPrefix(String prefix) Check to see if the string prefix is a prefix of any word in the dictionary. This check should be case-insensitive!
- String suggest(String word)} Return an element in the dictionary as close as possible to word. If word is in the dictionary, then return word. If word is not in the dictionary, return the word that is "closest" in the dictionary.
- The methods add and check can be performed in constant time (with respect to the number of words in the dictionary -- both these operations are linear in the length of the word being added or checked)
- Suggest can be done (relatively) easily
- Determining if a string is a valid prefix of some word can be done in constant time (relative to the size of the dictionary)
To see more examples of a trie in action, take a look at the Trie Visualization .
Dictionary Implementation Details
Finding a word.
Finding a Prefix
Adding a word, removing a word, printing the dictionary, indexing child array.
- The word must be at least three letters long.
- The path traced out by the letters in the word must be connected horizontally, vertically, or diagonally. You can't skip over intervening cubes to get the next letter.
- Each cube may be used only once in a given word.
- Make sure it has at least 3 letters (many words in the provided dictionary have 2 or fewer letters!)
- Make sure it is in the dictionary
- Make sure it can be formed on the board
- playerMove The set of all words in the players guess
- computerMove An output parameter, the moves made by the computer. This should be a set of all words that can be found in the board that are not in the set playerMove
- Start with a letter, and start searching, keeping track of the word you are building up
- When you create a word (that is, the word that you've built up so far is in the dictionary) add it to a set of all generated words, and go on.
- If the word so far is not a prefix of any valid word (the Dictionary method checkPrefix is your friend here), you can stop
- Keep track of which positions you have used, so you don't reuse them. Note that only the tiles in the current word you are buiding up should be marked as used -- as you backtrack, you will need to modify your markings of which tiles are used.
- Dictionary Documentation
- BoardGUI.java The main file for the Boggle game. You shouldn't need to change this ( documentation )
- BoggleDie.java A class to handle a single Boggle die. You shouldn't need to change this
- Board.java A class to handle a boggle board. This is the file that you need to add the funtions validGuess and computerMove to. The stubs are already there, just fill them in (and add any helper methods that you need. You will need to add helper methods. ( documentation )
- Output for Boggle for original board: output
- add: 15 points
- check: 10 points
- checkPrefix: 5 points
- suggest: 15 points
- remove: 15 points
- constructors and misc. other: 5 points
- validGuess: 15 points
- computerMove: 20 points
How to use Dictionary in java.util
Best java code snippets using java.util . dictionary (showing top 20 results out of 11,043), refine search.
Assignment of Dictionary Types and Java Types
The following tables show the assignments of runtime-dependent Dictionary types and the corresponding Java types in Web Dynpro for Java .
Predefined Dictionary Types
Dictionary types for web dynpro ui elements.