Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
–
public class YourClass {
public static final Map<String, String> staticMap = new HashMap<>();
static {
staticMap.put("key1", "value1");
staticMap.put("key2", "value2");
Look at Guavas ImmutableMap for a constant dictionary.
private static final Map<String, String> PAIRS = ImmutableMap.of("Name1", "Value1","Name2", "Value2");
If you have lots of entries you can use the
ImmutableMap.builder()
In Java, you can use the Map interface, and choose that implementation that best fits your needs; like:
Map<String, String> pairs = new HashMap<>();
pairs.put("key1", "value1");
System.out.println("The value would be:" + pairs.get("key1");
You would also want to read about java Collection in general; and look into the corresponding tutorials. When you need a Map that can't be "changed" upon adding the initial elements; you would first populate an ordinary Map object; and then use Collections.unmodifiableMap() afterwards.
Given your comments: Java's "built-in" java.lang.Map is always about a keys/value pairs. If you need more than one value, you would be using something like Map<String, List<String>> for example. Or you can turn to 3rd party libraries, such as Guavas Multimap.
–
–
–
–
A - Explanation
First you have to determine the functionalities of the dictionary you want in an interface. Then you can use simply a HashMap or a Map Implementation in your dictionary implementation. You can store the (word, meaning) pairs in your HashMap.
I've provided both a dictionary interface and an implementation with using the HashMap collection. The demo code and output is as below;
If you want to make the dictionary persistent, then you have to write the contents of the map to a text file.
B - Dictionary Interface
package com.levent.dictionary;
public interface Dictionary {
void addToDictionary(String word, String meaning);
void removeWord(String word);
int count();
boolean isExist(String word);
String getMeaning(String word);
C - Dictionary Implementation with HashMap
package com.levent.dictionary;
import java.util.HashMap;
import java.util.Map;
public class DictionaryImpl implements Dictionary {
private int count;
private Map<String, String> wordMap;
public DictionaryImpl() {
count = 0;
wordMap = new HashMap<>();
@Override
public void addToDictionary(String word, String meaning) {
if(!isExist(word)) {
wordMap.put(word, meaning);
count++;
@Override
public void removeWord(String word) {
if(isExist(word)) {
wordMap.remove(word);
count--;
@Override
public int count() {
return count;
@Override
public boolean isExist(String word) {
if(wordMap.get(word) != null)
return true;
return false;
@Override
public String getMeaning(String word) {
String meaning = null;
if(isExist(word)) {
meaning = wordMap.get(word);
return meaning;
D - Dictionary Demo Code
package com.levent.dictionary;
public class DictionaryDemo {
public static void main(String[] args) {
Dictionary dict = new DictionaryImpl();
// add word 'gitmek'
String word = "gitmek";
boolean isExist = dict.isExist(word);
System.out.println("\"" + word + "\"" + " is exist in dictionary? : " + isExist );
System.out.println("dict count: " + dict.count());
dict.addToDictionary(word, "to go");
System.out.println("\t" + "\"" + word + "\"" + " added to dictionary");
isExist = dict.isExist(word);
System.out.println("\"" + word + "\"" + " is exist in dictionary? : " + isExist );
System.out.println("\"" + word + "\"" + ": " + dict.getMeaning(word));
System.out.println("dict count: " + dict.count());
System.out.println("\n*************************\n");
// add word 'gelmek'
word = "gelmek";
isExist = dict.isExist(word);
System.out.println("\"" + word + "\"" + " is exist in dictionary? : " + isExist );
System.out.println("dict count: " + dict.count());
dict.addToDictionary(word, "to come");
System.out.println("\t" + "\"" + word + "\"" + " added to dictionary");
isExist = dict.isExist(word);
System.out.println("\"" + word + "\"" + " is exist in dictionary? : " + isExist );
System.out.println("\"" + word + "\"" + ": " + dict.getMeaning(word));
System.out.println("dict count: " + dict.count());
System.out.println("\n*************************\n");
// remove word 'gitmek'
word = "gitmek";
dict.removeWord(word);
System.out.println("\t" + "\"" + word + "\"" + " removed from dictionary");
isExist = dict.isExist(word);
System.out.println("\"" + word + "\"" + " is exist in dictionary? : " + isExist );
System.out.println("dict count: " + dict.count());
E - Demo Output
"gitmek" is exist in dictionary? : false
dict count: 0
"gitmek" added to dictionary
"gitmek" is exist in dictionary? : true
"gitmek": to go
dict count: 1
*************************
"gelmek" is exist in dictionary? : false
dict count: 1
"gelmek" added to dictionary
"gelmek" is exist in dictionary? : true
"gelmek": to come
dict count: 2
*************************
"gitmek" removed from dictionary
"gitmek" is exist in dictionary? : false
dict count: 1
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.