當前位置:首頁 » 編程語言 » 排序listjava

排序listjava

發布時間: 2023-09-16 08:18:21

A. java 中 List 怎麼排序呢

Map<String, String> map =new HashMap<String, String>();
java.util.List<String> list=new ArrayList<String>();
map.put("a", "a");
map.put("c", "c");
map.put("b", "b");
list.addAll(map.keySet());
Collections.sort(list);
for (String key : list)
{
System.out.println(map.get(key));
}

直接Copy 到main方法中運行, 是個降序! 升序的話 Collections 這裡面也有的。

樓主 太摳門, 5分都不給!

B. java怎麼對list進行排序

1,使用Comparator 介面

Student類 結構如下:(省略getter,setter方法)

public class Student {
/***
* 姓名
*/
private String name;
private int age;
private String address;
/***
* 考試得分
*/
private int score;

//省略getter,setter方法
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", score=" + score
+ "]";
}

}

測試方法:

@Test
public void test_ListComparator(){
List<Student>students=new ArrayList<Student>();
Student stu=null;
stu=new Student();
stu.setName("whuang");
stu.setAge(12);
stu.setScore(80);
students.add(stu);

stu=new Student();
stu.setName("rong");
stu.setAge(11);
stu.setScore(90);
students.add(stu);

stu=new Student();
stu.setName("zhu");
stu.setAge(15);
stu.setScore(100);
students.add(stu);

Collections.sort(students,new SystemHWUtil. ListComparator(true,"age"));
System.out.println(students);

}

運行結果:

[Student [name=rong, age=11, score=90], Student [name=whuang, age=12, score=80], Student [name=zhu, age=15, score=100]]

核心類:

