-4

Point me why i can't declare upper bonds for generics like this?:

public R computeMove(Map<K extends String,V extends Integer> c);

But can declare it like this:

public R computeMove(Map<K,V> c);

UPDATE

Interface:

public interface Strategy<K extends String,V extends Integer,T extends Map<K,V>, R extends GameFigures>{

    public R computeMove(T c);
}

Interface Strategy realization:

Please, don't mind about return null - it's only for example.

    public class ProbabilityStrategy implements Strategy{

        @Override
        public GameFigures computeMove(Map c){
            Collection<Integer> values=c.values();//<-- unchecked exception
            Stream<Integer> stream=values.stream();//<-- unchecked exception
            Optional userMaxFigureMoves=stream.max(Integer::compare);
            return null;
       }
}

Code works, but why there is unchecked exception if upper bounds for K and V declared in interface Strategy? So, Map should be like Map<String, Integer>

If change the class code to:

public class ProbabilityStrategy implements Strategy{

            @Override
            public GameFigures computeMove(Map c){
                Collection values=c.values();
                Stream stream=values.stream();
                Optional userMaxFigureMoves=stream.max(Integer::compare);//<-- compiler error
                return null;
           }
    }

Code didn't compile and that's write, but why? According to interface Strategy, Map should be as Map <String,Integer>

Can't understand why upper bounds for K and V didn't work for Map?

user124
  • 13
  • 5
  • 4
    Why are you declaring the type variables K and V whose upper bounds are `final` classes? – rgettman Jun 05 '19 at 21:16
  • Have you tried your way, does it works? In my opinion, `Map c` is correct, as your way cannot cover other scenarioes such as `Map` – Qingfei Yuan Jun 05 '19 at 21:16
  • 2
    Enable all compiler warnings, and pay attention to them. Especially the warnings about “raw types.” – VGR Jun 05 '19 at 21:17
  • `R extends GameFigures` and it appears as `GameFigures` in overridden method, `T extends Map` and it appears as `Map`, but why, for example `K` which extends `String` not appears as its upper bound `String` in `Map`? – user124 Jun 05 '19 at 21:26
  • 2
    It doesn't actually look like `Strategy` should have any type parameters for your use case; it really ought to be `interface Strategy { public GameFigures computeMove(Map c); }`. – Louis Wasserman Jun 05 '19 at 21:38
  • Possible duplicate of [What is a raw type and why shouldn't we use it?](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – Tom Jun 05 '19 at 21:39
  • Where is `K` declared? `K extends String` would go in the declaration, either on the method level or class level. Or you can do `Map extends String, V extends Integer>`. Though I can't imagine a good reason to. – shmosel Jun 05 '19 at 22:28
  • can't write like this `Map extends String, V extends Integer>` -> wrong number of arguments, required 2. I can't declare any bounds for gen types inside collection! Compiler show error. – user124 Jun 05 '19 at 22:42
  • Your question makes less sense now. It now says "I can't be bothered to research how to declare generic types in methods". Researching that will tell you why your approach doesn't work. – Tom Jun 06 '19 at 04:17
  • @Tom I can declare generic, but i can't declare bounded generic inside Map, why i can't declare `Map` but can write `Map`, could you give an answer? – user124 Jun 06 '19 at 10:33
  • "I can declare generic": No you can't. Just because you write `Map` means you declared generics there. Either declare them in the class, like you did in the removed code, or do it properly in the method (research how to do that). `Map` as a method parameter just __uses__ already declared generics and doesn't declare them. – Tom Jun 06 '19 at 11:44
  • @Tom it's not an answer at all and the question was not about what you are trying to say. All generics declared as it should in full code, i simplified a code for concentrating only on problem. Try to think and look at the code before write something. I am already found the answer by myself. – user124 Jun 06 '19 at 12:02

1 Answers1

0

Found an answer, I didn't declare the generic types when implementing interface Strategy.

Correct realization of interface:

public class ProbabilityStrategy implements Strategy<String,Integer,Map<String, Integer>,GameFigures>{

        @Override
        public GameFigures computeMove(Map<String, Integer> c){
            Collection<Integer> values=c.values();
            Stream<Integer> stream=values.stream();
            Optional<Integer> userMaxFigureMoves=stream.max(Integer::compare);
            return null;
       }
}
Tom
  • 16,842
  • 17
  • 45
  • 54
user124
  • 13
  • 5
  • A moderator deleted your original answer because you continued to re-add the meta-commentary to your answer that the community then had to clean up again. Re-posting the answer *with the same meta-commentary added back in* is not an appropriate way to behave. Such commentary has no place in answers, sorry. I've locked this answer to prevent further edits of the sort. – Martijn Pieters Jun 11 '19 at 14:12