2

In JavaScript I can define the following collection with keys awaiting values

var items = {
                'book':null,
                'pen':null,
                'pencil':null,
                'chicken':null,
                'wallet':null
            };

Then when I am ready to add values to my collection, I can do for instance

for(var p in items){
  if(some condition){
     items[p]=someValue;
  }
}

Is there a way to do this with the same level of efficiency in java?

I know that in old Java I can combine a Map and a List to accomplish this, but are their new data structures in Java that can handle this? I am talking about Java 7 (or 8) perhaps? I am using Google App-Engine for my Java.

5
  • Note that you don't actually have to initialize JavaScript object properties. If you make an assignment to a non-existent property, the property is added to the object. Commented Oct 14, 2015 at 17:35
  • I dont understand the question I guess, but are you asking for this? Commented Oct 14, 2015 at 17:41
  • So I have edited to drive the point home: I am iterating before adding any values to the collection. Commented Oct 14, 2015 at 17:57
  • Updated my answer with regards to your latest comments. Commented Oct 14, 2015 at 18:08
  • Since the two answers are essentially the same, I will check the first response and up vote the second one. Thank you very much for helping resolve this problem. Commented Oct 14, 2015 at 18:17

2 Answers 2

6

You could try it this way, if you're looking for the same style.

 HashMap<String, String > items  = new HashMap<String, String>(){{
        put("book",null);
        put("pen",null);
    }};

Later you can put again with keys.

items.put("book", "Some Bible");

It seems you are new to Java and both Collections. I'm highly recommend you to read the about HashMap more before proceeding.

Sign up to request clarification or add additional context in comments.

10 Comments

This is more what OP was asking for I think
Although, to be honest, I don't see the point of putting those keys with a null value. The value null will be returned anyway, by getting "book", if it's not present in the map.
@ZaphodBeeblebrox True. But I don't exactly know why OP is looking that way and. One benefit I can see is containsKey() method behaviour.
This looks like a bad idea to me. Creating an anonymous class extending HashMap just to avoid using items. in front of put?
I suppose this is closer to what I was looking for, although it does in fact reduce to a Map and a List. You may be overlooking what is actually happening in my for-loop, where I am iterating over the keys essentially. So I suppose your answer works if I use items.keySet() to get the keys. Perhaps this is as close as it gets.
|
4

EDIT: Updated my answer based on the latest comments.

You could perfectly use a HashMap to achieve the same effect. To iterate over the existing keys, use the Map#keySet method.

Map<String, String> map = new HashMap<String, String>();
map.put("book", null);
map.put("pen", null);

for (String key : map.keySet()) {
    map.put(key, "Some Value");
}

System.out.println(map);

2 Comments

...and, starting with Java 7, you can use the diamond notation: Map<String, String> map = new HashMap<>();
The answers are the same, but this answer was first. So I up vote the other and check this one.

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.