jackthehipster
jackthehipster

Reputation: 1018

Looping over an array with alternating elements

Difficult to sum up the question in one sentence. It's easier to show you what I want. I have the following response of a Solr query:

{
  .....
  },
  "facet_counts":{
    "facet_queries":{},
    "facet_fields":{
      "city":[
        "New York",23258,
        "Los Angeles",13322,
        "Paris",1189]},
    "facet_ranges":{},
    "facet_intervals":{},
    "facet_heatmaps":{}}}

I use GSON to parse this JSON data and get a JsonArray containing the array "city". That contains alternating elements for the field name (key) and the corresponding value. So e.g. we have 13322 hits for "Los Angeles". I want to iterate over this array to get out the key=value pairs it contains.

I can think of a number of simple solutions for this task, for example

boolean isKey = true;
String key;
String val;
for(JsonElement je : facetMT) {
    if(isKey) {
        key = je.getAsString();
        isKey = false;
    } else {
        val = je.getAsString();
        resultMap.add(key,val);
        isKey = true;
    }

}

Or any other way to distinguish the odd and even elements, like checking for divisibility by 2.

But that feels crude. I'm sure Java has some other, elegant way to do this, like working with an iterator and skipping every other element. But I'm not good enough in Java to know how to do that, or any other supersmart way.

Any suggestions, or should I just go with the crude code?

Upvotes: 0

Views: 593

Answers (3)

Pshemo
Pshemo

Reputation: 124275

If you are sure that array will always have even amount of elements you can use two of them in one iteration like

Iterator<JsonElement> it = facetMT.iterator();
while (it.hasNext()) {
    // iterate over two elements
    resultMap.put(it.next().getAsString(), it.next().getAsString());
}

But to be honest proper answer would be enforcing results in form of array of objects like

  "city":[
    {"cityName":"New York", "amount":23258},
    {"cityName":"Los Angeles", "amount":13322},
    {"cityName":"Paris", "amount":1189}
  ]

instead of what you have now

  "city":[
    "New York",23258,
    "Los Angeles",13322,
    "Paris",1189
  ]

This way your code could look like:

for (JsonElement jsonElement : facetMT) {
    JsonObject obj = (JsonObject)jsonElement;
    resultMap.put(obj.get("cityName").getAsString(), 
                  obj.get("amount").getAsString());
}

Upvotes: 1

Mạnh Quyết Nguyễn
Mạnh Quyết Nguyễn

Reputation: 18235

If you're sure about the length of the array then you should read twice at one:

for(int i = 0; i < facetMT.size() - 1; i = i + 2) {
   JsonElement key = facetMT.get(i).getAsString();
   JsonElement value= facetMT.get(i + 1).getAsString();
   resultMap.add(key,val);
}

Note the i < facetMT.size() - 1 condition to avoid IndexOutOfBoundsException

Upvotes: 2

Duna
Duna

Reputation: 1612

In this case use for loop with indexes

For(int i=0;i<... FaceMT[i]= altered value.

Alterate value doesn't work with iterator for

Upvotes: 0

Related Questions