How to implement Map in singleton class?

collections.singletonmap multiple keys
singleton multiple class loaders
singleton class in java
classic singleton java
javaworld singleton
java singleton container
thread-safe singleton c++
map of java

I have the task to make a program about Florist's. I have to make a class PriceList, which is a singleton. I have also a following given testing function main:

 public static void main(String[] args) {
 PriceList pl = PriceList.getInstance();
 pl.put("rose", 10.0);
 pl.put("lilac", 12.0);
 pl.put("peony", 8.0);

Looking on these pl.puts(), I decided to implement Map interface in class PriceList, but I don't know exactly how to do it, when I have only one object of this class and it has to be a Map. I have already written that much and don't know what to do next:

public class PriceList <String, Double>  implements Map <String, Double> {

private static PriceList instance = null;

protected PriceList() {}

public static PriceList getInstance() {
    if (instance == null)
        instance = new PriceList();
    return instance;
}

public void put(String string, double d) {
    // TODO Auto-generated method stub

}}

Thanks in advance for any help!

Your Singleton is correct! Instead of implementing the map interface, you could create a Map attribute inside your class and delegate the put method to the maps'put method. Take this example:

public class PriceList{

    private Map<String, Double> map = new HashMap<String, Double>();

    private static PriceList instance = null;

    private PriceList() {}

    public static PriceList getInstance() {
        if (instance == null)
            instance = new PriceList();
        return instance;
    }

    public void put(String string, double d) {
        map.put(string,double);       
    }
}

How to navigate the deceptively simple Singleton pattern, He concludes with a look at implementing singleton registries in order to specify singletons Logger; public class Singleton { private static HashMap map = new � AbstractMap class provides a skeletal implementation of the Java Map interface and most of the Map concrete classes extend AbstractMap class and implement required methods. Now that you have an idea of what Map interface in Java is, let’s go ahead and check out the hierarchy of Java Map.

Collections singletonMap() method in Java with Examples , Collections class is used to return an immutable map, mapping only the specified key to the public static Map singletonMap(K key, V value). Singleton design pattern is a software design principle that is used to restrict the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system.

Map implementations are usually quite sophisticated (at least the efficient ones).

If you absolutely have to use this outline (PriceList as singleton and implementing Map interface), I would suggest using an existing Map implementation under the hood:

public class PriceList <String, Double>  implements Map <String, Double> {

    private Map<String, Double> map = new HashMap<>();
    private static PriceList instance = null;

    protected PriceList() {}

    public static PriceList getInstance() {
        if (instance == null)
            instance = new PriceList();
        return instance;
    }

    public void put(String string, double d) {
        map.put(string, d);

    }}

How to store Hashmap in Singleton in android?, A singleton is a design pattern that restricts the instantiation of a class to . Notable uses include controlling concurrency and creating a central point of access for an Map; public class singleTonExample { private static final� In this pattern we ensure that the class has only one instance and we provide a global point of access to this object. The normal implementation of singleton design pattern in C#like this. public class Singleton {private static Singleton _instance; private Singleton {Console. WriteLine ("Instance created");} public void Log (string message

public class MyContext {
    private static MyContext ourInstance = null;
    private HashMap<String, String> translatedValue;

    public static MyContext getInstance() {
        if (ourInstance == null)
            ourInstance = new MyContext();
        return ourInstance;
    }

    private MyContext() {
        translatedValue = new HashMap<>();
    }

    public void addTranslatedValue(String title, String value) {
        translatedValue.put(title, value);
    }

    public String getTranslatedValue(String value) {
        return translatedValue.get(value);
    }
}
Using
MyContext myContext = MyContext.getInstance();
myContext.addTranslatedValue("Next", valueTranslated);
System.out.println(myContext.getTranslatedValue("Next"));
Result
valueTranslated

SingletonMap (Apache Commons Collections 4.4 API), public class SingletonMap<K,V> extends Object implements OrderedMap<K,V>, A Map implementation that holds a single item and is fixed size. The single� The Singleton's purpose is to control object creation, limiting the number of objects to only one. Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields. Singletons often control access to resources, such as database

Working With Design Patterns: Singleton, The singleton pattern is one of the simplest in the catalog of design patterns. Maybe all you really need is the map implementation above. The singleton pattern is a design pattern that restricts the instantiation of a class to one object. Let’s see various design options for implementing such a class. If you have a good handle on static class variables and access modifiers this should not be a difficult task. Method 1: Classic Implementation

How to make the perfect Singleton? | by Keval Patel, Instead, you are creating one public static method (commonly name as for getInstance()) to provide the single entry point to create the new instance of the class. In object-oriented programming, a singleton class is a class that can have only one object (an instance of the class) at a time. After first time, if we try to instantiate the Singleton class, the new variable also points to the first instance created.

Singleton pattern, The following is a sample implementation written in Java. public final class Singleton { private static final Singleton� No. The java.lang.Cloneable interface must be implemented by the class whose object clone we want to create. If we don’t implement Cloneable interface, clone() method generates CloneNotSupportedException. So, if we are not implementing Cloneable interface in our Singleton class, then we do not require to prevent cloning. Java will do it for us.

Comments
  • Sounds like homework... What is exactly the question? Have you tested it yet?
  • yes, it's a homework, but it's bigger than this. I have a testing class FloristsTest, which I can't change and I have to implement necessary classes to make it work. I haven't tested it yet
  • People aren't keen to help with homework here - it is for you to research, but besides that, BrunoDM gave a good answer.
  • OK, thanks for information.
  • I thought that I have to make a singleton class, and the only object of this class as to be a Map. This solution i more convenient, thanks!