Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Ordered index in Collections

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 20-02-2010
Member
 
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?

Reply With Quote
  #2  
Old 20-02-2010
Member
 
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.
Reply With Quote
  #3  
Old 20-02-2010
Member
 
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?
Reply With Quote
  #4  
Old 20-02-2010
Member
 
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;
}
Reply With Quote
  #5  
Old 20-02-2010
Member
 
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);
	}
}
Reply With Quote
  #6  
Old 20-02-2010
Member
 
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Ordered index in Collections"
Thread Thread Starter Forum Replies Last Post
Collections available in FrontierVille Meditation Video Games 4 07-02-2011 10:16 AM
Does Google decides which content to index & what not to index? Uddhav Technology & Internet 5 04-03-2010 07:37 PM
ConcurrentModificationException in Collections ISAIAH Software Development 5 23-02-2010 05:42 AM
Collections in c# Jagdish Gada Software Development 5 20-01-2010 10:12 AM
Inserting an index entry into index 0 of file 25 Dharmesh Arora Operating Systems 3 16-07-2009 11:10 AM


All times are GMT +5.5. The time now is 10:11 PM.