Reputation: 43077
In an application I have this TreeMap object:
treePath = new TreeMap<String, DLFolder>();
The first String parameter should be the key and the DLFolder is the value.
Ok the DLFolder object have this method dlFolder.getPath() that return a String
So I want to know if the treePath object contains a DLFolder object having a specific path value
Can I do this thing?
Tnx
Upvotes: 0
Views: 1170
Reputation: 31720
If the map's key is also the value stored in dlFolder.getPath()
, then yes, you can just call treePath.contains("Value");
.
Other options include:
Iterating over treePath
's values either using an iterator, an enhanced for loop, or the Java 8 streams.
Creating another map to map the same DLFolder
objects, but by path.
Upvotes: 1
Reputation: 5243
for (DLFolder dlf : treePath.values()) {
if ("A SPECIFIC PATH".equals(dlf.getPath()) {
// do someting with the dlf
}
Upvotes: 4
Reputation: 34176
You can loop through the values of the TreeMap
:
for (DLFoder folder : treePath.values())
if (folder.getPath().equals(somePathValue))
// path found!
Upvotes: 1
Reputation: 200296
In Java 8 this is rather straightforward.
treePath.values().anyMatch(dlf -> dlf.getPath().equals(specificValue))
Upvotes: 2