2

I have a Tree object that contains children (HashMap) of Tree objects and so on.
I need to filter objects by numericPosition variable.

For example:

Tree mapTreeRoot = new Tree("Root",0);      
int answer = 111;

mapTreeRoot
    .addNode("ChilldOfRoot",111)
    .addNode("ChildOfRootExample1",222)
    .addNode("ChildOfRootExample1Example2",333);

Tree treeObj  = mapTreeRoot
        .children
        .entrySet().stream()
        .filter(map -> answer == map.getValue().numericPosition)
        .collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue()));

In this case I should get a Tree object filtered by numericPosition
Tree class

   public Tree(String name,int numericPosition) {
        this.name = name;
        this.numericPosition = numericPosition;
    }

    public Tree addNode(String key,int numericPosition) {
        Object hasKey =  children.get(key);
        if(hasKey == null) {
             children.put(key,new Tree(key,numericPosition)); 
        }

        return children.get(key);
    }

    public Tree getNode(String key) {
         Object hasKey =  children.get(key);
         if(hasKey != null) {
            return children.get(key);
        }
        return this;
    }

Just in case
I got this error: error: incompatible types: inference variable R has incompatible bounds

I have been following by this example but it's not working for me. https://www.mkyong.com/java8/java-8-filter-a-map-examples/

Also I have tried HashMap<String,Tree> treeObj = mapTreeRoot.. but got the same error message.

Oyeme
  • 11,088
  • 4
  • 42
  • 65
  • Your stream operation returns a map. This return value is incompatible to the treeObj which is no map. – Calculator Dec 26 '16 at 19:49
  • @Calculator I tried to use HashMap treeObj = mapTreeRoot... still the same issue. – Oyeme Dec 26 '16 at 19:51
  • 1
    What is the point of `("" + answer)` when `answer` is *already* a `String`? – Andreas Dec 26 '16 at 19:57
  • 4
    `Collectors.toMap()` produces a map of type `Map` not `HashMap`. – Calculator Dec 26 '16 at 19:58
  • 1
    @Andreas sorry, I have just an example.My work code is much bigger, you're right.I have just wrongly made an example.It was int. – Oyeme Dec 26 '16 at 20:00
  • @Calculator how to get a single object from Map? I need to a Tree object – Oyeme Dec 26 '16 at 20:01
  • 1
    @Oyeme `"111"` is not an `int`. Also, `numericPosition` is an `int` *(presumably)*, and `String.equals(int)` will never be true. – Andreas Dec 26 '16 at 20:01
  • 1
    @Oyeme Are you truly asking us how to get an object of type `Tree`? You invoke `new Tree(...)` on an appropriate constructor. – Andreas Dec 26 '16 at 20:03
  • @Andreas just updated to .filter(map -> answer == map.getValue().numericPosition); – Oyeme Dec 26 '16 at 20:06
  • 2
    Can you please post a [mcve], i.e. a full example. – assylias Dec 26 '16 at 20:08
  • @Andreas yes, I need get a Tree object filtered by numericPosition field. – Oyeme Dec 26 '16 at 20:08
  • 1
    @Oyeme Do you want to make a tree out of the filtered map or do you expect to get exactly one tree that fulfills the filter condition: map -> answer == map.getValue().numericPosition? – Calculator Dec 26 '16 at 20:17
  • @assylias just added an example to generate an Tree of children. mapTreeRoot .addNode("ChilldOfRoot",111) .addNode("ChildOfRootExample1",222) .addNode("ChildOfRootExample1Example2",333); – Oyeme Dec 26 '16 at 20:20
  • @Calculator correct, one tree that fulfils the filter condition. – Oyeme Dec 26 '16 at 20:21
  • 1
    you can use something like this. `Tree treeObj = mapTreeRoot.children .values().stream().filter(map -> answer == map.numericPosition).findFirst().get();` – s7vr Dec 26 '16 at 20:45

1 Answers1

4

If you want to filter for exactly one tree you can use:

Tree treeObj = null;
Optional<Entry<String, Tree>> optional  = mapTreeRoot
        .children
        .entrySet().stream()
        .filter(map -> answer == map.getValue().numericPosition)
        .findAny();
if(optional.isPresent()){
    treeObj = optional.get().getValue();
}
Calculator
  • 2,769
  • 1
  • 13
  • 18