Java常用工具类--集合排序

集合排序:

集合排序的方法:


案例:

例1:对存放在list的数据进行排序

代码:

package com.imooc.sort;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class IntSort {

	public static void main(String[] args) {
		// 对存储在List中的整型数据进行排序
		List<Integer> list = new ArrayList<Integer>();
		list.add(5);
		list.add(9);
		list.add(3);
		list.add(1);
		System.out.println("排序前:");
		for (int n : list) {
			System.out.print(n + " ");
		}
		System.out.println();
		System.out.println("*********");
		// 对list中数据进行排序
		System.out.println("排序后:");
		Collections.sort(list);
		for (int n : list) {
			System.out.print(n + " ");
		}

	}

}

输出结果:

排序前:
5 9 3 1 
*********
排序后:
1 3 5 9 

例2:对存放在list的数据进行排序

代码:

package com.imooc.sort;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class StringSort {

	public static void main(String[] args) {
		// 对存放在list的数据进行排序
		List<String> list = new ArrayList<String>();
		list.add("orange");
		list.add("blue");
		list.add("yellow");
		list.add("gray");
		
		System.out.println("排序前:");
		for (String s : list) {
			System.out.print(s + " ");
		}
		System.out.println();
		System.out.println("*********");
		// 对list中数据进行排序
		System.out.println("排序后:");
		Collections.sort(list);
		for (String s : list) {
			System.out.print(s + " ");
		}


	}

}

输出结果:

排序前:
orange blue yellow gray 
*********
排序后:
blue gray orange yellow 

对类的实例化对象进行排序:


Comparator接口介绍:


对宠物猫按照名称进行排序:

Cat类:

package com.imooc.sort;

public class Cat {
	private String name;
	private int month;
	private String species;

	// setter getter
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public String getSpecies() {
		return species;
	}

	public void setSpecies(String species) {
		this.species = species;
	}

	// 无参构造
	public Cat() {
		super();
	}

	public Cat(String name, int month, String species) {
		this.setName(name);
		this.setMonth(month);
		this.setSpecies(species);
	}

	// 重写toString
	@Override
	public String toString() {
		return "[名字:" + name + ", 年龄:" + month + ", 品种:" + species + "]";
	}

}

NameComparator类:

package com.imooc.sort;

import java.util.Comparator;

public class NameComparator implements Comparator<Cat> {
	@Override
	public int compare(Cat o1, Cat o2) {
		// 按名字升序进行排序
		String name1 = o1.getName();
		String name2 = o2.getName();
		//若想逆序输出,加个 负号  就可以了
		return name1.compareTo(name2);
	}

}

测试代码:

package com.imooc.sort;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CatTest {

	public static void main(String[] args) {
		// 按宠物猫名字升序排序
		Cat huahua = new Cat("huahua", 5, "英国短毛猫");
		Cat fanfan = new Cat("fanfan", 2, "中华田园猫");
		Cat maomao = new Cat("maomao", 3, "中华田园猫");

		List<Cat> catList = new ArrayList<Cat>();

		// 添加宠物猫
		catList.add(huahua);
		catList.add(fanfan);
		catList.add(maomao);
		// 输出排序前对象
		System.out.println("按名字排序前:");

		for (Cat cat : catList) {
			System.out.println(cat);
		}

		// 输出排序后对象
		System.out.println("按名字排序后:");
		Collections.sort(catList, new NameComparator());
		for (Cat cat : catList) {
			System.out.println(cat);
		}

	}

}

输出结果:

按名字排序前:
[名字:huahua, 年龄:5, 品种:英国短毛猫]
[名字:fanfan, 年龄:2, 品种:中华田园猫]
[名字:maomao, 年龄:3, 品种:中华田园猫]
按名字排序后:
[名字:fanfan, 年龄:2, 品种:中华田园猫]
[名字:huahua, 年龄:5, 品种:英国短毛猫]
[名字:maomao, 年龄:3, 品种:中华田园猫]

对宠物猫按照年龄进行排序:

AgeComparator类:

package com.imooc.sort;

import java.util.Comparator;

public class AgeComparator implements Comparator<Cat>{
	@Override
	public int compare(Cat o1, Cat o2) {
		// 按年龄降序排序
		int age1 = o1.getMonth();
		int age2 = o2.getMonth();
	
		return age2-age1;
	}

}

测试代码:

package com.imooc.sort;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CatTest {

	public static void main(String[] args) {
		// 按宠物猫名字升序排序
		Cat huahua = new Cat("huahua", 5, "英国短毛猫");
		Cat fanfan = new Cat("fanfan", 2, "中华田园猫");
		Cat maomao = new Cat("maomao", 3, "中华田园猫");

		List<Cat> catList = new ArrayList<Cat>();

		// 添加宠物猫
		catList.add(huahua);
		catList.add(fanfan);
		catList.add(maomao);
		// 输出排序前对象
		System.out.println("按年龄排序前:");
		for (Cat cat : catList) {
			System.out.println(cat);
		}

		// 按年龄进行降序排序
		System.out.println("按年龄降序排序后:");
		Collections.sort(catList, new AgeComparator());
		for (Cat cat : catList) {
			System.out.println(cat);
		}

	}

}

