Java Basic notes (iv)

Source: Internet
Author: User
Tags class manager comparable

generic type:
Generics: Accepts a reference data type by < Data amount type >, which acts at compile time using check
Whether the object that adds the collection belongs to that type, which translates the run-time problem to a compile-time exception, improving the security of the program
Avoid throwing classcastexception, and do not require type conversions

<> when do I use it?
When the reference data type of the operation is not deterministic, use <> pass in the data type you want to manipulate.
In fact <> is equivalent to accepting specific parameter ranges for reference data types

Application of generics in interfaces:
Instance:
public class Demo21 {
public static void Main (string[] args) {
Manager m = new manager ();
M.add (New Worker ("a", 23));
M.add (The New Worker ("AB", 21));
M.add (The New Worker ("AC", 22));

    System.out.println("delete-->" + m.del(1));    m.show();}static class Worker {    private String name;    private int age;    public Worker(String name, int age) {        super();        this.name = name;        this.age = age;    }    @Override    public String toString() {        return "Worker [name=" + name + ", age=" + age + "]";    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }}

}

Interface IManager {
public void Add (T data);

public T del(int index);public void show();

}

Class Manager implements IManager {
Private list List;
Manager () {
List = new ArrayList ();
}
@Override
public void Add (T data) {
List.add (data);
}
@Override
Public T del (int index) {
T data = list.get (index);
List.remove (index);
return data;
}
@Override
public void Show () {
Iterator it = List.iterator ();
while (It.hasnext ()) {
System.out.println (It.next ());
}
}
}

Method uses generics: When the method parameter data type is not deterministic, the generic type is used
Class Genericmethod {
public void Test (T t) {
System.out.println ("Method T with no return value");
}

public <T> T getT(T t) {    System.out.println("有返回值的方法getT");    return t;}

}

Wildcard characters:?

Qualifying Type:
? Extends e: Accepts subtypes of type E or E, upper limit
General storage of objects when AddAll ()
? Super E: Accepts the parent type of type E or E, lower bound
Commonly used to remove objects, comparators
public class Demo22 {
public static void Main (string[] args) {
Food f = new food ();
F.setfood ("Hamburg");
System.out.println (F.getfood ());
Food f1 = new food ();
F1.setfood (100);
Food F2 = new food ();
Buy (f);
Buy (F1);
Buy1 (F1);
Buy2 (f);
Buy2 (F2);
}
public static void Buy

Equals ().->key is the only

TreeMap: (Key-value) collection, sorting by key

Similar to the TreeSet

1) Natural order
2) Custom Comparator
A custom class implements the comparable interface
b Define a custom comparer class to implement the comparator interface

(Key-value) collection, sorted by key

Similar to the TreeSet

1) Natural order
2) Custom Comparator
A custom class implements the comparable interface
b Define a custom comparer class to implement the comparator interface

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Basic notes (iv)

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.