0

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)
  • Every 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?

3
  • are your properties in order and with no gaps? (0,1,2,3,4) or have random numbers? Commented Sep 14, 2013 at 10:58
  • 1
    Why not create one object which contains name and Object elements? Hashtable<Integer, NameObject> Commented Sep 14, 2013 at 11:10
  • i put every Hashtable<String,Object> into the main Hashtable every time i get an object of witch i have to save the properties, so they are ordered as founded. Commented Sep 14, 2013 at 13:43

2 Answers 2

1

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);
     }
  }
Sign up to request clarification or add additional context in comments.

Comments

0

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);

               // .....
           }

        }

1 Comment

thank's for the answer but my hashtable is different (as i wrote ): Hashtable<Integer, Hashtable<String, Object>> properties = new Hashtable<Integer, Hashtable<String, Object>>(); how can i get and array or arraylist of the Object in the second level Hashtable? thank's

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.