當前位置:首頁 » 編程語言 » java集合排序

java集合排序

發布時間: 2023-08-20 22:52:44

java中,如何實現集合的升序和降序排列

sql語句的時候定義升降序排列,就行了。如果單純用Java代碼實現的話,可以用冒泡排序,代碼比較簡單:
publicclassBubbleSort
{
publicvoidsort(int[]a)
{
inttemp=0;
for(inti=a.length-1;i>0;--i)
{
for(intj=0;j<i;++j)
{
if(a[j+1]<a[j])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
}

⑵ Java的List怎麼排序啊

問題在於

doubletemp=0.0;

你把temp設置成double型,然後

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

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


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

⑶ java中如何對數組和集合進行排序

java中對集合排序,可以使用Collections.sort來進行排序,可以對中文、字母、數字進行排序,當比較的是對象時候,讓該類實現comparable介面,示例如下:
Collections.sort(dataMap, new Comparator<Map<String, Object>>() { //排序介面實現方法 @Override public int compare(Map<String, Object> lhs, Map<String, 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集合如何排序

放到一個list中排zd序就專可以了
List<Article>
list
=
new
ArrayList<Article>();
list.addAll(list1);
list.addAll(list2);
然後2層循屬環就可以了

⑸ JAVA中list集合的排序

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

⑹ java 集合中怎麼將元素倒序排列

方法一:實現Comparable介面排序package collsort.comparable;
package com.cvicse.sort.comparable;

public class Cat implements Comparable<Cat> {
private int age;
private String name;

public Cat(int age, String name) {
this.age = age;
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
......
public int compareTo(Cat o) {
return this.getAge() - o.getAge();
}
......
}
通過實現Comparable介面實現個性化排序測試。排序測試,Collection.sort(list)升序排列Collections.sort(list, Collections.reverseOrder());降序排列;Collections.reverse(list);反轉排序,先輸出列表最後一個元素
public class TestComparable {
public static void main(String args[]) {
test();
test2();
}
public static void test() {
......
List<Cat> listCat1 = new ArrayList<Cat>();
Cat cat1 = new Cat(34, "hehe");
Cat cat2 = new Cat(12, "haha");
Cat cat3 = new Cat(23, "leimin");
Cat cat4 = new Cat(13, "lavasoft");
listCat1.add(cat1);
listCat1.add(cat2);
listCat1.add(cat3);
......
System.out.println("調用Collections.sort(List<T> list)listCat2升序排序:");
Collections.sort(listCat1);
System.out.println("降序排列元素:");
Collections.sort(listCat1, Collections.reverseOrder());
System.out.println("Collections.reverse 從列表中最後一個元素開始輸出:");
Collections.reverse(listCat1);
......
}
/**
* 針對數組的排序
*/
public static void test2() {
String[] strArray = new String[] { "z", "a", "C" };
System.out.println("數組轉換為列表");
List<String> list = Arrays.asList(strArray);
System.out.println("順序排序列表");
Collections.sort(list);
System.out
.println("按String實現的Comparator對象String.CASE_INSENSITIVE_ORDER排序----");
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
System.out.println("倒序排序列表");
Collections.sort(list, Collections.reverseOrder());
......
}
}
方法二:實現Comparator介面排序
public class Person {
private int age;
private String name;
......
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
......
}
實現了Comparator介面,重寫了compare方法
import java.util.Comparator;
public class PersonComparator implements Comparator<Person> {
public int compare(Person o1, Person o2) {
return o1.getAge() - o2.getAge();
}
}
測試方法
public class TestComparator {
public static void main(String args[]) {
test1();
}
public static void test1() {
System.out.println("升序排序測試:");
List<Person> listPerson = new ArrayList<Person>();
Person person1 = new Person(34, "lavasoft");
Person person2 = new Person(12, "lavasoft");
Person person3 = new Person(23, "leimin");
Person person4 = new Person(13, "sdg");
listPerson.add(person1);
listPerson.add(person2);
listPerson.add(person3);
Comparator<Person> ascComparator = new PersonComparator();
System.out.println("排序後集合為:");
// 利用Collections類靜態工具方法對集合List進行排序
Collections.sort(listPerson, ascComparator);
System.out.println("\n降序排序測試:");
// 從升序排序對象產生一個反轉(降序)的排序對象
Comparator<Person> descComparator = Collections
.reverseOrder(ascComparator);
System.out.println("利用反轉後的排序介面對象對集合List排序並輸出:");
Collections.sort(listPerson, descComparator);
outCollection(listPerson);
}
}

⑺ Java中 map集合能排序嗎

Java中的Map默認是對元素不排序的,但是Map的實現類TreeMap能夠把它保存的記錄根據key排序,默認是按升序排序。如果我們想要改變排序方式,則需要使用比較器:Comparator。

熱點內容
安卓手機中的投影在哪裡 發布:2025-02-05 08:01:57 瀏覽:594
php調用定義函數 發布:2025-02-05 08:00:30 瀏覽:452
ubuntujava環境變數 發布:2025-02-05 07:57:13 瀏覽:443
sql語句on 發布:2025-02-05 07:41:42 瀏覽:598
取消電腦密碼怎麼設置8 發布:2025-02-05 07:24:16 瀏覽:393
洗腦編程 發布:2025-02-05 07:23:52 瀏覽:949
osd加密 發布:2025-02-05 07:17:39 瀏覽:36
微信游戲源碼下載 發布:2025-02-05 07:17:29 瀏覽:385
計算機內存儲器是 發布:2025-02-05 07:13:35 瀏覽:145
classpathlinux 發布:2025-02-05 07:12:57 瀏覽:565