Java Fundamentals (vii)

Source: Internet
Author: User

(i) generic type
(1) The array directly defines the type of storage, to prevent the occurrence of other types of elements, the collection can also be like an array, directly specify the storage type of our collection, for this case
One technique: Generics
< data types >---reference data types

Generics: Deferring the work of an explicit collection type to a parameterized type that can be passed as a parameter when creating an object or invoking a method.
Benefits of Generics:
1) Advance the run-time exception to the compile time
2) optimized the design, solve the XXX warning line problem
3) Avoidance of forced type conversions

Generic derivation can provide program security!
(2) Defining generics on a class
In the early days, using the object type to represent any type, the upward transformation was no problem,
When you use a downward transition, you may have problems (problems with the type you need), and you need to introduce a generic operation

See APIs, interfaces, classes, methods <E>, all generics
Improve program security by defining generics on classes
Resolves an issue with down-type conversions: classcastexception
Problem: You can define generics on a class, do you not define generics on a class, and apply generics to methods?
Can be defined on the method!
(3) Defining generics on an interface
Type of application: In the interface, class, method applied, in the collection to apply the most!

The first case of a generic, sub-implementation class in an interface is that you already know what type it is.
public class Interimpl implements inter<string> {

@Overridepublic void show() {    System.out.println("hello");}

}
The second situation is that you don't know what the specific type is.
public class Interimpl<t> implements inter<t>{

@Overridepublic void show() {    System.out.println("hello");}

}
(4) Generic Advanced (wildcard)
<?>: Represents any type of object type, or any Java class
<? Extends E>: down qualification, subclass E or E this type
<? Super E>: Up qualification, E and other parent classes
(ii) Collection
(1) ArrayList
ArrayList is a commonly used sub-implementation class in the list interface.

