1

Let say I have such Hashtable:

Hashtable<Object, Object> test = new Hashtable<>();

test.put("t", 1);
test.put(2, "t123");
test.put(3, true);

How could I just return any value from it or get any key from this hash table without knowing any key or value? Should I just iterate it and get first value or maybe there is some simpler better way?

1
  • I think it has entrySet() method, which might help. But why a hashtable instead of arraylist or array? Why do you need string keys? Commented Nov 21, 2013 at 20:29

4 Answers 4

1

If you meant "any":

test.keys().nextElement();

or

test.element().nextElement();
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. This one is simple and good. And yes I meant random or any:)
random means distributed well across your sample space, which is not happening here.
I know what it means, but I as you can see author wrote "yes I meant random or any".
@RuslanOstafiychuk Then he should change the question to any not random in the original question.
@Trying "return any value or get random key" (maybe he added it later)
1

You can put all entries into a List and then use a random index to retrieve a random entry.

Hashtable<Object, Object> test = new Hashtable<Object, Object>();

test.put("t", 1);
test.put(2, "t123");
test.put(3, true);

List<Entry<Object, Object>> entries = new ArrayList<Entry<Object, Object>>(test.entrySet());
Random random = new Random();
Entry<Object, Object> randomEntry = entries.get(random.nextInt(entries.size()));

Object randomValue = randomEntry.getValue();
Object randomKey = randomEntry.getKey();

Comments

1
HashMap<Integer, String> map = new   HashMap<Integer, String>();
        map.put(10, "ddsadas");
        map.put(23, "sdss");
        map.put(24, "sdss");
        map.put(90, "sdss");

now random key generation O(N):

        int size = map.keySet().size();
        int index=(int)(Math.random() * size)+1;
        System.out.println(index);
        for(Integer i : map.keySet()){
            if(index==1){
                System.out.println(i);
                break;
            }
            else{
                index--;
            }
        }

You can achieve this in O(1) by putting there set in to an array and generate a random number among the array index and just return the value in the index of array. This is o(1).

Comments

1

You can't get a random element from a hashtable in O(1), but the most efficient way to do it in O(n) would be something like this:

int chosenIndex = (int) Math.random()*map.size();
    i = 0;
for (Object v : map.values())
  if (i++ == chosenIndex) 
    return v;

BTW never use the Hashtable class, which is still around only for backwards compatibility. Use HashMap.

3 Comments

I think we are gone too ahead, OP want any key it seems i.e. just throw the first value. See the discussion below. :)
Yes, I saw it. But since the wording of the question implies a random element, this page could still be reached by someone actually needing a random element from a hashtable.
Marko Topolnik very true.

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.