输出结果:

按年龄排序前:
[名字:huahua, 年龄:5, 品种:英国短毛猫]
[名字:fanfan, 年龄:2, 品种:中华田园猫]
[名字:maomao, 年龄:3, 品种:中华田园猫]
按年龄降序排序后:
[名字:huahua, 年龄:5, 品种:英国短毛猫]
[名字:maomao, 年龄:3, 品种:中华田园猫]
[名字:fanfan, 年龄:2, 品种:中华田园猫]

编程练习:

要求:

定义一个学生信息类,包括学号,姓名,年龄三个成员变量,然后按名字进行升序排序。(使用Comparator接口)

Student类:
 

package test013;

public class Student {
	// 成员变量
	private int id;
	private String name;
	private int age;

	// 构造方法
	public Student() {
		super();
	}

	public Student(int id, String name, int age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}

	// getter和setter方法
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	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;
	}

	@Override
	public String toString() {
		return "[学号:" + id + ", 年龄:" + age + ", 姓名:" + name + "]";
	}

	// toString()方法

}

StudentTest类:

package test013;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

//实现Comparator接口
public class StudentTest implements Comparator<Student> {

	// 实现接口中的方法
	@Override
	public int compare(Student o1, Student o2) {
		String name1 = o1.getName();
		String name2 = o2.getName();
		return name1.compareTo(name2);
	}

	public static void main(String[] args) {
		// 定义Student类的对象
		Student stu1 = new Student(40, "peter", 20);
		Student stu2 = new Student(28, "angel", 5);
		Student stu3 = new Student(35, "tom", 18);

		// 将对象添加到List中
		List<Student> stuList = new ArrayList<Student>();
		stuList.add(stu1);
		stuList.add(stu2);
		stuList.add(stu3);

		// 输出排序前的数据
		System.out.println("按名字排序前:");
		for(Student stu:stuList) {
			System.out.println(stu);
		}

		// 排序
		Collections.sort(stuList,new StudentTest());

		// 输出排序后的数据
		System.out.println("按名字排序后:");
		for(Student stu:stuList) {
			System.out.println(stu);
		}

	}
}

输出结果:

按名字排序前:
[学号:40, 年龄:20, 姓名:peter]
[学号:28, 年龄:5, 姓名:angel]
[学号:35, 年龄:18, 姓名:tom]
按名字排序后:
[学号:28, 年龄:5, 姓名:angel]
[学号:40, 年龄:20, 姓名:peter]
[学号:35, 年龄:18, 姓名:tom]

Comparable接口:

案例:对商品价格进行升序排列

Goods类:

package com.imooc.sort;

public class Goods implements Comparable<Goods> {

	private String id;
	private String name;
	private double price;

	// setter getter
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	// 无参构造
	public Goods() {
	}

	// 多参构造
	public Goods(String id, String name, double price) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
	}

	@Override
	public String toString() {
		return "商品编号:" + id + ", 商品名字:" + name + ", 商品价格:" + price;
	}

	@Override
	public int compareTo(Goods o) {
		double price1 = this.getPrice();
		double price2 = o.getPrice();
		return new Double(price1 - price2).intValue();
	}

}

GoodsTest类:

package com.imooc.sort;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class GoodsTest {

	public static void main(String[] args) {
		Goods g1 = new Goods("s0001", "手机", 2000);
		Goods g2 = new Goods("s0002", "冰箱", 5000);
		Goods g3 = new Goods("s0003", "电视机", 3000);

		List<Goods> goodsList = new ArrayList<Goods>();

		goodsList.add(g1);
		goodsList.add(g2);
		goodsList.add(g3);

		// 输出排序前的数据
		System.out.println("排序前:");
		for (Goods goods : goodsList) {
			System.out.println(goods);
		}

		// 输出排序后的数据
		Collections.sort(goodsList);
		System.out.println("排序后:");
		for (Goods goods : goodsList) {
			System.out.println(goods);
		}

	}

}

输出结果:

排序前:
商品编号:s0001, 商品名字:手机, 商品价格:2000.0
商品编号:s0002, 商品名字:冰箱, 商品价格:5000.0
商品编号:s0003, 商品名字:电视机, 商品价格:3000.0
排序后:
商品编号:s0001, 商品名字:手机, 商品价格:2000.0
商品编号:s0003, 商品名字:电视机, 商品价格:3000.0
商品编号:s0002, 商品名字:冰箱, 商品价格:5000.0

总结:

 

 

 

 

 

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值