Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a new map with constructor?

Tags:

java

I am wanting to do two things

  1. Create a private instance variable which is a map

  2. To create an empty instance in my constructor that impliments a map and assigns it to the previous private instance variable.

The Private instance I have is

private final Map<Character, SortedSet<String>> thesaurus = 
                      new HashMap <Character, SortedSet<String>>();

but how would create an instance variable in the constructor that would reference the private variable thesaurus upon the constructors creation.

For example

public class Book{

    private final Map<Character, SortedSet<String>> thesaurus = 
                           new HashMap <Character, SortedSet<String>>();

public Book(){

    super();    
    /* What do i put here as an empty instance 
     * variable that implements a map and how 
     * do i assign it to thesaurus? 
     */
}
like image 308
user386537 Avatar asked Jul 17 '10 11:07

user386537


People also ask

What is map constructor?

The Map() constructor is used to create Map objects in JavaScript. The map is a data structure that stores elements as a key, value pair. Syntax: new Map() new Map(iterable)

How do I create a map in TypeScript?

Creating a MapUse Map type and new keyword to create a map in TypeScript. let myMap = new Map<string, number>(); To create a Map with initial key-value pairs, pass the key-value pairs as an array to the Map constructor.

What is new map in JavaScript?

new Map([it]) Parameter: it - It is any iterable object whose values are stored as key, value pair, If the parameter is not specified then a new map is created is Empty Returns: A new Map object. Now lets create some Map using the Map object. Javascript.

What is map () in JavaScript?

map() creates a new array from calling a function for every array element. map() calls a function once for each element in an array. map() does not execute the function for empty elements. map() does not change the original array.


2 Answers

It's not clear what you're asking, but here are some points:

  • You can't declare an instance variable in a constructor; you have to do declare it as a member of the type (i.e. as a field).
  • You can assign values to already declared instance variables in a constructor.
  • You don't have to assign values to instance variables in a constructor; you can do it at the declarations.

When you write something like this:

public class Book{

   private final Map<Character, SortedSet<String>> thesaurus =
      new HashMap <Character, SortedSet<String>>(); 
   //...

}

Then you've declared thesaurus to be an instance variable of class Book, and you also initialized its value to be a new HashMap. Since this field is final, you can no longer set its value to be anything else (barring reflection-based attacks).

You can, should you wish to choose so, move the initialization into the constructor. You can do this even when the field is final (subject to various definite assignment rules).

public class Book{

   private final Map<Character, SortedSet<String>> thesaurus;

   public class Book {
      thesaurus = new HashMap <Character, SortedSet<String>>();
   }
   //...

}

Something like this is done sometimes when e.g. the creation of the initial value may throw a checked exception, and therefore needs to be put in a try-catch block.

Another option is to initialize fields in an instance initializer block:

   private final Map<Character, SortedSet<String>> thesaurus;

   {
      thesaurus = new HashMap <Character, SortedSet<String>>();
   }

And yet another option is to refactor said instance initializer block into a helper method:

   private final Map<Character, SortedSet<String>> thesaurus = emptyMap();

   private static Map<Character, Sorted<String>> emptyMap() {
      return new HashMap <Character, SortedSet<String>>();
   }

References

  • JLS 8.3 Field Declarations
    • 8.3.1.2 final Fields
    • 8.3.2 Initialization of Fields
  • JLS 8.6 Instance Initializers
  • JLS 16 Definite Assignment

Related questions

  • Initialize final variable before constructor in Java
  • Proper way to declare and set a private final member variable from the constructor in Java?
  • In Java, can a final field be initialized from a constructor helper?
  • Java - Can final variables be initialized in static initialization block?
  • Best Practice: Initialize class fields in constructor or at declaration?
like image 149
polygenelubricants Avatar answered Nov 05 '22 15:11

polygenelubricants


You are already initializing your thesaurus variable with a map. You can move it to the constructor, like:

public class Book
{
  private final Map<Character, SortedSet<String>> thesaurus;

  public Book(){
    this.thesaurus = new HashMap <Character, SortedSet<String>>();    
  }
}

There's no need to change, though. Either way, the instance field will be initialized every time an instance is created. Also note that you don't need super() here since it's implicit.

like image 1
Matthew Flaschen Avatar answered Nov 05 '22 13:11

Matthew Flaschen