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 list = new arraylist();
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 list = new arraylist();
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传递给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 {
@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 catlist=new arraylist();
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 {
@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);
}
- 自然排序
- compareto(o)自然比较
- 例题:对商品价格进行降序排列
- goods类
package day2;
public class goods implements comparable{
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 goodslist=new arraylist();
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}
int[] arrs
array.sort(arrs);