Java .properties read Strings with the same name

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
31 Replies
JavaBot
JavaBot7mo ago
This post has been reserved for your question.
Hey @netty.bootstrap! 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.
netty
nettyOP7mo ago
No description
Defective
Defective7mo ago
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
netty
nettyOP7mo ago
like this
No description
netty
nettyOP7mo ago
and resources
Defective
Defective7mo ago
So you have a Properties object, right?
netty
nettyOP7mo ago
yes
Defective
Defective7mo ago
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?
netty
nettyOP7mo ago
yes indeed
Defective
Defective7mo ago
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);
}
}
}
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
netty
nettyOP7mo ago
ok I see
Defective
Defective7mo ago
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
netty
nettyOP7mo ago
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
Defective
Defective7mo ago
In this example it will be the api_links List, it should contain all links read from Properties
netty
nettyOP7mo ago
api_links[] ojk ye I can just iterate through that list using .length right
Defective
Defective7mo ago
size(), but yeah
netty
nettyOP7mo ago
ok do I need .getProperty also or no No Cuz its already saved in the list right
Defective
Defective7mo ago
Yeah
netty
nettyOP7mo ago
ok
Defective
Defective7mo ago
This list is completely independent from the Properties object
netty
nettyOP7mo ago
ok good ok thats it I guess thanks men
JavaBot
JavaBot7mo ago
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.
Defective
Defective7mo ago
You are welcome
netty
nettyOP7mo ago
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
netty
nettyOP7mo ago
No description
netty
nettyOP7mo ago
would this work
No description
Defective
Defective7mo ago
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
}
}
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
netty
nettyOP7mo ago
ok, will Vars.apis return single strings or god ive never used this, that shit is breaking my brain
Defective
Defective7mo ago
It returns the very same List you passed with setApis setApis just assigns your list to a variable in the class
netty
nettyOP7mo ago
ok it wokred fire ok for now ill try to work w that thanks mate
JavaBot
JavaBot7mo ago
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. 💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server