遍历功能:    1)一个是Collection的iterator()    2)size()和get(int index)    3)普通for循环  4)增强for循环     5)针对数组操作的工具类:Arrays,提供了一个方法:
  • public static <T> list<t> aslist (T ... a): Converts an array to a fixed-size collection

    Note: If you use this method, the length of the collection is not variable
    (2) Vector set
    The bottom layer is an array of objects that can grow, query fast, and delete slowly
    Thread-safe, synchronous, high-efficiency execution

    Unique Features:

  • public void AddElement (object obj)------->add (Object obj)
  • Public enumeration elements (): Returns an enumeration of this vector---> equivalent: Public Iterator Iterator ()
  • Boolean hasmoreelements ()--->boolean hasnext ();
  • Object nextelement ()--->object next ();
    (3) LinkedList collection
    Features of the LinkedList collection:
    The bottom is a kind of linked list implementation, query slow, and delete quickly
    Threads are unsafe, out of sync, and execution is high efficiency

    Unique Features:
    Add Features

  • AddFirst (Object E): Inserts the specified element at the beginning of the list
  • AddLast (Object E): Adds the specified element to the end of the list
    Get Features:
  • GetFirst (): Gets the first element of the list
  • GetLast (): Gets the second element of the list
  • Remove Features
  • The public Object Removefirst () removes and returns the first element of this list.
  • Public Object Removelast ()
    (4) Nested traversal of a collection
    Enhanced for traversal: large collection
    For (data type in large set < small collection data type > variable name: Large Collection Object name) {
    For (data type variable name in small collection: Small collection object name) {
    System.out.println (S.getname () + "---" +s.getage ());
    }
    }
    (5) Map Collection
    1) Demand: There are many students, students have a name, have a student number, according to the students to find students?
    Before the collection of learning, you can use the name, student number as the class of two member variables, the creation of the collection when the storage of this type, you can
  • The key is only to know the student number, through the study number to find the name
    Key Value

    School Number 1 Name 1
    School Number 2 Name 2
    School Number 3 Name 3
    Study number 1 (duplicate key) name 4

    Java for this technique----->map collection, a collection of key-value mappings (interfaces)
    The object that maps the key to a value. A map cannot contain duplicate keys, and each key can be mapped to at most one value.
    Map<k,v>, in the map collection, only valid for keys, not value-independent
    2)
    Features of the Map interface:
    V put (K key,v value): Add function: Associates the specified value and key
    If the current key is stored once, the return value is null
    If it is not the first time, the return value is the first corresponding value, and the current value replaces the value corresponding to the previous key!

    获取功能
  • Set<map.entry<k,v>> EntrySet (): Has a relationship with the traversal of the Map collection (key-value pairs object)
  • Set<k> KeySet (): Gets the collection of all the keys in the mapping relationship
  • int size () returns the number of key-value mapping relationships in this map
    Remove Features
  • void Clear (): Delete all mapping relationships
  • Vremove (Object key) If there is a mapping relationship for a key, remove it from this mapping

    Judging function:

  • Boolean ContainsKey (Object key) returns True if this map contains a mapping relationship for the specified key
  • Boolean Containsvalue (Object value): whether the specified value is included in the mapping relationship
  • Boolean IsEmpty (): Determines whether the mapping relationship is empty
    3) Two methods of traversal
    1, this kind of traversal method is often used in the actual development ...
    Set<k> KeySet (): Gets the collection of all the keys in the mapping relationship

    V get (Object key): Find value by key
    Conversion method:
    1) Bring all the husbands together.
    2) Let the husband its corresponding wife
    3) to traverse them out
    Another way of the map collection is to traverse set<map.entry<k,v>> entryset (): It is related to the traversal of the map collection (key-value pairs object)

    Forwarding method: 1) Get all marriage license 2) find the corresponding husband and wife by marriage certificate 3) Traverse output

    Idea: 1) Get all the key value objects EntrySet ()
    2) K GetKey () and V GetValue (), gets the key values for each key and each value in the object
    3) Traversal

    Ideas:
    1) Gets the collection of all the keys
    2) find its corresponding value by key
    3) Traverse can
    (6) Set set
    1) What is the difference between a set set and a list set?
    Set collection: element repetition is not allowed, unique (element can be null), no guaranteed sequence of iterations (underlying hash table and Hascode)
    Unordered (store and remove inconsistencies)

    2) The Add method of the HashSet collection relies on the two-column set HashMap, which relies on two methods, the Hashcode () method, and the Equals () method
    Compare the Hashcode () code value of the string first, and then compare the Equals () method
    If the Hascode code value is the same, the content is also compared, because the string is stored, overriding the Equals () method

    The string itself overrides the Equals method, so no more rewriting is required!
    3) The custom class is not rewritten in the hashcode () and Equals () methods, both of which resolve, overriding both methods

    (iii) new features after JDK5
    New features after Jdk5:
    Enhanced for, static import, variable parameters, generics, auto-unboxing.
    (1) Variable parameter: When the parameter number of a method is indeterminate, use a variable parameter
    Format:
    Modifier returns a value type method name (data type ... Variable name) {...}
    Attention:
    1) Variable name: as an array
    2) When using the data type ...

    Note: According to the specific needs to complete, the general situation, know that this feature can be
    (2) Enhanced for
    Enhance the format of a for loop
    For (data large type variable name: Array or Collection object name) {
    Output variable can!
    }

    Enhanced for occurs when an iterator is substituted, so you can use the enhanced for to go through the collection or iterate through the array

    The disadvantage of enhancing A For loop: If the object of the collection is NULL, an exception will occur if the collection operation is again
    To judge a set, to solve a non-null judgment
    (3) Static import
    Characteristics:
    1) The premise is that the method must be static
    2) The level of a method to import to

    Format for static import:

  • Import static package name. Class name. method name;

    *import static Java.util.ArrayList.add; Method must be a static method
    Which class in a common class has the most static method: Math

    Previous Guide package: Java.util.Scanner; Level of import to class
    Static import is the level at which a method is imported
    Note: The method name in a class of its own is the same as the method name that needs to be statically imported, and must be prefixed

Java Fundamentals (vii)

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.