JAVA Collections

18 views
Skip to first unread message

vivek joshi

unread,
Oct 9, 2012, 4:13:50 AM10/9/12
to newidea_or...@googlegroups.com
Hi All

I have a clarification I need. In Java, LinkedHashMap is used to retain the order in which inserts were made. Now if I create a clone of a LinkedHashMap, will the original insertion order be retained in the clone? Please note that LinkedHashMap doesn't overwrite the clone() method of HashMap and uses clone method defined in HashMap.

Any help here will be highly appreciated.


Thanks in advance!

Shailendra shail

unread,
Oct 10, 2012, 3:16:11 AM10/10/12
to newidea_or...@googlegroups.com
I'd like to request ankur gupta(makemytrip.com) and Nishant Gupta(Oracle.com) to give better answer for this question..

Neo

unread,
Oct 10, 2012, 6:34:21 AM10/10/12
to newidea_or...@googlegroups.com
Hi,

I would like to ask one thing here,

How exactly are you cloning the LinkedHashMap<> ? by what line of code...

it would help me provide a better answer.

--
You received this message because you are subscribed to the Google
Groups "Technical Discussion" group.
To post to this group, send email to
newidea_or...@googlegroups.com
To unsubscribe from this group, send email to
newidea_or_techn...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/newidea_or_technical?hl=en

Shailendra shail

unread,
Oct 10, 2012, 7:22:34 AM10/10/12
to newidea_or...@googlegroups.com
I agree with Neo, After looking code only it can be decided. Basically cloning is happening in tow ways. shadow cloning and deep cloning, if you have don deep cloning then order will be same other wise it could differ. So plz a snapshot of code so that it can be analyze better.  

-Shail
--
Shailendra Kumar shail

vivek joshi

unread,
Oct 11, 2012, 11:55:55 PM10/11/12
to newidea_or...@googlegroups.com
Hi All

Thanks for your swift response!
Here is how I am using LinkedHashMap



LinkedHashMap<String,String> map=new LinkedHashMap<String,String>();


// Here I am adding some data to the map


LinkedHashMap<String,String> clone=(LinkedHashMap<String, String>) map.clone();

//Here I am adding some data to clone

//To retrieve the data stored in clone I am using following approach
Set<String> keys=clone.keySet();
for(String key:keys){

     //Here comes BL to process this data
     //and as per my BL, data should be returned in same order as it was added
}




I hope this helps you get a better understanding of how I am using cloning. If you need any further information, do let me know.



Shailendra shail

unread,
Oct 12, 2012, 12:32:18 AM10/12/12
to newidea_or...@googlegroups.com
Vivek,

I think you got confused with the object name (map) :-) , Here you are not using Map at all, Everywhere you have used LinkedHashMap so  If you will create clone of LinkedHashMap of course order will be same. Not sure if you have some other thought in your mind but after looking code I actually didn't get what is your confusion.

Let me ask you one question are you trying to cast a clone of HashMap into LinkedHashMap?
HashMap<String,String> data=new HashMap<String, String>();

and trying to cast like

LinkedHashMap<String,String> data1= data.clone();

if this is case then your doubt is valid. Then I can give you correct answer.

-Shail



--
You received this message because you are subscribed to the Google
Groups "Technical Discussion" group.
To post to this group, send email to
newidea_or...@googlegroups.com
To unsubscribe from this group, send email to
newidea_or_techn...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/newidea_or_technical?hl=en



--
Shailendra Kumar shail

vivek joshi

unread,
Oct 12, 2012, 1:35:45 AM10/12/12
to newidea_or...@googlegroups.com
Hi Shailendra

Here is my confusion:
If you see javadocs of LinkedHashMap  ( http://download.java.net/jdk7/archive/b123/docs/api/java/util/LinkedHashMap.html ),  it clearly mentions that the clone() method of LinkedHashMap is inherited from HashMap (LinkedHashMap doesn't override clone() method of HashMap). So even when I write


LinkedHashMap<String,String> map=new LinkedHashMap<String,String>();
LinkedHashMap<String,String> clone=(LinkedHashMap<String, String>) map.clone();

the clone() method being invoked is the one implemented by java.util.HashMap . Hence I am  confused whether order of "map" will be retained in the "clone" map (the whole thing depends on the logic of java.util.HashMap.clone()  method and that is where I need clarification)

Hope this clarifies my doubt. Please correct me if my understanding is wrong somewhere.






Shailendra shail

unread,
Oct 12, 2012, 1:49:48 AM10/12/12
to newidea_or...@googlegroups.com
That's fine Josi,

So answer is yes. Order will be same, Since clone method does not change the logic of insertion and retrieval of linked hash map.
However the order of key is always decided at insertion time, so when you are inserting the index is being decided and data are stored at exact location(see below put code of LinkedHashMap).

   int hash = hash(key.hashCode());
        int i = indexFor(hash, table.length);
        for (Entry<K,V> e = table[i]; e != null; e = e.next) {
            Object k;
            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
                V oldValue = e.value;
                e.value = value;
                e.recordAccess(this);
                return oldValue;
            }
        }

        modCount++;
        addEntry(hash, key, value, i);


So you can clone anytime order will remain same.. :)

Hope this is clear..

-Shail

--
You received this message because you are subscribed to the Google
Groups "Technical Discussion" group.
To post to this group, send email to
newidea_or...@googlegroups.com
To unsubscribe from this group, send email to
newidea_or_techn...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/newidea_or_technical?hl=en



--
Shailendra Kumar shail

vivek joshi

unread,
Oct 12, 2012, 4:30:50 AM10/12/12
to newidea_or...@googlegroups.com
Thanks for the swift reply Shailendra

Missing working with you :)

Shailendra shail

unread,
May 28, 2014, 6:46:01 AM5/28/14
to newidea_or...@googlegroups.com
Hi Friends,

Any one who has worked on Eclipse Plugins for Hooking Save action? If so please help (I am trying to get this done but till now I am unable to do this).

Regards,
Shailendra

Neo

unread,
May 28, 2014, 1:02:35 PM5/28/14
to newidea_or...@googlegroups.com
Quit  Java 2 years ago... :(

Regards,

NISHANT GUPTA / Master of Computer Applications
8050041001/ ccsC...@gmail.com

Facebook Twitter Google Plus Linkedin

-- If you could reason with Religious people, there would be no religion.



--
--
You received this message because you are subscribed to the Google
Groups "Technical Discussion" group.
To post to this group, send email to
newidea_or...@googlegroups.com
To unsubscribe from this group, send email to
newidea_or_techn...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/newidea_or_technical?hl=en

---
You received this message because you are subscribed to the Google Groups "Technical Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to newidea_or_techn...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

vijay kumar

unread,
May 29, 2014, 5:50:41 AM5/29/14
to newidea_or...@googlegroups.com
Hi Shail g

           sorry shail, Not work that but may be this link help u

Regards
vijay
Reply all
Reply to author
Forward
0 new messages