I have coded some object properties in a Hashtable<Integer,Hashtable<String,Object>>
, where:
Integer
is the key for the primary Hashtable
(representing the object number) Hashtable<String,Object>
represents respectively the property name (String)
and the property value(Object)
. I would like to put all properties values into an ArrayList
(or array...) containing the values of the properties, and then would like to access every Object
. How can I do this?
if you want only a list for this purpose the Maxim's solutions is very good or you can create a custom class that implements collection and internally manage hashtable & objects. I like the second way if you need to use this in many points of your program.
for example you can modify this class and add as Element T the custom class that have all proprerties and hastable linked to a string key, and in this class add a custo mmethod for search throught key name (excuse for my english):
public class NList<T> implements Iterable<T> //, List<T>
{
private boolean synchron;
public List<T> list;
public NList(boolean synchron)
{
this(15, synchron);
}
public NList(int initialCapacity, boolean synchron)
{
this.synchron = synchron;
this.list = synchron ? new Vector<T>(initialCapacity) : new ArrayList<T>(initialCapacity);
}
public NList(Collection<T> c, boolean synchron)
{
this.synchron = synchron;
this.list = synchron ? new Vector<T>(c) : new ArrayList<T>(c);
}
public final boolean isSynchronized()
{
return synchron;
}
//@Override
public final boolean add(T element)
{
return list.add(element);
}
//@Override
public final void add(int index, T element)
{
list.add(index, element);
}
//@Override
public final T remove(int index)
{
return list.remove(index);
}
//@Override
public final List<T> subList(int fromIndex, int toIndex)
{
return list.subList(fromIndex, toIndex);
}
//@Override
@SuppressWarnings("unchecked")
public final T[] toArray()
{
return (T[])list.toArray();
}
//@Override
public final T get(int index)
{
return list.get(index);
}
//@Override
public final int size()
{
return list.size();
}
//@Override
public final boolean isEmpty()
{
return list.isEmpty();
}
//@Override
public final void clear()
{
list.clear();
}
@Override
public final Iterator<T> iterator()
{
return list.iterator();
}
//@Override
public final boolean contains(Object element)
{
return list.contains(element);
}
//@Override
@SuppressWarnings("hiding")
public final <T> T[] toArray(T[] a)
{
return list.toArray(a);
}
//@Override
public final boolean remove(Object element)
{
return list.remove(element);
}
//@Override
public final boolean containsAll(Collection<?> c)
{
return list.containsAll(c);
}
//@Override
public final boolean addAll(Collection<? extends T> c)
{
return list.addAll(c);
}
//@Override
public final boolean addAll(int index, Collection<? extends T> c)
{
return list.addAll(index, c);
}
//@Override
public final boolean removeAll(Collection<?> c)
{
return list.removeAll(c);
}
//@Override
public final boolean retainAll(Collection<?> c)
{
return list.retainAll(c);
}
//@Override
public final T set(int index, T element)
{
return list.set(index, element);
}
//@Override
public final int indexOf(Object o)
{
return list.indexOf(o);
}
//@Override
public final int lastIndexOf(Object o)
{
return list.lastIndexOf(o);
}
//@Override
public final ListIterator<T> listIterator()
{
return list.listIterator();
}
//@Override
public final ListIterator<T> listIterator(int index)
{
return list.listIterator(index);
}
}
Something like this:
List<HashTable<Integer,Object>> list = new ArrayList<HashTable<Integer,Object>>();
for order use LinkedHashTable
.
And this is how can you access HashTable
:
Hashtable<Integer, Hashtable<String, Object>> properties = new Hashtable<Integer, Hashtable<String, Object>>();
Enumeration<Integer> nmExt;
Enumeration<String> nmInt;
Hashtable<String, Object> innerHash;
int externalKey;
String interanlKey;
Object obj;
nmExt = properties.keys();
while (nmExt.hasMoreElements()){
externalKey = nmExt.nextElement();
innerHash = properties.get(externalKey);
nmInt = innerHash.keys();
while (nmExt.hasMoreElements()){
interanlKey = nmInt.nextElement();
obj = innerHash.get(interanlKey);
// .....
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.