• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Initialise an Map object without the use of HashMap<>()  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am implementing a project in which a number of notifications are stored in a Map<Double, LocNot>, where LocNot is the Type for the notifications.
I am not allowed to use any ready-made data strcuture of Java such an arraylist or a map. There is already a map class, user-defined for that reason, which includes the method:



The method is implemented in a class that implements the Map interface as :



When i am trying to insert a new value from another class, whith the use of insert , i am having an error that the Map used has not been initialized
(i am creating a static content within the class



which leads to NullPointerException on behalf of both minimap and notmap. Any ideas, on how i can procceed from this point?
 
Bartender
Posts: 2200
46
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Does it even compile? You are using a variable that is not initialized.
 
Lampros Marantos
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:

Does it even compile? You are using a variable that is not initialized.



That is the point of the question, i am now allowed to use a HashMap in order to initialize the Map...(and afterwards use Put method to insert an element.)
In such a case, how can i initialize it, so it accepts the insert method?

Thank you.
 
Paweł Baczyński
Bartender
Posts: 2200
46
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You said: "There is already a map class, user-defined for that reason, which includes the method."

You need to create an instance of this class. Look at the code of the class. Does it have public constructors? Static factory methods?
Is it the BST class? Does it implement Map interface?
 
Bartender
Posts: 19955
94
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you define a variable as being of type Map, the only time that the insert method wouldn't be allowable would be if the value of the variable was null, since no methods can be executed when there's nothing to execute them on.

The mere fact that you declared it as type Map means that anything that gets assigned to that variable has promised (via the "implements" clause of the class definition) to honor each and every one of the methods declared in the Map interface as seen in the official JavaDocs.

Now a specific implementation of Map - say a read-only Map - might implement the "put" method as simply throwing an UnimplementedOperationException, but it's still a "put" method, regardless.
 
Lampros Marantos
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are supposed to use our own created structure storage scheme for Notifications.

These are included:



I am instantiating and implementing pair in a class, such as :



For some reason, the same statement in another class gives me error regarding the Type parameters K and T used. (K and T cannot be resolved to a type) What can be the issue?
I am fairly new to generics collections so it might be something simple.  
 
Lampros Marantos
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:If you define a variable as being of type Map, the only time that the insert method wouldn't be allowable would be if the value of the variable was null, since no methods can be executed when there's nothing to execute them on.

The mere fact that you declared it as type Map means that anything that gets assigned to that variable has promised (via the "implements" clause of the class definition) to honor each and every one of the methods declared in the Map interface as seen in the official JavaDocs.

Now a specific implementation of Map - say a read-only Map - might implement the "put" method as simply throwing an UnimplementedOperationException, but it's still a "put" method, regardless.



I guessed that i should add an element to the instantiation of Map, but i get the error that the Map collection is null. So how am i able to add an element to such a oollection if the variable is null?  
 
Tim Holloway
Bartender
Posts: 19955
94
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't. A value of null means there's no collection to add anything to. Null means literally "nothing". Not there. Nowhere. Nada.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!