A multi-Problem solving method of Java set frame _java

Source: Internet
Author: User

question: The student number of 30 students in a class is 20070301-20070330, all of them take the Java programming course, give the results of all the students (available random number generation, range 60-100), Please write the program to the students in this class grades from low to high order print output.

Requirements: use list, MAP, set to achieve, printed information including the number, name and results.

1, using the list set to achieve

Import java.util.ArrayList; 
Import java.util.Collections; 
Import Java.util.Comparator; 
Import Java.util.Iterator; 
Import java.util.LinkedList; 
 
 
 
Import Java.util.TreeMap; public class test2{public static void Main (string[] args) {/* Here is implemented with ArrayList * * arraylist& Lt 
    Student>al=new arraylist<student> (); 
    for (int i=20070301,j=10;i<=20070330;i++,j++) {Al.add (new Student (i, (int) (40*math.random () +60), "classmate" +j)); 
    //arraylist sorting is implemented using the sort () method in collections. 
    Collections.sort (AL, New Sortbygrade ()); 
     
    for (Student sd:al) System.out.println (SD); 
    * * linkedlist<student> lt=new linkedlist<student> (); 
    for (int i=20070301,j=10;i<=20070330;i++,j++) {Lt.add (new Student (i, (int) (40*math.random () +60), "classmate" +j)); 
    //To sort the list collections.sort (LT, New Sortbygrade ()); 
     
  Output list for (Student sd:lt) System.out.println (SD); } 
   
}//Student class student{int num,grade; 
   
  String name; 
    Constructor public Student (int num,int grade,string name) {this.num=num; 
    This.name=name; 
  This.grade=grade; 
    //Here must overwrite public String toString () {//System.out.println ("HI"); 
  Return "School Number:" +this.num+ "T" + "Name:" +this.name+ "" + "results:" +this.grade; }///Create a Comparator class class Sortbygrade implements comparator<student>{@Override public int compare (stude 
    NT S1, Student S2) {if (s1.grade>s2.grade) return 1; 
    if (S1.grade<s2.grade) return-1; 
    if (S1.grade==s2.grade) return S1.name.compareTo (S2.name);     
     
  return 0; 
 } 
   
}

Output as shown in figure:

Summary of the list collection Framework :

1, the list is actually a dynamic array, elements can be directly through the for loop out, without the need for iteration.
2. When outputting the list collection, the ToString () method for storing objects in the collection is invoked by default, so it is necessary to overwrite them in the class.
If you do not overwrite the ToString () method, you must use the

for (int i=0;i<lt.size (); i++) 
{ 
  Student s=lt.get (i); 
  System.out.println ("School Number:" +s.num+ "  Name:" +s.name+ "  results:" +s.grade); 
} 

3. The order of the list collection requires the help of the collections tool class, the Collections.sort (List,new Comparator ()) method. So you need to customize a comparer class to define your own comparison rules.

2, using set set to implement
(1) using TreeSet to achieve

Package com.package1; 
 
Import java.util.*; public class Stuscore {public static void main (string[] args) {treeset<student> ts=new treeset<student& 
  gt; (new Com ()); add element in for (int i=20070301,j=1;i<=20070330;i++,j++) {Ts.add (new Student (I, "Classmate" +j, (int) (40*math.random () +6 
  0)); 
  ()///iteration loop to remove iterator<student> it=ts.iterator (); 
    while (It.hasnext ()) {Student o1=it.next (); 
 
  System.out.println ("School Number:" +o1.num+ "" + "Name:" +o1.name+ "" + "" + "Results:" +o1.grade); 
}}//Student class Student {int num; 
int grade; 
 
 
String name; 
  public Student (int num, String name,int grade) {this.num=num; 
  This.name=name; 
This.grade=grade; Class Com implements Comparator {@Override public int compare (object O1, Object O2) {Student s1= (Student 
  ) O1; 
  Student s2= (Student) O2; 
  if (S1.grade>s2.grade) return 1; 
  if (S1.grade<s2.grade) return-1; if (S1.grade==s2.grade) {return new IntEger (S1.num). CompareTo (New Integer (S2.num)); 
return 0; 
 } 
}

The output results are:

Student No.: 20070307 Name: Classmate 16 Grade: 60
Student No.: 20070309 Name: Classmate 18 Grade: 60
Student No.: 20070314 Name: Classmate 23 Grade: 61
Student No.: 20070318 Name: Classmate 27 Grade: 61
Student No.: 20070322 Name: Classmate 31 Grade: 61
Student No.: 20070306 Name: Classmate 15 Grade: 62
Student No.: 20070310 Name: Classmate 19 Grade: 64
Student No.: 20070302 Name: Classmate 11 Grade: 66
Student No.: 20070308 Name: Classmate 17 Grade: 68
Student No.: 20070321 Name: Classmate 30 Grade: 68
Student No.: 20070330 Name: Classmate 39 Grade: 69
Student No.: 20070303 Name: Classmate 12 Grade: 70
Student No.: 20070320 Name: Classmate 29 Grade: 70
Student No.: 20070323 Name: Classmate 32 Grade: 77
Student No.: 20070313 Name: Classmate 22 Grade: 78
Student No.: 20070304 Name: Classmate 13 Grade: 79
Student No.: 20070324 Name: Classmate 33 Grade: 83
Student No.: 20070326 Name: Classmate 35 Grade: 84
Student No.: 20070327 Name: Classmate 36 Grade: 85
Student No.: 20070311 Name: Classmate 20 Grade: 88
Student No.: 20070305 Name: Classmate 14 Grade: 89
Student No.: 20070329 Name: Classmate 38 Grade: 89
Student No.: 20070316 Name: Classmate 25 Grade: 90
Student No.: 20070301 Name: Classmate 10 Grade: 95
Student No.: 20070312 Name: Classmate 21 Grade: 96
Student No.: 20070317 Name: Classmate 26 Grade: 97
Student No.: 20070319 Name: Classmate 28 Grade: 97
Student No.: 20070325 Name: Classmate 34 Grade: 98
Student No.: 20070315 Name: Classmate 24 Grade: 99
Student No.: 20070328 Name: Classmate 37 Grade: 99

Summary of the TreeSet :
1, elements can not be repeated, and treeset is orderly.
2. Two sorts of sorting methods:
(1) Customize a comparator class, such as Class Com implementscomparator{}, to implement the Compare (object O1, Object O2) method where you define the comparison rules.
(2) to make the elements of their own comparative.
Step: Implement the comparable interface to the elements in the add into TreeSet and overwrite the CompareTo method. This order is also the natural order of the elements, or the default order.

The difference between Method 1 and Method 2:

Two methods have advantages and disadvantages, with comparable simple, as long as the object of the implementation of comparable interface directly becomes a can be compared to the object, but need to modify the source code.

The advantage of using comparator is that you do not need to modify the source code, but also implement a comparator, when a custom object needs to be compared, the comparator and objects passed through the past can be compared to the size, and in the comparator users can implement the complex can be universal logic, So that it can match some of the simpler objects, so you can save a lot of repetitive work.

(2) using hashset to achieve

Package com.package1; 
 
 
 
 
Import java.util.*; public class Stuscore {public static void main (string[] args) {hashset<student> hs=new hashset< 
    Student> (); add element in for (int i=20070301,j=1;i<=20070330;i++,j++) {Hs.add (new Student (I, "Classmate" +j, (int) (40 
    *math.random () +60)); 
     
    } arraylist<student>li=new ArrayList (HS); 
     
    Collections.sort (Li, New Sortbygrade ()); 
     
 
  for (Student Ss:li) System.out.println (ss); 
  }//Student class Student {int num; 
  int grade; 
 
   
  String name; 
    public Student (int num, String name, int grade) {this.num=num; 
    This.name=name; 
  This.grade=grade; 
    Public String toString () {//system.out.println ("hi"); 
  Return "School Number:" +this.num+ "T" + "Name:" +this.name + "" + "results:" +this.grade; 
     Class Sortbygrade implements comparator{@Override public int Compare (object O1, Object O2) {
    Student s1= (Student) O1; 
    Student s2= (Student) O2; 
    if (S1.grade>s2.grade) return 1; 
if (S1.grade<s2.grade) return-1; 
  if (S1.grade==s2.grade) return 0; 
 } 
   
}

The output results are as follows:
Student No.: 20070310 Name: Classmate 19 Grade: 60
Student No.: 20070330 Name: Classmate 39 Grade: 62
Student No.: 20070326 Name: Classmate 35 Grade: 63
Student No.: 20070317 Name: Classmate 26 Grade: 64
Student No.: 20070318 Name: Classmate 27 Grade: 65
Student No.: 20070322 Name: Classmate 31 Grade: 65
Student No.: 20070301 Name: Classmate 10 Grade: 67
Student No.: 20070328 Name: Classmate 37 Grade: 68
Student No.: 20070304 Name: Classmate 13 Grade: 68
Student No.: 20070319 Name: Classmate 28 Grade: 69
Student No.: 20070313 Name: Classmate 22 Grade: 70
Student No.: 20070303 Name: Classmate 12 Grade: 71
Student No.: 20070312 Name: Classmate 21 Grade: 71
Student No.: 20070329 Name: Classmate 38 Grade: 72
Student No.: 20070306 Name: Classmate 15 Grade: 72
Student No.: 20070324 Name: Classmate 33 Grade: 72
Student No.: 20070305 Name: Classmate 14 Grade: 75
Student No.: 20070315 Name: Classmate 24 Grade: 75
Student No.: 20070314 Name: Classmate 23 Grade: 78
Student No.: 20070307 Name: Classmate 16 Grade: 80
Student No.: 20070311 Name: Classmate 20 Grade: 81
Student No.: 20070302 Name: Classmate 11 Grade: 83
Student No.: 20070309 Name: Classmate 18 Grade: 84
Student No.: 20070320 Name: Classmate 29 Grade: 85
Student No.: 20070321 Name: Classmate 30 Grade: 85
Student No.: 20070316 Name: Classmate 25 Grade: 86
Student No.: 20070327 Name: Classmate 36 Grade: 90
Student No.: 20070308 Name: Classmate 17 Grade: 94
Student No.: 20070323 Name: Classmate 32 Grade: 94
Student No.: 20070325 Name: Classmate 34 Grade: 95

Summary of the hashset :
1, the elements in the hashset can not be repeated, if repeatedly added, will only show one.
The principle is as follows:
HashSet: The underlying data structure is a hash table. is not thread safe. Different steps.
2, HashSet is how to ensure the uniqueness of the element?
Answer: It is through the elements of the two methods, hashcode and equals to complete.
If the element's Hashcode value is the same, the equals is judged to be true. Equals is not invoked if the hashcode value of the element is different.
3, the sort of hashset, by converting set set to list collection, by means of Collections.sort () method to achieve sorting.

3, using TreeMap to achieve

Package com.package1; 
Import Java.util.Comparator; 
Import Java.util.Iterator; 
Import Java.util.Set; 
 
Import Java.util.TreeMap;  public class Testtreemap {public static void main (string[] args) {//1. Create a collection treemap<student,integer> 
    Tm=new treemap<student,integer> (); 
       for (int i=20070301,j=10;i<=20070330;i++,j++) {int grade= (int) (40*math.random () +60); 
    2, add elements to the collection Object Tm.put (new Student (grade, "classmate" +j), I); 
    //3. Traversal collection, sorting complete set<student> k=tm.keyset (); 
     
    Iterator<student> It=k.iterator (); 
      while (It.hasnext ()) {Student key=it.next (); 
       
      Integer Num=tm.get (key); 
    System.out.println ("School Number:" +num+ "" + "Name:" +key.name+ "" + "Results:" +key.grade); 
  {}} class Student implements comparable<student>{int grade; 
   
  String name; 
    Public Student (int grade,string name) {This.grade =grade; 
  This.name=name; } @Override public inT CompareTo (Student o) {if (this.grade>o.grade) return 1;  
      if (This.grade==o.grade) {//When the score is the same, sort by name return This.name.compareTo (o.name);  
 
  } return-1; 
 } 
   
 
   
}

The output results are:

Student No.: 20070303 Name: Classmate 12 Grade: 61
Student No.: 20070323 Name: Classmate 32 Grade: 61
Student No.: 20070317 Name: Classmate 26 Grade: 62
Student No.: 20070309 Name: Classmate 18 Grade: 64
Student No.: 20070301 Name: Classmate 10 Grade: 67
Student No.: 20070304 Name: Classmate 13 Grade: 69
Student No.: 20070322 Name: Classmate 31 Grade: 69
Student No.: 20070328 Name: Classmate 37 Grade: 70
Student No.: 20070305 Name: Classmate 14 Grade: 71
Student No.: 20070319 Name: Classmate 28 Grade: 73
Student No.: 20070321 Name: Classmate 30 Grade: 74
Student No.: 20070310 Name: Classmate 19 Grade: 81
Student No.: 20070315 Name: Classmate 24 Grade: 82
Student No.: 20070307 Name: Classmate 16 Grade: 84
Student No.: 20070330 Name: Classmate 39 Grade: 84
Student No.: 20070312 Name: Classmate 21 Grade: 85
Student No.: 20070324 Name: Classmate 33 Grade: 87
Student No.: 20070306 Name: Classmate 15 Grade: 88
Student No.: 20070308 Name: Classmate 17 Grade: 90
Student No.: 20070327 Name: Classmate 36 Grade: 90
Student No.: 20070318 Name: Classmate 27 Grade: 91
Student No.: 20070316 Name: Classmate 25 Grade: 92
Student No.: 20070320 Name: Classmate 29 Grade: 92
Student No.: 20070314 Name: Classmate 23 Grade: 93
Student No.: 20070313 Name: Classmate 22 Grade: 94
Student No.: 20070302 Name: Classmate 11 Grade: 95
Student No.: 20070325 Name: Classmate 34 Grade: 95
Student No.: 20070329 Name: Classmate 38 Grade: 97
Student No.: 20070326 Name: Classmate 35 Grade: 98
Student No.: 20070311 Name: Classmate 20 Grade: 99

Summary of the TreeMap :
1, TreeMap The default key to sort, so you can put the custom object into the key, will represent the number of the integral type into value. When you sort the key, you can specify a property in the custom object to sort.
2. The map collection uses the Put () method to add elements.
3, the map set out principle: The Map collection into set set. Out through the iterator. Two ways to remove a map collection:
(1) set<k> keyset: all the keys in the map are saved to the set collection. Because the set has an iterator. All the keys can be iterated out, in accordance with the Get method. Gets the corresponding value for each key.
(2) set<map.entry<k,v>> EntrySet: The mapping relationship in the map collection is stored in the set set, and the data type of the relationship is: map.entry

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.