Results 1 to 6 of 6

Thread: Ordered index in Collections

  1. #1
    Join Date
    Dec 2009
    Posts
    213

    Ordered index in Collections

    Hello,
    I try to set up a data structure which would have the following features:
    - Key / value for direct access via get (key)
    - The structure must be ordered (and order does not change with time):
    key1 -> value1
    key2 -> value2
    Key3 -> vlaeur3
    - Access via index also get (i)
    I work in a small enthronement and have access to:
    - Interfaces: Collection, List, Map, Set
    - Classes: AbstractCollection, AbstractList, AbstractMap, AbstractSet, ArrayList, Dictionary, HashMap, HashSet, Hashtable, Vector
    well on any class does not meet all these requirements, it is necessary that I implement my own structure by extending / implementing something exists: In your opinion what is the best way?

  2. #2
    Join Date
    Nov 2009
    Posts
    333

    Re: Ordered index in Collections

    Hello,
    See if you can use LinkedHashMap which allows you to create map preserving the order of addition of components. Then you can very well use entrySet (). toArray () if it is not too penalizing in access time. I think you can go with the following and then create a new table to access your program , this will be easy and faster to access.

  3. #3
    Join Date
    Nov 2009
    Posts
    583

    Re: Ordered index in Collections

    Hello,
    This requires that you work with two hashtables. The first contains your data, the second is a table that associates an index (order came from your element) the value of the corresponding key in the first table. I know if I am not very clear? A small example to understand:
    Code:
    Hashtb tb1 = new HashTable();
    Hashtb tb2 = new HashTable();
     
    for (int i = 0 ; I <NumElements; i + +){
    
      tb1.could(key, value);
      Table2.could(new Integer(i), key);
    }
    String value1 = tb1.get( Table2.get(new Integer(0)));
    I think it answers your problem?

  4. #4
    Join Date
    Nov 2009
    Posts
    518

    Re: Ordered index in Collections

    Hello,
    Why not simply use two's List? one for keys and another for values to get (Key) can be translated.
    Code:
    try {
        return lsval.get(listCles.indexOf(Key);
    } catch(ArrayOutOfBoundsException e) {
        return null;
    }

  5. #5
    Join Date
    Dec 2009
    Posts
    213

    Re: Ordered index in Collections

    Hey,
    This is my updated code.
    Code:
    Public class enchasht {
     
    	private Hashtable ori;
    	private Hashtable ordmp;
    	
    	Public enchasht() {
    		Original = new Hashtable();
    ordmp = new Hashtable();
    	}
    	
    	Public void could(Object key, Object val) {
    		ordmp.could(new Integer(ori.size()), Key);
    ori.could(key, val);
    	}
    	
    	Public Object get(Object key) {
    		return ori.get(key);
    	}
    	
    	Public Object get(int i) {
    		Object key = ordmp.get(new Integer(i));
    Object val = null;
    		try {
    			val = ori.get(key);
    		} catch(NullPointerException npex) {}
    		
    		return val;
    	}
    	
    	Public void clear() {
    		ordmp.clear();
    ori.clear();
    	}
    	
    	Public Object getFirstElement() {
    		return this.get(0);
    	}
    	
    	Public GetLastElement Object() {
    		return this.get(ori.size()-1);
    	}
    }

  6. #6
    Join Date
    Nov 2009
    Posts
    446

    Re: Ordered index in Collections

    Hello,
    The key is not stored twice in memory (there is not no new () or clone ()). Do not forget when passing java parameters to a function is by reference not by value (the notion of pointer and memory address). Therefore the value of the object key is not duplicated . The access is in the worst case (ie retrieve the last element) with a linear list, it is consistent with a HashTable. If you got a lot of data, uses a HashTable otherwise the list. You're probably right about the fact that a HashTable is more space in memory than a list, because the "new Integer (i). I hope to have you provide some clarification. Otherwise, your class is very well

Similar Threads

  1. Collections available in FrontierVille
    By Meditation in forum Video Games
    Replies: 4
    Last Post: 07-02-2011, 09:16 AM
  2. Does Google decides which content to index & what not to index?
    By Uddhav in forum Technology & Internet
    Replies: 5
    Last Post: 04-03-2010, 06:37 PM
  3. ConcurrentModificationException in Collections
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 23-02-2010, 04:42 AM
  4. Collections in c#
    By Jagdish Gada in forum Software Development
    Replies: 5
    Last Post: 20-01-2010, 09:12 AM
  5. Inserting an index entry into index 0 of file 25
    By Dharmesh Arora in forum Operating Systems
    Replies: 3
    Last Post: 16-07-2009, 10:10 AM

Posting Permissions

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