public static class ListComparator implements Comparator{
/***
* 是否轉化為Int之後再比較
*/
private boolean isConvertInteger;
/***
* 對哪個列進行排序
*/
private String comparedProperty;
public ListComparator(boolean isConvertInteger,String comparedProperty) {
super();
this.isConvertInteger = isConvertInteger;
this.comparedProperty=comparedProperty;
}
public int compare(Object o1, Object o2) {
if(null!=o1&&null!=o2)
{
try {
Object obj1=ReflectHWUtils.getObjectValue(o1, comparedProperty);
Object obj2=ReflectHWUtils.getObjectValue(o2, comparedProperty);
if(isConvertInteger){
int num1;
int num2;
if(obj1 instanceof Integer){
num1=(Integer)obj1;
num2=(Integer)obj2;
}else{
num1=Integer.parseInt(obj1.toString());
num2=Integer.parseInt(obj2.toString());
}
if(num1>num2){
return 1;
}else if(num1<num2){
return -1;
}else{
return 0;
}
}else{
return obj1.toString().compareTo(obj2.toString());
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return 0/*等於*/;
}
}

2,可以指定是升序還是降序

實例:

@Test
public void test_ListComparator(){
List<Student>students=new ArrayList<Student>();
Student stu=null;
stu=new Student();
stu.setName("whuang");
stu.setAge(12);
stu.setScore(80);
students.add(stu);

stu=new Student();
stu.setName("rong");
stu.setAge(11);
stu.setScore(90);
students.add(stu);

stu=new Student();
stu.setName("zhu");
stu.setAge(15);
stu.setScore(100);
students.add(stu);
SortList<Student> sortList = new SortList<Student>();
sortList.Sort(students, "getAge", "asc");
System.out.println(students);

}

注意:sortList.Sort 的第二個參數是方法名,不是成員變數名.

核心代碼

package com.common.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortList<E> {
public void Sort(List<E> list, final String method, final String sort) {
Collections.sort(list, new Comparator() {
public int compare(Object a, Object b) {
int ret = 0;
try {
Method m1 = ((E) a).getClass().getMethod(method, null);
Method m2 = ((E) b).getClass().getMethod(method, null);
if (sort != null && "desc".equals(sort))// 倒序
ret = m2.invoke(((E) b), null).toString()
.compareTo(m1.invoke(((E) a), null).toString());
else
// 正序
ret = m1.invoke(((E) a), null).toString()
.compareTo(m2.invoke(((E) b), null).toString());
} catch (NoSuchMethodException ne) {
System.out.println(ne);
} catch (IllegalAccessException ie) {
System.out.println(ie);
} catch (InvocationTargetException it) {
System.out.println(it);
}
return ret;
}
});
}
}

C. java 中 List<T>如何按照T中的一個欄位排序

可以通過以下工具類進行實現:
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* List按照指定欄位排序工具類
*
* @param <T>
*/
public class ListSortUtil<T> {
/**
* @param targetList 目標排序List
* @param sortField 排序欄位(實體類屬性名)
* @param sortMode 排序方式(asc or desc)
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public void sort(List<T> targetList, final String sortField, final String sortMode) {

Collections.sort(targetList, new Comparator() {
public int compare(Object obj1, Object obj2) {
int retVal = 0;
try {
//首字母轉大寫
String newStr=sortField.substring(0, 1).toUpperCase()+sortField.replaceFirst("\\w","");
String methodStr="get"+newStr;

Method method1 = ((T)obj1).getClass().getMethod(methodStr, null);
Method method2 = ((T)obj2).getClass().getMethod(methodStr, null);
if (sortMode != null && "desc".equals(sortMode)) {
retVal = method2.invoke(((T) obj2), null).toString().compareTo(method1.invoke(((T) obj1), null).toString()); // 倒序
} else {
retVal = method1.invoke(((T) obj1), null).toString().compareTo(method2.invoke(((T) obj2), null).toString()); // 正序
}
} catch (Exception e) {
throw new RuntimeException();
}
return retVal;
}
});
}

}

Collections.sort(list.);//升序

D. java中list排序

最好是在將日期插入list前排序,即在sql查詢是排序,這樣比較好處理。
另一種方法是構建一個比較類或比較方法,如一樓所述。

E. JAVA中list集合的排序

根據字元串的含義,進行對象化,比如,Student,有三個屬性,序號,姓名,分數
注意重寫Student的Compareable介面
然後,List<String>變成List<Student> students=new ArrayList<Student>
然後,遍歷list,算出平均分,放入新的SortList<Student>
列印結果

F. Java的List怎麼排序啊

問題在於

doubletemp=0.0;

你把temp設置成double型,然後

temp=arr.get(l).doubleValue();

所以你把取得的int類型轉換成了double類型,最後輸出就帶上了.0


你仔細看看你的程序,int類型有的排序後沒有變,有的變x.0了,凡是加上.0的都是它在排序過程中與double類型的數字進行過交換。

G. java 怎麼將List裡面數據排序

學生實體類,包含姓名和年齡屬性,

比較時先按姓名升序排序,如果姓名相同則按年齡升序排序。

H. java 怎麼將List裡面數據排序

importjava.util.ArrayList;
importjava.util.Collections;
importjava.util.Comparator;
importjava.util.List;


publicclassArrayListOrder{

publicstaticvoidmain(String[]args){
List<List<Integer>>list=newArrayList<List<Integer>>();
List<Integer>arrayList1=newArrayList<Integer>();
arrayList1.add(2);
arrayList1.add(5);
arrayList1.add(6);
list.add(arrayList1);
List<Integer>arrayList2=newArrayList<Integer>();
arrayList2.add(2);
arrayList2.add(4);
arrayList2.add(6);
list.add(arrayList2);
List<Integer>arrayList3=newArrayList<Integer>();
arrayList3.add(2);
arrayList3.add(6);
arrayList3.add(6);
list.add(arrayList3);
List<Integer>arrayList4=newArrayList<Integer>();
arrayList4.add(2);
arrayList4.add(1);
arrayList4.add(6);
list.add(arrayList4);
for(List<Integer>tmpList:list){
System.out.print(tmpList.get(1)+"");
}
System.out.println("");

//排序
Collections.sort(list,newComparator<List<Integer>>(){
publicintcompare(List<Integer>list1,List<Integer>list2){
//比較每個ArrayList的第二個元素
if(list1.get(1)==list2.get(1)){
return0;
}elseif(list1.get(1)>list2.get(1)){
return1;
}else{
return-1;
}
}
});
for(List<Integer>tmpList:list){
System.out.print(tmpList.get(1)+"");
}
}
}

I. java List 排名

importjava.util.ArrayList;
importjava.util.Comparator;
importjava.util.List;

publicclassStudentSortDemo{

publicstaticvoidmain(String[]args){
List<Student>studentList=newArrayList<Student>();
studentList.add(newStudent("小明",85));
studentList.add(newStudent("小花",85));
studentList.add(newStudent("小軍",100));
studentList.add(newStudent("小強",70));
studentList.add(newStudent("小紅",85));
Comparator<Student>comparator=newComparator<Student>(){
@Override
publicintcompare(Studento1,Studento2){
if(o1.getSorce()<o2.getSorce()){
return1;
}elseif(o1.getSorce()==o2.getSorce()){
return0;
}else{
return-1;
}
}
};
studentList.sort(comparator);
System.out.println(studentList);
for(inti=0,s=studentList.size();i<s;i++){
if(i>0&&studentList.get(i).getSorce()==studentList.get(i-1).getSorce()){
studentList.get(i).setRank(studentList.get(i-1).getRank());
}else{
studentList.get(i).setRank(i+1);
}
}
System.out.println(studentList);
}
}
classStudent{
privateStringname;
privateintsorce;
privateintrank;
publicStudent(Stringname,intsorce){
this.name=name;
this.sorce=sorce;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicintgetSorce(){
returnsorce;
}
publicvoidsetSorce(intsorce){
this.sorce=sorce;
}
publicintgetRank(){
returnrank;
}
publicvoidsetRank(intrank){
this.rank=rank;
}
@Override
publicStringtoString(){
returngetName()+"分數:"+getSorce()+"排名:"+getRank();
}
}

運行結果:

[小軍 分數:100 排名:0, 小明 分數:85 排名:0, 小花 分數:85 排名:0, 小紅 分數:85 排名:0, 小強 分數:70 排名:0]

[小軍 分數:100 排名:1, 小明 分數:85 排名:2, 小花 分數:85 排名:2, 小紅 分數:85 排名:2, 小強 分數:70 排名:5]

熱點內容
滑板鞋腳本視頻 發布:2025-02-02 09:48:54 瀏覽:432
群暉怎麼玩安卓模擬器 發布:2025-02-02 09:45:23 瀏覽:557
三星安卓12彩蛋怎麼玩 發布:2025-02-02 09:44:39 瀏覽:743
電腦顯示連接伺服器錯誤 發布:2025-02-02 09:24:10 瀏覽:536
瑞芯微開發板編譯 發布:2025-02-02 09:22:54 瀏覽:146
linux虛擬機用gcc編譯時顯示錯誤 發布:2025-02-02 09:14:01 瀏覽:232
java駝峰 發布:2025-02-02 09:13:26 瀏覽:651
魔獸腳本怎麼用 發布:2025-02-02 09:10:28 瀏覽:532
linuxadobe 發布:2025-02-02 09:09:43 瀏覽:212
sql2000資料庫連接 發布:2025-02-02 09:09:43 瀏覽:726