Java Basic Learning Note "08" Generics-qualified, generic operation

Source: Internet
Author: User

  1. When inheriting the method of implementing an interface, if the generic interface directly specifies the type without subsequent casts, this directly represents the type, and the parameter is the type
  2. 1 public static void up (list<? extends number> l) { //method implementation}
  3. 1 public Static void down (list<? super number> l) { //method implementation  }
  4. Generic Erase
    12 List<Integer> l1 = newArrayList<Integer>();  List l2 = l1;
  5. Map cannot traverse the for loop directly because the interface is not implemented
  6. List wildcard lsit<?> generally as a method parameter does not indicate what type is not yet known, can receive all the list
  7. If you add the same key in the map, overwrite the previous
  8. Collections: The tool Class of the collection: it contains the methods of sorting, finding, exchanging elements, and getting synchronized set objects; See API for details;
  9. Arrays:
    1 publicstatic<T>List<T> asList(T ... t){}
    returns a fixed-length list set, and a T array element that consists of a list collection element; The returned list collection length cannot be changed, otherwise, an error occurs;
    12345678910111213 int[] arr = {1,2,3};   List<Integer> l1 = Arrays.asList(arr);//ERROR   List<int[]> l2 = Arrays.asList(arr);//YES     Integer[] arr1 = arr;//ERROR   Integer[] arr1 = {1,2,3};   List<Integer> l3 = Arrays.asList(arr1);     List <----> 数组   List-->数组: Collection接口里的 Object[] toArray();    List l = new ArrayList();      l.add(1);   l.add("a");   l.add(false); Object[] arr= l.toArray();
  10. generics: Simple to use (must be mastered):
    123 List list = newArrayList();      String ele = (String)set.get(0);   List<String> list = newArrayList<String>();
    indicates that the set collection can only be loaded as a string type element;
    1 String ele = set.get(0);
    at the bottom are still:
    1 String ele = (String)set.get(0);
    123456789 Map<String,Date> map = new HashMap<String,Date>();     map.put("A",new Date());    Set<Map.Entry<String,Date>> entrys = map.entrySet();   //取出Map里所有Entry对象Iterator<Map.Entry<String,Date>> it = entrys.iterator();//使用迭代器//迭代出每一个Entry对象   while(it.hasNext())    {Map.Entry<String,Date> entry = it.next();     //取出每个entry里的 key和 valueString key = entry.getKey();Date value = entry.getValue();    }  //show(new HashMap<>());
  11. Generic method://The declaration placeholder for a generic method can only be placed in front of the method return type;
    1 publicstatic<T> List<T> asList(T ... t){     }
    Static methods of arrays change some mutable parameters to list objects
  12. get classname permissions named class bytes static method for code object   class
    1 static class<?> forname (String className)
      Represents all types that are indeterminate and can accept all types;   But you can't add elements to it
  13. The upper and lower limits of the list<> cannot be used simultaneously
  14. The delimiter for a generic file path uses "/" to be compatible with multiple platforms, "\ \" generally only supports Windows
  15. Generally using the file file class to create files does not specify the path is the abstract path is the bin directory, the default is created below



Java Basic Learning Note "08" Generics-qualified, generic operation

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.