1, a good generic, it is recommended not to write this
public static <k, v> map<k, v> getmap (string source, String firstsplit, String secondsplit)
Suggestions can be written like this
public static <k, v> map<k, v> getmap (list<k> keys, list<v> values)
or similar
public class mapitem<k, v>
{
Public K key;
public V value;
}
public static <k, v> map<k, v> getmap (list<mapitem<k, v>> items)
That is, if you are generic, you are a pan-bar, preferably not mixed. That is, the "pan" loses its meaning, it is a direct point
public static map<string, integer> Getmap (string source, String firstsplit, String secondsplit) {
map<string, integer> result = new hashmap<string, integer> ();
if (Source.equals ("")) {
return result;
}
String[] strings = Source.split (Firstsplit);
for (int i = 0; i < strings.length; i++) {
string[] tmp = Strings[i].split (secondsplit);
if (tmp.length = = 2) {
Result.put (Tmp[0], Integer.parseint (tmp[1]));
}
}
return result;
}
2, generics generally have "universal" sex, if we really want to do so, whether it can do so?
Use generics for specific types
public static map<string, integer> Getmap (string source, String firstsplit, String secondsplit) {
String[] strings = Source.split (Firstsplit);
arraylist<mapitem<string, integer>> items = new arraylist<mapitem<string, Integer>> ();
for (int i = 0; i < strings.length; i++) {
string[] tmp = Strings[i].split (secondsplit);
if (tmp.length = = 2) {
mapitem<string, integer> item = new mapitem<string, integer> ();
Item.key = tmp[0];
Item.value = Integer.parseint (tmp[1]);
Items.Add (item);
}
}
return Tomap (items);
}
Use of generics to provide universal encapsulation
public static <k, v> map<k, v> tomap (list<mapitem<k,v>> items) {
map<k, v> result = new hashmap<k, v> ();
For (mapitem<k, v> item:items) {
Result.put (Item.key, Item.value);
}
3, sometimes a thing always feel not good, is not originally we use excessive or design insufficient, and deviate from its essence? I personally feel that both Java and C # Generics are good, improving the efficiency and reusability of coding.
Http://www.cnblogs.com/magialmoon/p/3803114.html
A little usage of Java generics (GO)