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
JavaBot9mo 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
nettyOP9mo ago
No description
Defective
Defective9mo 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
nettyOP9mo ago
like this
No description
netty
nettyOP9mo ago
and resources
Defective
Defective9mo ago
So you have a Properties object, right?
netty
nettyOP9mo ago
yes
Defective
Defective9mo 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
nettyOP9mo ago
yes indeed
Defective
Defective9mo 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
nettyOP9mo ago
ok I see
Defective
Defective9mo 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
nettyOP9mo 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
Defective9mo ago
In this example it will be the api_links List, it should contain all links read from Properties
netty
nettyOP9mo ago
api_links[] ojk ye I can just iterate through that list using .length right
Defective
Defective9mo ago
size(), but yeah
netty
nettyOP9mo ago
ok do I need .getProperty also or no No Cuz its already saved in the list right
Defective
Defective9mo ago
Yeah
netty
nettyOP9mo ago
ok
Defective
Defective9mo ago
This list is completely independent from the Properties object
netty
nettyOP9mo ago
ok good ok thats it I guess thanks men
JavaBot
JavaBot9mo 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
Defective9mo ago
You are welcome
netty
nettyOP9mo 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
nettyOP9mo ago
No description
netty
nettyOP9mo ago
would this work
No description
Defective
Defective9mo 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
nettyOP9mo ago
ok, will Vars.apis return single strings or god ive never used this, that shit is breaking my brain
Defective
Defective9mo ago
It returns the very same List you passed with setApis setApis just assigns your list to a variable in the class
netty
nettyOP9mo ago
ok it wokred fire ok for now ill try to work w that thanks mate
JavaBot
JavaBot9mo 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.

Did you find this page helpful?