本文目錄一覽:
Java中如何對集合排序
Java內建的排序(冒泡):Java集合有兩個實現的工具類,Collections和Arrays。Collections針對集合類型。Arrays針對數組。只需要一個為你排序需要的定製的Comparator或Comparable的實現,將其作為參數傳給Collections或Arrays的sort方法就行。
可以自己用程序去實現這個排序。用for從原List裡面一個一個拿出來比較然後一個新建的List裡面去。
樓主看一下參考資料。
java怎樣對集合按照實體類的字段排序
import java.util.Comparator;
import java.util.TreeSet;
/*
* 需求:請按照姓名的長度排序
*
* TreeSet集合保證元素排序和唯一性的原理
* 唯一性:是根據比較的返回是否是0來決定。
* 排序:
* A:自然排序(元素具備比較性)
* 讓元素所屬的類實現自然排序接口 Comparable
* B:比較器排序(集合具備比較性)
* 讓集合的構造方法接收一個比較器接口的子類對象 Comparator
*/
public class TreeSetDemo {
public static void main(String[] args) {
// 創建集合對象
// TreeSetStudent ts = new TreeSetStudent(); //自然排序
// public TreeSet(Comparator comparator) //比較器排序
// TreeSetStudent ts = new TreeSetStudent(new MyComparator());
// 如果一個方法的參數是接口,那麼真正要的是接口的實現類的對象
// 而匿名內部類就可以實現這個東西
TreeSetStudent ts = new TreeSetStudent(new ComparatorStudent() {
@Override
public int compare(Student s1, Student s2) {
// 姓名長度
int num = s1.getName().length() – s2.getName().length();
// 姓名內容
int num2 = num == 0 ? s1.getName().compareTo(s2.getName())
: num;
// 年齡
int num3 = num2 == 0 ? s1.getAge() – s2.getAge() : num2;
return num3;
}
});
// 創建元素
Student s1 = new Student(“linqingxia”, 27);
Student s2 = new Student(“zhangguorong”, 29);
Student s3 = new Student(“wanglihong”, 23);
Student s4 = new Student(“linqingxia”, 27);
Student s5 = new Student(“liushishi”, 22);
Student s6 = new Student(“wuqilong”, 40);
Student s7 = new Student(“fengqingy”, 22);
Student s8 = new Student(“linqingxia”, 29);
// 添加元素
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
ts.add(s6);
ts.add(s7);
ts.add(s8);
// 遍歷
for (Student s : ts) {
System.out.println(s.getName() + “—” + s.getAge());
}
}
}
排序的集合有哪些? java
Collection
List
Set
HashSet
TreeSet 是(用二叉樹排序)
Map使用key-value來映射和存儲數據,Key必須惟一,
其中List和Set繼承自Collection接口。
Set不允許元素重複。HashSet和TreeSet是兩個主要的實現類。
List有序且允許元素重複。ArrayList、LinkedList和Vector是三個主要的實現類。
Map也屬於集合系統,但和Collection接口不同。Map是key對value的映射集合,其中key列就是一個集合。key不能重複,但是value可以重複。HashMap、TreeMap和Hashtable是三個主要的實現類。
SortedSet和SortedMap接口對元素按指定規則排序,SortedMap是對key列進行排序。
java中如何對數組和集合進行排序
java中對集合排序,可以使用Collections.sort來進行排序,可以對中文、字母、數字進行排序,當比較的是對象時候,讓該類實現comparable接口,示例如下:
Collections.sort(dataMap, new ComparatorMapString, Object() { //排序接口實現方法 @Override public int compare(MapString, Object lhs, MapString, Object rhs) { switch (whichsort) { case System_OpenPosition_Sort_Currency: String d2 = ((String) rhs.get(Instrument)); String d1 = (String) lhs.get(Instrument); if (d2 != null d1 != null) { int flag = d1.compareTo(d2); if (flag == 0) { Double d3 = ((Double) rhs.get(OpenPrice)); Double d4 = (Double) lhs.get(OpenPrice); if (d3 != null d4 != null) { int flag2 = d4.compareTo(d3); if (flag2 == 0) { String d5 = ((String) rhs.get(BuySell)); String d6 = (String) lhs.get(BuySell);//文字排序 if (d5 != null d6 != null) { return d6.compareTo(d5);//返回一個int類型,用來判斷是否大於、小於還是等於 } } return d4.compareTo(d3); } } else { return flag; } // return d1.compareTo(d2); }
JAVA中list集合的排序
根據字符串的含義,進行對象化,比如,Student,有三個屬性,序號,姓名,分數
注意重寫Student的Compareable接口
然後,ListString變成ListStudent students=new ArrayListStudent
然後,遍歷list,算出平均分,放入新的SortListStudent
打印結果
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/285633.html