CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2007
    Posts
    273

    choose the right collection

    Hello,
    I have:
    Code:
    Map<Var, Integer = new LinkedHashMap<Var, Integer>();
    //with Var my own object
    I need a collection that:
    - no permits duplicates; that is fine
    - keep the order of insertion //fine too
    - I can retrive an element by its position //no fine!
    - I can retrive the value by the key //fine

    WHich is the best? I thought arrayList and every time check if the new element I want insert within, is not within yet. The problem is that I must to many extraction by position, for this reason I thought it wasn't the best. actually I thinking to store them by Integer key instead of Var since even the Intger give me the position. The problem is that in other part of the program I'm accessign to it by the Var key

    suggestions? thanks
    Last edited by mickey0; September 6th, 2010 at 06:26 AM.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: choose the right collection

    WHich is the best? I thought arrayList
    You can't really compare a Map and a List as there are used for different purposes. How are you inserting your key and value into the List?

    You need to decide what your primary actions on this collection are before trying to work out which one (or combination of them) to use.

    For example:
    If you are doing lots of insertion/removals other than just at the end of the list then using a LinkedHashMap is a good idea. If you mainly referencing objects by the value object then it isn't.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Jul 2007
    Posts
    273

    Re: choose the right collection

    Code:
    Map<Var, Integer> map = new LinkedHashMap<Var, Integer>();
    it's a map; with put I insert key; map.put (new Var(), new Integer(num));

    1. The primary operation is insert but now I realised that I must do the others operations I said too;

    2. Do I need to override hashCode and equals of Var? (var contains 2 strings fileds).


    2. Another problem: I overrode hashCode and equals of MyObject to put the value inside a
    Code:
    Set<MyObject, Integer> mymap = new LinkedHAshSer<MyObject, Integer>();
    Code:
    class MyObject {
         Map<Var, Integer> internalMap = new LinkedHashMap<Var, Integer>();
         String name;
    }
    when I do: mymap.put( new Object(), new Integer(num) ); All seems to work fine;

    But in another part of the program I need to iterate on mymap and put it in a
    Code:
    Set<MyObject> myset = new HashSet<MyObject>();
    but now two object like these:
    Code:
    Obeject1 : <var1, 20> //1
    Object2 : <var1, 5>   //2
    are for me identic; so I don't want put them twice withint myset; more or less I think I need two version of equals and hasCode.
    Is there a way to solve this?

    thanks

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: choose the right collection

    it's a map; with put I insert key; map.put (new Var(), new Integer(num));
    Yes I can see that is a map but your original statement said "WHich is the best? I thought arrayList " so I asked how you were using the arraylist.
    1. The primary operation is insert but now I realised that I must do the others operations I said too;
    Ok, so use the LinkedHashMap. Determining which object type to use as the key is down to which one you want to ensure is unique as maps only test keys for duplicates. Now determine if the value object lookup is infrequent enough for an inefficient trawl through the map to not be a problem. Of course if the map is small this inefficiency is not really relevant.

    If the value object lookup is so inefficient it really is a problem and I mean only after using profiling tools to prove it's an issue there are ways around it.

    Another problem: I overrode hashCode and equals of MyObject to put the value inside a
    The code you have shown is confusing and wrong. If you are going to post code make sure it compiles and clearly shows the point you are trying to make.
    1. The first code snippet has typos in it and the generics are for a map but you are creating a set.
    2. The code for MyObject doesn't have overridden hashcode and equals methods
    3. How do these code snippets relate to each other?


    are for me identic; so I don't want put them twice withint myset; more or less I think I need two version of equals and hasCode.
    Is there a way to solve this?
    I've no idea what you are talking about. Give all the classes involved sensible names, clearly explain which class is in which collection, and what makes two instances of a class equal.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured