首页 > 开发 > Java > 正文

java对ArrayList排序代码示例

2024-07-13 09:56:03
字体:
来源:转载
供稿:网友

本文通过代码示例给大家介绍java对arraylist排序,代码简洁易懂,感兴趣的朋友一起学习吧

不废话了,直接给大家贴代码了。

 

 
  1. class term {  
  2. String str;  
  3. int id;  
  4. public term(String str, int id) {  
  5. this.str = str;  
  6. this.id = id;  
  7. }  
  8. public String toString() {  
  9. return str+" "+id;  
  10. }  
  11. }  
  12. class sterm implements Comparable{  
  13. String str;  
  14. int id;  
  15. public sterm(String str, int id) {  
  16. this.str = str;  
  17. this.id = id;  
  18. }  
  19. public int compareTo(Object o) {  
  20. return ((sterm)o).id - id;  
  21. }  
  22. public String toString() {  
  23. return str+" "+id;  
  24. }  
  25. }  
  26. //method1: explicit implements Comparator  
  27. class termComparator implements Comparator {  
  28. public int compare (Object o1, Object o2) {  
  29. return ((term)o1).id - ((term)o2).id;  
  30. }  
  31. }  
  32. public class t1 {  
  33. /**  
  34. * @param args  
  35. */ 
  36. public static void main(String[] args) {  
  37. // TODO Auto-generated method stub  
  38. // ArrayList<Integer> arr = new ArrayList<Integer>( Arrays.asList(3,1,3,7,8,0));  
  39. //  
  40. // Collections.sort(arr, new Comparator(){  
  41. //  
  42. // public int compare(Object o1, Object o2){  
  43. // return new Double((Integer)o1).compareTo(new Double ((Integer)o2));  
  44. // }  
  45. // });  
  46. //method1  
  47. List<term> ls = new ArrayList<term>();  
  48. ls.add(new term("a",1));  
  49. ls.add(new term("b",5));  
  50. ls.add(new term("c",2));  
  51. ls.add(new term("d",2));  
  52. ls.add(new term("e",3));  
  53. ls.add(new term("f",0));  
  54. Collections.sort(ls, new termComparator());  
  55. System.out.println(ls);//[f 0, a 1, c 2, d 2, e 3, b 5]  
  56. //method2: anonymous implements  
  57. Collections.sort(ls, new Comparator(){  
  58. public int compare(Object o1, Object o2){  
  59. return ((term)o2).id - ((term)o1).id;  
  60. }  
  61. });  
  62. System.out.println(ls);//[b 5, e 3, c 2, d 2, a 1, f 0]  
  63. //method3:instantiate a Comparator template  
  64. Comparator<term> termCmp = new Comparator<term>() {  
  65. public int compare(term t1, term t2) {  
  66. return t1.id - t2.id;  
  67. }  
  68. };  
  69. Collections.sort(ls, termCmp);  
  70. System.out.println(ls);//[f 0, a 1, c 2, d 2, e 3, b 5]  
  71. //method4:element implements Comparable  
  72. List<sterm> lss = new ArrayList<sterm>();  
  73. lss.add(new sterm("a",1));  
  74. lss.add(new sterm("b",5));  
  75. lss.add(new sterm("c",2));  
  76. lss.add(new sterm("d",2));  
  77. lss.add(new sterm("e",3));  
  78. lss.add(new sterm("f",0));  
  79. Collections.sort(lss);  
  80. System.out.println(lss);//[b 5, e 3, c 2, d 2, a 1, f 0]  
  81. }  

PrioriyQueue的用法和上述的排序类似,有三种方法:

 

 
  1. class WordFreq implements Comparable{  
  2. public String wd;  
  3. public int freq;  
  4. public WordFreq(String wd, int freq) {  
  5. this.wd = wd;  
  6. this.freq = freq;  
  7. }  
  8. public int compareTo(Object o) {  
  9. return ((WordFreq)o).freq - freq;  
  10. }  
  11. public String toString() {  
  12. return wd+" "+freq;  
  13. }  
  14. }  
  15. public class testt {  
  16. public static void main(String[] args) {  
  17. // TODO Auto-generated method stub  
  18. PriorityQueue<WordFreq> pq = new PriorityQueue<WordFreq>();  
  19. pq.offer(new WordFreq("aaa", 3));  
  20. pq.offer(new WordFreq("bbb", 4));  
  21. pq.offer(new WordFreq("ccc",1));  
  22. while(pq.peek() != null) {  
  23. System.out.println(pq.poll());  
  24. }//从大到小输出  
  25. }  

注意,

 

  
  1. for (WordFreq wf : pq) { 
  2. System.out.println(wf); 

并不保证遍历的有序

如果List ls 进行排序的话,不需要写Comparator, 因为String本身有compareTo的实现。


注:相关教程知识阅读请移步到JAVA教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表