Java集合排序
Java集合排序
- Java集合排序
-
- @[TOC](Java集合排序)
- 主要内容
- 一、集合中的基本数据类型排序
- 二、集合中的字符串排序
-
- 例题
- 三、Comparator
- 四、Comparable
- 五、Comparator与Comparable区别
- 六、总结
- 补充:数组的排序
主要内容
一、集合中的基本数据类型排序
- 使用Collections类的sort()方法
- sort(List list)根据元素的自然顺序对指数列表按升序进行排序
二、集合中的字符串排序
例题
- 对整形进行排序
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class day28 {
public static void main(String[] args) {
// 泛型使用类的形式
List<Integer> list = new ArrayList<Integer>();
list.add(8);
list.add(5);
list.add(9);
list.add(1);
for (int i:list){
System.out.print(i+" ");
}
System.out.println();
System.out.println("***********************");
// list排序
Collections.sort(list);
for (int i:list){
System.out.print(i+" ");
}
System.out.println();
System.out.println("***********************");
}
}
- 对字符串进行排序
// 对存在list字符串进行排序
List<String> list = new ArrayList<String>();
list.add("orange");
list.add("red");
list.add("blue");
list.add("yellow");
System.out.println("Before sorting:");
for (String s : list) {
System.out.print(s + " ");
}
// 按照字母顺序排序
Collections.sort(list);
System.out.println("After sorting:");
for (String s:list){
System.out.print(s+" ");
}
三、Comparator
- 强行对某个对象进行整体排序的比较函数
- 可以将Comparator传递给sort方法
- int compare(n1,n2)比较用来排序的两个参数
- equals(obj)该方法不需重写
- 对宠物猫进行排序
- Cat类
package day2;
public class Cat {
private String name;
private int month;
private String species;
//构造方法
public Cat(String name, int month, String species) {
this.name = name;
this.month = month;
this.species = species;
}
//getter与setter
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;
}
@Override
public String toString() {
return "Cat{" +
"name='" + name + '\'' +
", month=" + month +
", species='" + species + '\'' +
'}';
}
}
- NameComparator类
package day2;
import java.util.Comparator;
public class NameCompatator implements Comparator<Cat> {
@Override
public int compare(Cat o1, Cat o2) {
/**
* 按照名字升序
*/
String name1=o1.getName();
String name2=o2.getName();
// 对字符串进行比较,~ 倒序
return name1.compareTo(name2);
}
}
- Main 按照名字升序
package day2;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class day28 {
public static void main(String[] args) {
/**
* 例题:对宠物猫分别按照名字升序,年龄降序排列
*/
Cat huahua = new Cat("huahua",6,"波斯");
Cat fanfan = new Cat("fanfan",2,"波斯");
Cat maomao = new Cat("maomao",4,"波斯");
List<Cat> catList=new ArrayList<Cat>();
catList.add(huahua);
catList.add(fanfan);
catList.add(maomao);
System.out.println("Before sorting:");
for (Cat cat:catList){
System.out.println(cat);
}
// 名字升序
Collections.sort(catList,new NameCompatator());
System.out.println("After sorting:");
for (Cat cat:catList){
System.out.println(cat);
}
}
}
- AgeComparator类
package day2;
import java.util.Comparator;
public class AgeComparator implements Comparator<Cat> {
@Override
public int compare(Cat o1, Cat o2) {
// 按照年龄降序
int age=o1.getMonth();
int age2=o2.getMonth();
return age2-age;
}
}
- Main 年龄降序
// 按照年龄进行降序
Collections.sort(catList,new AgeComparator());
System.out.println("Age:After sorting:");
for (Cat cat:catList){
System.out.println(cat);
}
四、Comparable
- 自然排序
- compareTO(o)自然比较
- 例题:对商品价格进行降序排列
- Goods类
package day2;
public class Goods implements Comparable<Goods>{
private String id;
private String name;
private double price;
public Goods(String id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
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(float price) {
this.price = price;
}
@Override
public String toString() {
return "Goods{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
'}';
}
/**
* 商品价格降序
* @param o
* @return new Double(price2-price1).intValue()
*/
@Override
public int compareTo(Goods o) {
// 取出商品价格
double price1=this.getPrice();
double price2=o.getPrice();
return new Double(price2-price1).intValue();
}
}
- Main
Goods g1=new Goods("s01","手机",2000);
Goods g2=new Goods("s02","冰箱",5000);
Goods g3=new Goods("s03","电视剧",3000);
List<Goods> goodsList=new ArrayList<Goods>();
goodsList.add(g1);
goodsList.add(g2);
goodsList.add(g3);
System.out.println("Before sorting:");
for (Goods goods:goodsList){
System.out.println(goods);
}
Collections.sort(goodsList);
System.out.println("After sorting:");
for (Goods goods:goodsList){
System.out.println(goods);
}
- result
Before sorting:
Goods{
id=s01, name='手机', price=2000.0}
Goods{
id=s02, name='冰箱', price=5000.0}
Goods{
id=s03, name='电视剧', price=3000.0}
After sorting:
Goods{
id=s02, name='冰箱', price=5000.0}
Goods{
id=s03, name='电视剧', price=3000.0}
Goods{
id=s01, name='手机', price=2000.0}
五、Comparator与Comparable区别
六、总结
补充:数组的排序
int[] arrs
Array.sort(arrs);