#Java .properties read Strings with the same name
54 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @cerulean knoll! Please use
/closeor theClose Postbutton 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.
You can either iterate over all keys in properties and filter all that start with "API", or use streams
Depends how you are currently reading your properties
So you have a Properties object, right?
yes
Okay, sec
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?
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
ok I see
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
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
In this example it will be the api_links List, it should contain all links read from Properties
size(), but yeah
ok
do I need .getProperty also or no
No
Cuz its already saved in the list right
Yeah
ok
This list is completely independent from the Properties object
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.
You are welcome
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
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
ok, will Vars.apis return single strings or
god ive never used this, that shit is breaking my brain
It returns the very same List you passed with setApis
setApis just assigns your list to a variable in the class
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.