How do I iterate and enter values from a mapped entrySet into an array?

  • Java
  • Thread starter iamjon.smith
  • Start date
  • Tags
    Java
In summary, the code is using a for loop to iterate through a mapped entrySet and print out the values in the console. However, the desired outcome is to put each value into an array. To achieve this, the for loop should be modified to access each individual item using the getKey() and getValue() methods.
  • #1
iamjon.smith
117
3
Using the following code:

Code:
Set<Map.Entry<String,JsonElement>> mySet = jsonObject.entrySet();
for(Map.Entry<String,JsonElement> singleItem : mySet)
{
	singleItem.getKey();
	System.out.println("Set values: " + mySet);
}

mySet gets printed to the console as:

Code:
Set values: [columnGridContainer="[[1,\"1\",\"Default Value\",\"Y\",\"N\",\"0\",\"Left\",\"Default Value\",\"Default Value\",\"Default Value\",\"Default Value\",\"Left\",\"Center\",\"Center\",\"Center\",\"#000056\",\"None\",\"Date/Time\",\"No Filter\",\"N\"]]", header_row_2_enabled="Y", header_row_3_enabled="Y", header_row_4_enabled="Y", col_widths_units="pixels", default_sub_rows_closed="Y", extjs_component="grid", refreshrate="181", title="everything works right", subtitle="except display for checkboxes...", autoheight="N", bottom_buttons="N", col_label_source="col_config_table"]

I need to iterate through the values and put them into an array. Please provide some direction as to how to iterate over this mapped entrySet and enter each value into an array.
 
Technology news on Phys.org
  • #2
You are printing out the whole set in your for loop, don't you want to look at each item? Like this:

Code:
Set<Map.Entry<String,JsonElement>> mySet = jsonObject.entrySet();
System.out.println("mySet as 'key : value'");
for(Map.Entry<String,JsonElement> singleItem : mySet)
{
	System.out.println(singleItem.getKey() + " : " + singleItem.getValue());
}
 

What is JAVA?

JAVA is a programming language that is used to create computer applications and systems. It was developed by Sun Microsystems in 1995 and is currently owned by Oracle Corporation.

What is EntrySet() in JAVA?

EntrySet() is a method in the Java Collection Framework that returns a set view of the mappings contained in a Map. It is used to iterate over the key-value pairs in a map.

What is an iterator in JAVA?

An iterator in JAVA is an object that is used to traverse over a collection of objects. It allows for sequential access to the elements in a collection and provides methods for adding, removing, and querying elements.

How is EntrySet() different from keySet() in JAVA?

EntrySet() returns a set of key-value pairs, while keySet() only returns a set of keys from a map. EntrySet() is useful for iterating over both keys and values, while keySet() is only useful for iterating over keys.

What are some common uses of iterators in JAVA?

Iterators are commonly used in JAVA for traversing over collections such as lists, sets, and maps. They are also useful for removing elements from a collection while iterating over it.

Similar threads

  • Programming and Computer Science
Replies
5
Views
3K
Back
Top