generics

Is it possible to use a single generic for both key and value of a HashMap?

风格不统一 提交于 2021-02-19 06:08:05
问题 In chapter 13 of the Rust book, you implement a Cacher to use memoization to demonstrate functional programming and how to speed up long-running tasks. As an extra challenge, they recommend making the Cacher allow multiple keys using a HashMap and also leveraging generics to allow more flexibility. Try modifying Cacher to hold a hash map rather than a single value. The keys of the hash map will be the arg values that are passed in, and the values of the hash map will be the result of calling

Is it possible to use a single generic for both key and value of a HashMap?

不问归期 提交于 2021-02-19 06:06:10
问题 In chapter 13 of the Rust book, you implement a Cacher to use memoization to demonstrate functional programming and how to speed up long-running tasks. As an extra challenge, they recommend making the Cacher allow multiple keys using a HashMap and also leveraging generics to allow more flexibility. Try modifying Cacher to hold a hash map rather than a single value. The keys of the hash map will be the arg values that are passed in, and the values of the hash map will be the result of calling

Is it possible to use a single generic for both key and value of a HashMap?

风流意气都作罢 提交于 2021-02-19 06:04:57
问题 In chapter 13 of the Rust book, you implement a Cacher to use memoization to demonstrate functional programming and how to speed up long-running tasks. As an extra challenge, they recommend making the Cacher allow multiple keys using a HashMap and also leveraging generics to allow more flexibility. Try modifying Cacher to hold a hash map rather than a single value. The keys of the hash map will be the arg values that are passed in, and the values of the hash map will be the result of calling

Casting Nested Generic Types

送分小仙女□ 提交于 2021-02-19 05:24:06
问题 I understand that it is illegal in Java to cast a List<Number> to a List<Double> , since List<Double> is not a subtype of List<Number> : List<Number> list1 = new ArrayList<>(); List<Double> list2 = (List<Double>) list1; // inconvertible types With this understanding I would expect that it is also illegal to cast a List<? extends List<Number>> to a List<List<Double>> , since I would think that List<Double> does not belong to the family of types given by List<Number> . However, this cast is

Check if two generic types are equal

微笑、不失礼 提交于 2021-02-18 22:31:28
问题 I need to find if a type is a certain generic type. class MyType<T> {} var instance = new MyType<int>(); var type = instance.GetType(); This check does not work, but this is what I want to check. If the type is of that generic type, regardless of what T is. type == typeof( MyType<> ) This does work, but feels dirty. It could also be wrong since it's not the FullName . type.Name == typeof( MyType<> ).Name I'm assuming there is a way to do this, but I haven't found one. Using IsAssignableFrom

Equivalent of TreeSet in Java to C#.net

空扰寡人 提交于 2021-02-18 20:01:50
问题 I have Java code containing a TreeSet . I want to convert the code to C#. Which equivalent collection can I use? If there is none please suggest alternatives. 回答1: I think there is no treeset in C#. There was similar question asked in msdn, check that may be useful. http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/823c46ca-4a60-4429-a606-e76c3195d4cc/ 回答2: That would be System.Collections.Generic.SortedSet<T>. It does have the methods and complexity guarantees that one would

How to re-use duplicate code with Enums in Java 8?

心已入冬 提交于 2021-02-18 19:26:07
问题 I have 3 enumerator classes in my application. All 3 classes have 2 duplicate methods that we want available in every enum that we implement. public static List<String> supported(){ return Arrays.asList([[EnumClass]].values()) .stream().map(Enum::name).collect(Collectors.toList()); } public static boolean contains(String value){ boolean response = false; try { response = value != null ? [[EnumClass]].valueOf(value.trim().toUppercase()) != null : false; } catch (Exception e){ LOGGER.error(

Generic type parameters inference in method chaining

痞子三分冷 提交于 2021-02-18 10:36:45
问题 After reading this question, I've started to think about generic methods in Java 8 . Specifically, what happens with generic type parameters when methods are chained. For this question, I will use some generic methods from Guava's ImmutableMap , but my question is more general and can be applied to all chained generic methods. Consider ImmutableMap.of generic method, which has this signature: public static <K, V> ImmutableMap<K, V> of(K k1, V v1) If we use this generic method to declare a Map

Java Object return type vs. Generic Methods

♀尐吖头ヾ 提交于 2021-02-18 09:01:14
问题 I saw several questions about generic return type, but none answers my question. If there is no bound for any of the arguments, such as the following method in JayWay : public static <T> T read(String json, String jsonPath, Filter... filters) { return new JsonReader().parse(json).read(jsonPath, filters); } What is the point of using this as generic ? I told the guys from my team that this method should be used as : JsonPath.<Boolean>read(currentRule, "$.logged") instead of: (boolean) JsonPath

Java Object return type vs. Generic Methods

与世无争的帅哥 提交于 2021-02-18 09:00:50
问题 I saw several questions about generic return type, but none answers my question. If there is no bound for any of the arguments, such as the following method in JayWay : public static <T> T read(String json, String jsonPath, Filter... filters) { return new JsonReader().parse(json).read(jsonPath, filters); } What is the point of using this as generic ? I told the guys from my team that this method should be used as : JsonPath.<Boolean>read(currentRule, "$.logged") instead of: (boolean) JsonPath