AndreaNobili
AndreaNobili

Reputation: 43077

Howto check if a TreeMap contains a specific object?

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

Answers (4)

Todd
Todd

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:

  1. Iterating over treePath's values either using an iterator, an enhanced for loop, or the Java 8 streams.

  2. Creating another map to map the same DLFolder objects, but by path.

Upvotes: 1

Thierry
Thierry

Reputation: 5243

for (DLFolder dlf : treePath.values()) {
if ("A SPECIFIC PATH".equals(dlf.getPath()) {
// do someting with the dlf
}

Upvotes: 4

Christian Tapia
Christian Tapia

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

Marko Topolnik
Marko Topolnik

Reputation: 200296

In Java 8 this is rather straightforward.

treePath.values().anyMatch(dlf -> dlf.getPath().equals(specificValue))

Upvotes: 2

Related Questions