java中集合的排序
java中集合的排序
import java.util.Set;
import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
import java.util.Collections;
/**
* 將要完成
* 1.通過Collections.sort()方法,對Integer泛型的List進行排序
* 2.對String泛型的List進行排序
* 3.對其他類型泛型的List進行排序,以Student為例
*/
public class CollectionsTest
{
? ? /**
?? ?* 1.通過Collections.sort()方法,對Integer泛型的List進行排序;
?? ?* 創建一個Integet泛型的List,插入十個100以內的不重復隨機整數,
?? ?* 調用Collections.sort()方法對其進行排序
?? ?*/
?? ?public void testSort1() {
?? ? ? List<Integer> integerList = new ArrayList<Integer>();
?? ? ? //插入十個100以內的不重復隨機整數
?? ? ? Random random = new Random();
?? ? ? Integer k;
?? ? ? for(int i=0;i<10;i++) {
?? ? ? ? do{
?? ??? ? ? k = random.nextInt(100);
?? ??? ? }while(integerList.contains(k));
?? ??? ? integerList.add(k);
?? ??? ? System.out.println("成功添加整數:"+k);
?? ? ? }//for end
? ? ? ?System.out.println("--------排序前--------");
?? ? ? for(Integer integer:integerList) {
?? ? ? ? ?System.out.println("元素:"+integer);
?? ? ? }
?? ? ? Collections.sort(integerList);
?? ? ? System.out.println("--------排序后--------");
?? ? ? for(Integer integer:integerList) {
?? ? ? ? ?System.out.println("元素:"+integer);
?? ? ? }
?? ?}//testSort1 end
?? ?/**
?? ?* 2.對String泛型的List進行排序
?? ?* 創建String泛型的List,添加三個亂序的String元素
?? ?* 調用sort()方法,再次輸出排序后的順序
?? ?*/
? ? ?public void testSort2() {
?? ? ? ?List<String> stringList = new ArrayList<String>();
?? ??? ?stringList.add("microsoft");
?? ??? ?stringList.add("google");
?? ??? ?stringList.add("lenovo");
?? ??? ?System.out.println("--------排序前--------");
? ? ? ? for(String string:stringList) {
?? ? ? ? ?System.out.println("元素:"+string);
?? ? ? ?}
?? ??? ?Collections.sort(stringList);
?? ? ? ?System.out.println("--------排序后--------");
?? ??? ?for(String string:stringList) {
?? ? ? ? ?System.out.println("元素:"+string);
?? ? ? ?}
?? ? }//testSort2 end
?? ? /**
?? ? ?* 3.對其他類型泛型的List進行排序,以Student為例
?? ?
? ? ?public void testSort3() {
?? ? ? ?List<Student> studentList = new ArrayList<Student>();
?? ??? ?studentList.add(new Student(1+"","小明"));
?? ??? ?studentList.add(new Student(2+"","小紅"));
?? ??? ?studentList.add(new Student(3+"","小蘭"));
?? ??? ?System.out.println("--------排序前--------");
? ? ? ? for(Student student:studentList) {
?? ? ? ? ?System.out.println("學生:"+student.name);
?? ? ? ?}
?? ??? ?Collections.sort(studentList); //這塊為什么報錯呢?改變了Student.java的寫法這塊不報錯了
?? ? }//testSort3 end
?? ? */
?? ?/**
?? ?* 3.對其他類型泛型的List進行排序,以Student為例
?? ?*/
? ? ?public void testSort3() {
?? ? ? ?List<Student> studentList = new ArrayList<Student>();
?? ??? ?Random random = new Random();
?? ??? ?studentList.add(new Student(random.nextInt(1000)+"","Mike"));
?? ??? ?studentList.add(new Student(random.nextInt(1000)+"","Angela"));
?? ??? ?studentList.add(new Student(random.nextInt(1000)+"","Lucy"));
?? ??? ?studentList.add(new Student(10000+"","Beyonce"));
?? ??? ?System.out.println("--------排序前--------");
? ? ? ? for(Student student:studentList) {
?? ? ? ? ?System.out.println("學生:"+student.id+":"+student.name);
?? ? ? ?}
?? ??? ?Collections.sort(studentList); //這塊為什么報錯呢?改變了Student.java的寫法這塊不報錯了
?? ??? ?System.out.println("--------排序后--------");
?? ??? ?for(Student student:studentList) {
?? ? ? ? ?System.out.println("學生:"+student.id+":"+student.name);
?? ? ? ?}
?? ??? ?Collections.sort(studentList,new StudentComparator());
?? ??? ?System.out.println("--------按照姓名排序后--------");
?? ??? ?for(Student student:studentList) {
?? ? ? ? ?System.out.println("學生:"+student.id+":"+student.name);
?? ? ? ?}
?? ? }//testSort3 end
?? ?public static void main(String[] args) {
?? ? ?//System.out.println("曾經擁有");
?? ? ?CollectionsTest ct = new CollectionsTest();
?? ? ?//ct.testSort1();
?? ? ?//ct.testSort2();
?? ? ?ct.testSort3();
?? ?}//main end
?? ?
}//CollectionsTest end
總結
以上是生活随笔為你收集整理的java中集合的排序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 超频三散热器怎么样 有名的散热领域品牌
- 下一篇: 网上银行跨行转账需要手续费吗