Java 14th _ set, generic

Source: Internet
Author: User

Set:

Architecture:

Set: The elements are unordered (the order in which they are stored and retrieved is not necessarily the same) and cannot be repeated.

| --Hashset: The underlying data structure is a hash table.

| --Treeset: Underlying data structure Binary Tree.

The Set set function is consistent with the collection function.

 

How does hashset ensure the uniqueness of elements?

Is done through two methods of elements, hashcode and equals.

If the hashcode value of the element is the same, the system checks whether equals is true.

Equals is not called if the hashcode value of an element is different.

Note that for operations such as determining whether an element exists and deleting it, the dependent methods are the hashcode and equals methods of the element.

Hashset:

| --Hashset: Data Structure hash table, not synchronized,

The basis for ensuring element uniqueness:

Determine whether the hashcode value of the element is the same. If the value is the same, continue to determine whether equals is "true ".

 

Treeset:

| -- Treeset: sorts the elements in the Set set. The underlying data structure is a binary tree.

The basis for ensuring element uniqueness:

Compareto method return 0.

 

The first method of treeset sorting: Make the elements have a comparison. The comparable interface must be implemented to override the compareto method. This method is also called the natural sequence of elements, or the default sequence.

 

The second sorting method of treeset: When the element itself does not have a comparison, or the comparison is not required. In this case, the set itself needs to be compared. When the set is initialized, a comparison method is available.

Define a class to implement the comparator interface and overwrite the compare method.

The comparator is defined, and the comparator object is passed as a parameter to the constructor of the treeset set.

when both sorts exist, dominated by comparator .

Import Java. util. comparator; import Java. util. iterator; import Java. util. treeset; public class treesetdemo {/*** @ Param ARGs */public static void main (string [] ARGs) {treeset <student> TS = new treeset <student> (New mycompare (); // The comparator is preferred when both methods are used. Ts. add (new student ("zhangsan", 18); Ts. add (new student ("zhangsan", 18); Ts. add (new student ("Lisi", 18); Ts. add (new student ("Lisi", 21); Ts. add (new student ("zhangsan", 19); Ts. add (new student ("zhangsan", 20); For (iterator <student> it = ts. iterator (); it. hasnext ();) {student s = it. next (); system. out. println (S. getname () + "... "+ S. getage () ;}} class student implements comparable <student> // force student to be comparable. {Private string name; private int 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;} student (string name, int age) {This. name = Name; this. age = age;} public int compareto (student s) // the basis for ensuring the uniqueness of elements. {/* If (! (OBJ instanceof student) throw new runtimeexception ("not a student object"); student s = (student) OBJ; */If (this. age> S. age) return 1; else if (this. age = S. age) // return 0; return this. name. compareto (S. name); // when the main conditions are compared, the secondary conditions must be compared. Return-1 ;}} class mycompare implements comparator <student> {public int compare (student S1, student S2) {int num = s1.getname (). compareto (s2.getname (); If (num = 0) return New INTEGER (s1.getage ()). compareto (New INTEGER (s2.getage (); Return num ;}}

 

Generic:

New features after JDK. It is a type of security mechanism used to solve security problems.

Benefits:

1. classcastexception occurs during the running period, and the compilation period is stolen. ConvenientProgramPersonnel solve the problem. Reduce running problems and ensure security.

2. This avoids the trouble of forced conversion.

Generic Format: Use <> to define the data type to be referenced.

When can I write generics when I use objects provided by Java?

It is usually common in the Collection framework. As long as you see <>, You need to define the generic type. In fact, <> is used to receive type.

When using a set, pass the data type to be stored in the set as a parameter to <>.

When to define generic classes: When the referenced data types to be operated in the class are uncertain, define the object to complete the extension. Now define the generic type to complete the extension.

Static methods cannot be generic defined on the generic class. If the data type of the application operated by the static method is unknown, you can define the generic type on the method.

The generic type defined by the generic class is valid throughout the class. If it is used by methods, all the types to be operated will be fixed after the object of the generic class is clear about the specific type to be operated.

In order to allow different methods to operate on different types, the types are not sure. Then you can define generics on methods.

 

 

 
Public class genericdemo {/*** @ generic class and Method */public static void main (string [] ARGs) {demo <integer> d = new demo <integer> (); D. show (2);/* D. show ("hah"); D. show ('A'); D. show (2.333); */d. print ("hah"); demo. methot ("hahhh") ;}} class demo <t> {public void show (t) {system. out. println ("show" + T);} Public <q> void print (Q) {system. out. println ("print" + q);} public static <t> void methot (t) {system. out. println ("method" + T );}}

 

 

<?> Wildcard. It can also be understood as a placeholder.

Limits on Generics:

? Extends E: it can receive the sub-types of E or E.

? Super E: you can accept the parent type of E or E. Lower limit.

Note: comparator when defining a comparison class, you can import the parent class of the element to be compared in the class, but the method used by the element must also be the parent class.

Import Java. util. arraylist; import Java. util. comparator; import Java. util. iterator; import Java. util. treeset; public class genericdemo2 {/*** @ Param ARGs */public static void main (string [] ARGs) {arraylist <student1> Al = new arraylist <student1> (); al. add (New student1 ("zhangsan", 20); Al. add (New student1 ("zhangsan1", 21); Al. add (New student1 ("zhangsan2", 22); printcoll (Al); treeset <student1> TS = new treeset <Stu Dent1> (new COM (); Ts. add (New student1 ("zhangsan", 20); Ts. add (New student1 ("Lisi", 21); Ts. add (New student1 ("Lisi", 18); printcoll2 (TS);} public static void printcoll (arraylist <? Extends person> Al) {for (iterator <? Extends person> it = Al. iterator (); it. hasnext ();) {system. out. println (it. next (). getage () ;}} public static void printcoll2 (treeset <? Extends person> Al) {for (iterator <? Extends person> it = Al. iterator (); it. hasnext ();) {system. out. println (it. next (). getage () ;}} class person {private string name; 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;} private int age; person (string name, int age) {This. name = Name; this. age = age ;}} class student1 extends person {stud Ent1 (string name, int age) {super (name, age) ;}} class com implements comparator <person> // The generic type here can use the parent class of the specified class .? Extends student; {public int compare (person P1, person P2) {return p1.getname (). compareto (p2.getname ());}}

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.