1

First of all, this is my code (just started learning java):

Queue<String> qe = new LinkedList<String>();

qe.add("b");
qe.add("a");
qe.add("c");
qe.add("d");
qe.add("e");

My question:

  1. Is it possible to add element to the queue with two values, like:

    qe.add("a","1"); // where 1 is integer

So, that I know element "a" have value 1. If I want to add a number let say "2" to element a, I will have like a => 3.

If this cant be done, what else in java classes that can handle this? I tried to use multi-dimention array, but its kinda hard to do the queue, like pop, push etc. (Maybe I am wrong)

  1. How to call specific element in the queue? Like, call element a, to check its value.

[Note]

Please don't give me links that ask me to read java docs. I was reading, and I still dont get it. The reason why I ask here is because, I know I can find the answer faster and easier.

8
  • 3
    You are looking for a HashMap. Commented May 10, 2010 at 16:43
  • HashMap? brb... Googling Commented May 10, 2010 at 16:44
  • @Konerak: Looks like mashmap can do it. So, basically the difference between hashmap and queue is the stored values? Hashmap stores in pairs, where queue stores single value? Commented May 10, 2010 at 16:47
  • And we will give you links to the docs, because we can't repeat them here entirily - there is too much information to paraphrase. If you are having trouble with a specific piece of the javadocs, we're glad to explain that part in further detail :) Commented May 10, 2010 at 16:47
  • I though queue => linked list has "head" and "pointer"?? Commented May 10, 2010 at 16:48

4 Answers 4

2

You'd want to combine a Queue<K> with a Map<K,V>:

  • Put the keys (e.g. "a", "b") into the Queue<K>
  • Assign the mapping of the keys to values (e.g. "a"=>3) in the Map<K,V>
Sign up to request clarification or add additional context in comments.

Comments

1

I think you're asking for a dictionary type in Java.

Map<String, Integer> map = new HashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);

You can then access them by key - in this case the String you choose as the key.

int value = map.get("a");

Value in this case will return 1.

Is that what you want?

3 Comments

cool!! Yeah. I think I got it. No need to read the java docs already. Thanks
I am checking if I can do push, pop thing in hashmap. brb ^^,
see the stack/queue collections
1

You want to use a HashMap instead of LinkedList. HashMap is a dictionary-like structure that allows you to create associations, for instance a=>1.

Check out JavaDocs for HashMap to get a grasp how to use it:-).

2 Comments

While I am reading, could you tell how to to access specific element? Like element 11 of 20?
Easy, use HashMap.get("a"). You access elements basing on the first side of association (the key), you cannot access them basing on their index (the is no such thing here). But, there is possibility to iterate over a whole collection if you like.
0

I think what you are asking for is LinkedHashMap which is a combination of a Queue and a HashMap. While you are able to store the key and value pairs, it would also remember the order like Queue does. The only thing is you'd have to use an iterator since there is no poll() method, however you can visit each element in the order that they were added.

Comments

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.