#Java .properties read Strings with the same name

54 messages · Page 1 of 1 (latest)

cerulean knoll
#

Ok so I got a .properties file in my Project and now, I would like to read every single property, which starts with "API" in this example and return it as a list or something, how can I do that

old impBOT
#

This post has been reserved for your question.

Hey @cerulean knoll! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

cerulean knoll
wicked barn
#

Depends how you are currently reading your properties

cerulean knoll
#

like this

#

and resources

wicked barn
#

So you have a Properties object, right?

cerulean knoll
#

yes

wicked barn
#

Okay, sec

wicked barn
# cerulean knoll

So basically, you want to read all properties that start with specified string and store it in a list right? So in this example you expect to get a list containing just the api links?

cerulean knoll
#

yes

#

indeed

wicked barn
#

So you have two options. Let me start with the one that is fairly simple to understand.

#
Properties props = ...

List<String> api_links = new ArrayList<>();

for(Map.Entry<Object, Object> entry : props.entrySet()) {
    if(entry.getKey() instanceof String key && key.startsWith("API")) {
        Object value = entry.getValue();
        if(value instanceof String valueString) {
            api_links.add(valueString);
        }
    }
}

So, you can iterate over all entries in your Properties object

#

Properties extends a map, so it also has an entrySet method, which returns a set of entries in this properties object

#

You can then check every key one by one if it starts with desired string ("API" in your case), and if it does, just add a value associated with current entry to a List created before

cerulean knoll
#

ok I see

wicked barn
#

You should do instanceof checks, because Properties#entrySet() returns a set of <Object, Object> pairs, so it's safer to check if these Objects are Strings for sure

cerulean knoll
#

Ok I see, now what do I call or use to uhm write for example the first API thing into a string or something

#

like where will the links be saved

wicked barn
cerulean knoll
#

api_links[]

#

ojk ye

#

I can just iterate through that list using .length right

wicked barn
#

size(), but yeah

cerulean knoll
#

ok

#

do I need .getProperty also or no

#

No

#

Cuz its already saved in the list right

wicked barn
#

Yeah

cerulean knoll
#

ok

wicked barn
#

This list is completely independent from the Properties object

cerulean knoll
#

ok good

#

ok thats it I guess

#

thanks men

old impBOT
# cerulean knoll thanks men

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

wicked barn
#

You are welcome

cerulean knoll
#

uh another question rq

#

How do I create such a list, with as much elements as a method for example needs

#

cuz I got a vars.java file where all read properties are saved as static

#

would this work

wicked barn
#

Depends on what you want to achieve, but if your file looks roughly like this:

public static class Vars {
  public static List<String> apis;

  public static void setApis(List<String> api) {
    apis = api
  }
}

then that should work. You will be able to access the same list by just calling Vars.apis

cerulean knoll
#

ok, will Vars.apis return single strings or

#

god ive never used this, that shit is breaking my brain

wicked barn
#

setApis just assigns your list to a variable in the class

cerulean knoll
#

ok it wokred

#

fire

#

ok for now ill try to work w that

#

thanks mate

old impBOT
# cerulean knoll thanks mate

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.