Saurabh
BAN USER
- 0of 0 votes
AnswersTime complexity of Hash Map storage and retrieval in Java.
- Saurabh in India| Report Duplicate | Flag | PURGE
Morgan Stanley Java Developer Java - 1of 1 vote
AnswersWhat are immutable objects? What are their Advantages? Design a immutable object with Date object as a member attribute. NOTE : Since Date is mutable, he wanted to check whether I could resolve that. I dint though :(
- Saurabh in India| Report Duplicate | Flag | PURGE
Morgan Stanley Java Developer Java Object Oriented Design
@techieDeep
suppose your application needs several such objects(hundreds of thousands) , so for increasing the throughput of your application , instead of creating an object everytime , you can look into already existing objects table and assign an 'equal' object to it.
Precisely same is done in case of String class in java. Infact, it was the main reason to make String immutable.
This was the first answer I gave. But this implementation sucks , as it blocks all requesting threads that need only to fetch object reference. To make it efficient , solution has to be that we can get Singleton object reference anytime , since its a read only meahod.
private static SingletonObject obj;
SingletonObject getReference()
{
if (obj!=null)
return obj;
else
{
synchronized(this)
{
while(obj==null)
{
obj = new SingletonObject();
}
return obj;
}
}}
Yes , but instead creating a new Class for storing Date, we could use do that in our constructer itself.
// both name and date are marked final
immutable(String name,Date date)
{
this.name = name;
// create own copy, initialize it for the date object given , could have used copy constructer if any.
this.date = new Date(date.getTime());
}
Replcarton941, Android test engineer at 8x8
My name is Lilly. I grew up in Somerset and currently live in the US. One desire that has always ...
RepI am working as a Software quality assurance analyst in Turtle's Records company. I look for flaws and weaknesses ...
Repjaydkelvey, Accountant at Apkudo
I am 34 years old and live in Houston with my family. I am working as a Human resources consultant ...
Repcharlesndouglass, Employee at VANS
I am Michael from Nantucket USA. I am working as a Power plant dispatcher in Matrix Design company. I am ...
Rephamishleeh, Blockchain Developer at ASAPInfosystemsPvtLtd
Property custodian with a reputed organization and help in keeping accurate check over the supplies and inventory of materials and ...
Repkennypmillerk, AT&T Customer service email at 247quickbookshelp
My name is Kenny and I am working as a trusted investor in Pittsburgh USA.I identify / set up a ...
@techDeep It is constant time i.e. O(1). Basically for looking into the key value pair, HashMap uses hashing which gives the precise address location of the 'bucket' which contains the Key.
- Saurabh January 28, 2013For example, hashFunction(key)= Memory Location, which you can see is not dependent on n.
PS : it is not as simple as it appears in the example, its just for the sake of simplicity, but the process is essentialy O(1).