}classStudentextendsperson2{}three, comparable interface: A method (Comparato)int CompareTo (T o)Compares this object to the specified object for sorting.
The object is smaller than the specified object and returns a negative integer (-1);
The object is equal to the specified object and returns a 0 (0);
The object is larger than the specified object and returns a positive integer (1);
Example: PackageCollectiontext;Importjava
letter (sometimes multiple generic types with numbers, such as T1,T2), such as many container collections using E, representing element (elements), and map with K for key keys,v to represent values.Two practical applications:(1) Creating a Java file for interface ParentinterfacePackage Edu.tju.cs;public interface parentinterface(2) Creating
traverse and select objects in a sequence, and the developer does not need to know the underlying structure of the sequence . Iterators are often referred to as "lightweight" objects because they are less expensive to create.Iterators in the collectionSince there are many different sets in Java, we want to have a uniform way to get its value, so we have an iterator.Interface iterator: There are two abstract methods
Boolean Hasnext () determi
implementation classes: HashSet, TreeSet, EnumsetHashSet elements are not duplicatedTreeSet elements are not duplicated and can be sorted naturally2.Queue interface and its implementation classQueuing queue FIFOElement offer Peek Poll removeDeque interface, dual-ended queueAddFirst addlast GetFirst getlast OfferfirstLinkedList both list and deque, thread is unsafe.Arraydeque:Priorityqueue:Map interfaceMap
all subclasses of E and EIv. the embodiment of the lower limit:The lower bound is usually used when removing an element from a set.That is, regardless of the type (as long as it is a subclass of the current parent class), you can use the parent type to receiveImport Java.util.comparator;import Java.util.treeset;import Java.util.iterator;class ComparaName implements comparatorFive, the embodiment of the wildcard characterImport Java.util.collection;import Java.util.arraylist;import Java.util.ite
specific type A and B, such asNumber is not relatedto Integer, myclassmyclass The common parent class of myclassObject. When there is a relationship between parameter types, how to establish a relationship between two sub-generic classes like subtypes, refer to wildcard characters and subtypes.Generic class and sub-typeYou can either extend or implement it by using a subclass or interface. The relationship of a type parameter of a class or
Java generics and java generics
1. OverviewBefore introducing the fan type, Java types include the original type and the complex type. The complex types include arrays and classes. After the fan type is introduced, a complex typeYou can split it into more types in detail.For
. javapackage org. rui. generics. anonymity; public interface Generator
{// Returns the generic internal object T next ();}
// Generators. javapackage org. rui. generics. anonymity; import java. util. collection;/*** use the generator to easily fill in a collection * @ author lenovo **/public class Generators {
Learning GoalsMastering the use of generic interfacesMastering the definition and use of generic methodsMastering the use of generic arraysMastering nested settings for genericsall of the previous operations are directly using generic operations in the class, and for Java, generics can be defined and used directly in the interface. Defining generic Interfacesafte
technology is to solve some pain points and optimize work efficiency. Regardless of the new technology is a practical technology, such as Redis, Hadoop and other solutions to solve existing storage problems. Or, like MVC, design patterns, OAuth is generally just a way of thinking, different languages have different implementations. The new technology (thought) is only for a specific pain point of a dose of medicine, the use of the right can be more effective, not when it will be less effective.
Underlying source code parsing for Java generics and underlying source code for java generics
Statement:The following source code is based on JDK1.8 _ 112.
1. ArrayList source code parsing
1 ArrayList list = new ArrayList();2 list.add(new Integer(4)); list.add("abc");3 System.out.println((Integer)list.get(0));4 Syste
a generic class of the same type parameter depends on the inheritance architecture of the generic class itself. The list is a subtype of collection, and list can replace collection. This is also true for type declarations with upper and lower bounds.
When wildcard characters are used in the type declaration of a generic class, their subtypes can be expanded on two dimensions. such as the collection 2.4 Bridge method
Http://www.cnblogs.com/ggjucheng/p/3352519.html 3. Generic class
An example of
use of rewriting the simple version ArrayListUse generic code as follows:packagejavabasic.generic;/*** Simple Version arraylist*/classarrlistIf you want to useArrlist.add ("1");Will find:This is when generics are being used, and there is no need to cast the property Get method. In fact, Java generics are just a compile-time concept, because the type will be era
clutter of the object code that needs to be cast.
2. Use generics to easily find and resolve bugs at compile time
Third, the use of rewriting the simple version ArrayListUse generic code as follows:?
123456789101112131415161718192021222324252627282930313233
package javaBasic.generic;/*** 简易版ArrayList*/class ArrListT>{private T obj; public T getObj(){return obj;}public void add(T obj){this.obj = obj;}}public class TestArrayLi
Java generics drill-down set useful tools various sets generics drill-down using demo sample, Anonymous inner class, inner class applied to generic discussionSets.javapackage org.rui.generics.set;import Java.util.hashset;import java.util.set;/** * A set useful tool * @author Lenovo * */public class Sets {public staticWatercolors.javapackage org.rui.generics.set;/
Generics in Java can be said to allow novice confused, and the use of multiple generics at the same time can make a person face. Let's take a look at each situation now.1. Simplest generic examples
public class Ttest
This is the simplest generic, you can see in the TTest class definition of the place to write a
You can see,ttest2. Use of multiple
Java generics are a new feature introduced in JDK 5 that allows the use of type parameters when defining classes and interfaces. generic class
Generic classes are the generic forms we use most often, as follows:
/**
* Tool Class
* Created by Yuedong.li on 12/8/15.
* * Public
class Toolsutil
A tool class that is currently used to replace If-else
The use of the method is also very simple
public class Main
time is taken according to the person type, because Al2 is placed in the AL1, the time is naturally obtained by AL1. The person can receive student, Mycolleciton This method can accept the new collection to store the E-type element, or the subclass of type E, because there is no problem.take when there is no type of security risks, AddAll method Extensibility is strong, supposedly said to write an e can be, and now expand the sub-class is also possible. When do you use this? When stored, the up
Today, the new guy asked me to help you see a bug that prompted classcastexception for the For traversal list.Pseudo code for (Order order:model.getOrderList) ...After debug found orderlist in the object[] array, and then caught the injected positionOrderList = (arraylistGeneric erase, etc. to be continued ...Java generics are fake generics, so annoying this thin
for common methods. This generic class is applicable to all classes or partial classes, generally, it is used with a partial classification. Some restrictions apply to these classes. generic classes do not care about the specific types of classes, but whether they can execute a certain method!
This is the "potential type mechanism ". Therefore, generic classes generally require the parameter type to implement a method or have a method. The implementation
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.