I am wanting to do two things
Create a private instance variable which is a map
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?
*/
}
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)
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.
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.
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.
It's not clear what you're asking, but here are some points:
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>>();
}
final
FieldsYou 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With