A little usage of Java generics

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.