Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get keys and value from object in Java

Tags:

java

object

key

I was wondering how to get KEY and VALUE from object in JAVA

my object is like below

Object obj = [{ index_a = 1, index_b = 2}, { index_a = 3, index_b = 4}]

I want to get index_a, 1, index_b, 2, ...

like image 962
Jay Avatar asked Jan 30 '26 10:01

Jay


1 Answers

I'm guessing you've come to Java from a language in which objects have loose types and are accessed approximately as key, value pairs which are often defined at run time. An example is json converting directly to JS objects with minimal translation.

That does not describe Java at all. The type and structure of Java objects are explicitly defined at compile time and expected to be accessed through methods available at compile time. For an experienced Java coder, your question is confusing because accessing object variables as keys and values is just not how things are generally done in Java.

If your object is going to store a list of maps from string keys to date values (for example), then that would normally be expressed in java as a variable of type List<Map<String,Date>>.

like image 122
sprinter Avatar answered Feb 01 '26 23:02

sprinter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!