*set Collection
A duplicate element collection, and contains at most one null element, this class implements the set interface, there is a hash table support, the predefined types in Java, such as String, Integer can be used within the collection, but for the type that you create, notice that the set
There is a need for a way to maintain the order of storage, and how the order of storage is maintained is subject to change between the different implementations of the set. As a result, different set implementations not only have different behaviors, but they also have different requirements for the type of elements that can be placed in a particular set that I'm crazy about.
Inheriting from the collection collection, the hash table is instantiated by its self-implementing class HashSet collection, hashset the bottom of the collection is the implementation of HashMap
*list The difference between a set and a set set
List collection: elements are not unique and ordered (storage and extraction are inconsistent)
Set set: The element is unique, and storage and extraction are inconsistent
Custom storage objects do not override hashcode with the Equals method and will not guarantee the uniqueness of the element
package set;import java.util.hashset;import java.util.set;public class setdemo { Public static void main (String[] args) {//Create Set collection object, set set is implemented by HashSet Set<String> set=new HashSet<String> (); //adding elements to the collection set.add ("Hello"); set.add ("Hello"); set.add ("Javase"); set.add ("World"), set.add ("World"); set.add ("Java"); //enhanced for loop, traversing elements for (string str : set) { system.out.println (str); }}}
650) this.width=650; "Src=" Https://s1.51cto.com/oss/201711/16/054cbdbea1764a144d71d8ee6de12ad5.png-wh_500x0-wm_3 -wmp_4-s_3413339850.png "title=" Set.png "alt=" 054cbdbea1764a144d71d8ee6de12ad5.png-wh_ "/>
The Add () method of the *hashset collection, which relies on the hashmap<k,v> of the put (K key,v value) of the double-column collection.
The bottom of put (K key,v value) is dependent on hashcod () and Equals ()
When passing an element, the first thing to judge is whether each element corresponds to the same hashcode value, and if the hashcode value is the same, compare their equals () method. Since the collection now stores a string type, the string type itself overrides the Equals () method, so the default comparison is whether the content is the same, where the final return is the element stored for the first time, thus guaranteeing the uniqueness of the elements within the collection
(set that is designed for quick Find, the element that is stored in hashset must be defined hashcode ()))
Package Set;import Java.io.serializable;import Java.util.hashset;public class HashSetDemo1 implements serializable{ Private static final Long serialversionuid = 1l;transient int num;p ublic static void Main (string[] args) {//Create collection Object Hashse T<string> hs=new hashset<string> ();//add Element Hs.add ("Hello") to the collection; Hs.add ("World"); Hs.add ("Hello"); Hs.add ( "Javase"); Hs.add ("Java"); Hs.add ("word"); Hs.add ("javawab");//strengthen for loop, traverse collection for (String str:hs) {System.out.println ( str);}}}
650) this.width=650; "Src=" Https://s2.51cto.com/oss/201711/16/0231188701784210c516c7dccf9105ed.png-wh_500x0-wm_3 -wmp_4-s_19405683.png "title=" Hashset.png "alt=" 0231188701784210c516c7dccf9105ed.png-wh_ "/>
Requirements: Store custom objects and traverse (using HashSet collection)
hashset<student> ()
package set;//Custom Class Public class student {private string name;private int age;public student () {super ();} Public student (String name, int age) {super (); this.name = name;this.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;}} Test class package set;import java.util.hashset;public class hashsetdemo {public Static void main (String[] args) {//Create HashSet Collection Object Hashset<student> hs=new hashset <Student> ();//Create Student Object Student st1=new student ("Apple", 3); Student st2=new student ("Pear", 24); Student st3=new student ("Orange", 15); Student st4=new student ("Orange", 15); Student st5=new student ("Orange", 17); STudent st6=new student ("Walnut", 21);//add Element Hs.add (ST1) to the collection, Hs.add (ST2); Hs.add (ST3); Hs.add (ST4); Hs.add (ST5 ); Hs.add (ST6);//enhanced for loop, traversing set for (STUDENT&NBSP;ST&NBSP;:HS) {System.out.println (St.getname () + "-----" +st.getage ()) ;}}}
650) this.width=650; "Src=" Https://s1.51cto.com/oss/201711/16/2a01d75d0dff1f2122044694235a1d99.png-wh_500x0-wm_3 -wmp_4-s_1639422515.png "title=" Hashset.png "alt=" 2a01d75d0dff1f2122044694235a1d99.png-wh_ "/>
*treeset (focus of Set set)
(hold order set, bottom dependent on treemap instance, bottom is red black tree structure, can extract ordered sequence from set)
* There are two construction methods (depending on what kind of construction method the developer uses)
*public TreeSet (): Non-parametric construction: Sorting according to the natural order of its elements
Package Treeset;import Java.util.treeset;public class Treesetdemo {public static void main (string[] args) {//Call no-argument constructs The elements will be sorted in a natural sort treeset<integer> ts=new treeset<integer> ();//Add elements to the collection Ts.add (); Ts.add (64); Ts.add (98); Ts.add (23);//traverse element for (Integer in:ts) {System.out.print (in+ "");}}
650) this.width=650; "Src=" Https://s2.51cto.com/oss/201711/16/cec7cb3cb1ecbe47fd48ed15008e8999.png-wh_500x0-wm_3 -wmp_4-s_1506229888.png "title=" Treeset.png "alt=" Cec7cb3cb1ecbe47fd48ed15008e8999.png-wh_ "/>
*publict TreeSet (comparaptr<e> com)
* for TreeSet collections to achieve a natural sort, the custom types stored in the collection must implement the comparable interface and must override the CompareTo () method in the interface
* Rewrite the comparable interface in the CompareTo method in the code needs to give itself (sort mode)
package treeset;//for TreeSet collection storage custom objects must implement an interface: Compareable interface public class student implements Comparable<Student> {private String name;private int age;public Student () {super ();} Public student (String name, int age) {super (); this.name = name;this.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;} Public int compareto (student st) {//sort the code out, you need to define the sort criteria//main condition: Sort by student's age from small to large int num =st.age - this.age ;//age from big to small //comparison of the main conditions, but also to compare the secondary conditions: the name of the content also need the same int num2 = num==0 this.name.compareto (st.name): num ; return num2 ;}} package treeset;import java.util.treeset;public class treesetdemo1 { Public static void main (String[] args) {//Create object treeset<student> ts=new Treeset<student> ();//Create Student Object Student st1=new student ("Shangui ballad", 24); Student st2=new student ("Shangui ballad", 25); Student st3=new student ("Yi Mark Xi", 24); Student st4=new student ("Shangui ballad", 24); Student st5=new student ("Thousand June", 15); Student st6=new student ("Thousand June", 17); Student st7=new student ("Chen Yue", 16);//add Element Ts.add (ST1) to the collection, Ts.add (ST2); Ts.add (ST3); Ts.add (ST4); Ts.add ( ST5); Ts.add (ST6); Ts.add (ST7);//traverse element, sort by age descending for (student st : ts) {System.out.println (St.getname () + "----" +st.getage ());}}}
650) this.width=650; "Src=" Https://s1.51cto.com/oss/201711/16/e2a0fab3feedc5811f64736ff8a937db.png-wh_500x0-wm_3 -wmp_4-s_2695850569.png "title=" Treeset1.png "alt=" E2a0fab3feedc5811f64736ff8a937db.png-wh_ "/>
Keyboard Input 5 Student information (name, language score, Math score, English score), according to the total score from high to low output to the console
Package treeset;public class student1 {private string name;private int chinesescore;private int englishscore;private int mathscore;public student1 () {super ();} Public student1 (string name, int chinesescore, int englishscore,int Mathscore) {super ();this.name = name;this.chinesescore = chinesescore; This.englishscore = englishscore;this.mathscore = mathscore;} Public string getname () {return name;} Public void setname (String name) {this.name = name;} Public int getchinesescore () {return chinesescore;} Public void setchinesescore (Int chinesescore) {this.chinesescore = chinesescore;} Public int getenglishscore () {return englishscore;} Public void setenglishscore (Int englishscore) {this.englishscore = enGlishscore;} Public int getmathscore () {return mathscore;} Public void setmathscore (Int mathscore) {this.mathscore = mathscore;} Public int getsum () {Return this.chinesescore+this.englishscore+this.mathscore;}} package treeset;import java.util.comparator;import java.util.scanner;import Java.util.treeset;public class treesetdemo2 {public static void main (String[] args) {// Create TreeSet collection objects, using the// comparer to sort anonymous inner classes treeset<student1> ts = new TreeSet<Student1> (new comparator<student1> () {public int Compare (STUDENT1&NBSP;ST1,&NBSP;STUDENT1&NBSP;ST2) {// main conditions: score from highest to first to sort int num = St2.getsum () - st1.getsum ();// the same score, not necessarily the same language scores, comparative Chinese results int num2 = num == 0 ? st1.getchinesescore () - st2.getchinesescore () : num;// the same scores, ChineseSame score, comparative math score Int num3 = num2 == 0 ? st1.getenglishscore () - St2.getenglishscore () : num2;// scores same, the same language scores, the same math, comparative English int num4 = num3 == 0 ? st1.getmathscore () - st2.getmathscore () : num3;// scores are the same as all subjects, Not necessarily the same person, the name content is the same int num5 = num4 == 0 ? st1.getname (). CompareTo (St2.getname ()): &NBSP;NUM4;RETURN&NBSP;NUM5;}); SYSTEM.OUT.PRINTLN ("Input student Information start:");// keyboard input 5 Students ' information, name, language score, Math score, English score for (int i=1;i<=5;i++) {// Create keyboard Entry object// to facilitate data entry, data types are received Scanner sc = new scanner (system.in) using string type; System.out.println ("Please enter the name of" +i+ "Student:"); String name = sc.nextline (); SYSTEM.OUT.PRINTLN ("Please input" +i+ "Student's language Score:"); String chinesestr = sc.nextline (); SYSTEM.OUT.PRINTLN ("Please input" +i+ "Student's English Score:"); String mathstr = sc.nextline (); System.out.println ("Please enter section" +i+&nbsP; " Student's math score: "); String englishstr = sc.nextline ();// creates a student object that encapsulates the information into the student object student1 st = New student1 (); St.setname (name); St.setchinesescore (Integer.parseint (CHINESESTR)); St.setenglishscore ( Integer.parseint (MATHSTR)); St.setmathscore (Integer.parseint (ENGLISHSTR));// Add student objects to the collection Ts.add (ST);} System.out.println ("End of Student Information entry:"); System.out.println ("The total score of student information from high to the bottom ranked as follows:");//\t is a tab System.out.println ("name \ t language score \ t math score \ T English score");// enhanced for traversal set for (student1 std : ts) {system.out.println (Std.getname () + "\ T" + Std.getchinesescore () + "\ T" + std.getenglishscore () + "\ T" + Std.getmathscore ());}}}
650) this.width=650; "Src=" Https://s1.51cto.com/oss/201711/16/cc5977e980e0cbce6229a48125a1e6aa.png-wh_500x0-wm_3 -wmp_4-s_3008733131.png "title=" Treeset2.png "alt=" Cc5977e980e0cbce6229a48125a1e6aa.png-wh_ "/>
* Guaranteed element is unique to custom object under what circumstances
Member variables are the same, they are considered the same element
Primary condition given, need to analyze secondary condition
* Sort using comparators, using anonymous inner classes
Creating a TreeSet Collection object using a parameter construct
*linkedhashset<e>
Implementation of a hash table list with a predictable sequence of set interfaces
* The uniqueness of the element is guaranteed by the hash, and a list of links guarantees the order of the elements
Package Linkedhashset;import Java.util.linkedhashset;public class Linkedhashsetdemo {public static void main (string[] args) {//Create Linkedhashset Collection Object linkedhashset<string> link = new linkedhashset<string> ();//add element to the collection Link.add ("Hello"), Link.add ("Hello"), Link.add ("World"), Link.add ("Java"), Link.add ("Javase"); Link.add ("the World");//enhanced for loop, Traverse collection for (String s:link) {System.out.println (s);}}}
650) this.width=650; "Src=" Https://s4.51cto.com/oss/201711/16/841811aed13c670bbde769af5867906f.png-wh_500x0-wm_3 -wmp_4-s_471177932.png "title=" Linkedset.png "alt=" 841811aed13c670bbde769af5867906f.png-wh_ "/>
Java Learning Lesson 15