`
knight_black_bob
  • 浏览: 830778 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

list set array map 排序问题

    博客分类:
  • java
 
阅读更多

 

 

While analyzing source code of a large number of open source Java projects, I found Java developers frequently sort in two ways. One is using the sort() method of Collections or Arrays, and the other is using sorted data structures, such as TreeMap and TreeSet.

 

1. Using sort() Method

If it is a collection, use Collections.sort() method.

// Collections.sort
List<ObjectName> list = new ArrayList<ObjectName>();
Collections.sort(list, new Comparator<ObjectName>() {
	public int compare(ObjectName o1, ObjectName o2) {
		return o1.toString().compareTo(o2.toString());
	}
});

If it is an array, use Arrays.sort() method.

// Arrays.sort
ObjectName[] arr = new ObjectName[10];
Arrays.sort(arr, new Comparator<ObjectName>() {
	public int compare(ObjectName o1, ObjectName o2) {
		return o1.toString().compareTo(o2.toString());
	}
});

This is very convenient if a collection or an array is already set up.

2. Using Sorted Data Structures

If it is a list or set, use TreeSet to sort.

// TreeSet
Set<ObjectName> sortedSet = new TreeSet<ObjectName>(new Comparator<ObjectName>() {
	public int compare(ObjectName o1, ObjectName o2) {
		return o1.toString().compareTo(o2.toString());
	}
});
sortedSet.addAll(unsortedSet);

If it is a map, use TreeMap to sort. TreeMap is sorted by key.

// TreeMap - using String.CASE_INSENSITIVE_ORDER which is a Comparator that orders Strings by compareToIgnoreCase
Map<String, Integer> sortedMap = new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER);
sortedMap.putAll(unsortedMap);
//TreeMap - In general, defined comparator
Map<ObjectName, String> sortedMap = new TreeMap<ObjectName, String>(new Comparator<ObjectName>() {
	public int compare(ObjectName o1, ObjectName o2) {
		return o1.toString().compareTo(o2.toString());
	}
});
sortedMap.putAll(unsortedMap);

This approach is very useful, if you would do a lot of search operations for the collection. The sorted data structure will give time complexity of O(logn), which is lower than O(n).

3. Bad Practices

There are still bad practices, such as using self-defined sorting algorithm. Take the code below for example, not only the algorithm is not efficient, but also it is not readable. This happens a lot in different forms of variations.

double t;
for (int i = 0; i < 2; i++)
	for (int j = i + 1; j < 3; j++)
		if (r[j] < r[i]) {
			t = r[i];
			r[i] = r[j];
			r[j] = t;
		}
分享到:
评论

相关推荐

    Hibernate常见集合映射(Set,List_Array,Map,Bag)

    常见的集合映射类型有 Set、List、Array、Map 和 Bag 等,每种类型都有其特点和应用场景。 Set 集合映射 Set 集合是 Hibernate 中基础的集合类型,元素数据一般使用外键同主表关联。Set 集合非常适用于集合元素不...

    测试报告与总结\list,set,map,数组间的相互转换

    测试报告与总结\list,set,map,数组间的相互转换.rar测试报告与总结\list,set,map,数组间的相互转换.rar测试报告与总结\list,set,map,数组间的相互转换.rar测试报告与总结\list,set,map,数组间的相互转换.rar

    Java集合类List-Set-Map的区别和联系.doc

    Java 集合类 List-Set-Map 的区别和联系 Java 集合类 List、Set 和 Map 是 Java 语言中最基本的集合类,它们之间存在着紧密的联系和区别。在本文中,我们将对 Java 集合类 List、Set 和 Map 的区别和联系进行详细的...

    java中List、Array、Map、Set等集合相互转换

    主要介绍了java中List、Array、Map、Set等集合相互转换的相关资料,需要的朋友可以参考下

    ES6学习笔记之map、set与数组、对象的对比

    到目前为止,常用的数据结构有四种Array、Object、Set、Map。下面话不多说了,来一起看看详细的介绍吧。 // 数据结构横向对比,增,查,改,删 1、map和数组对比 { let map=new Map(); let array=[]; /**增**/...

    java面试宝典

    65、Set里的元素是不能重复的,那么用什么方法来区分重复与否呢? 是用==还是equals()? 它们有何区别 17 66、HashMap和Hashtable的区别 17 67、说出ArrayList,Vector, LinkedList的存储性能和特性 17 68、java中有几...

    java collections framework.ppt

    Linked List Array List Hash Set & Tree Set Hash Map & Tree Map Special set & map

    java集合框架笔记

    List set ArraryList Map java集合框架笔记 基于Array的List,其实就是封装了Array所不具备的一些功能方便我们使用

    【2022最新版】Java基础面试题总结(70道题含答案解析)

    Java 集合可以分为 List、Set、Map 三种类型。List 是一个有序的集合,可以包含重复的元素,提供了按索引访问的方式。ArrayList 和 LinkedList 是 List 的两个重要实现类。ArrayList 可以看作是能够自动增长容量的...

    Scala 计算算子

    Scala集合提供了丰富的计算算子,用于实现集合/数组的计算,这些计算子一般针对于List、Array、Set、Map、Range、Vector、Iterator等都可以适用。 排序 sorted def sorted[B &gt;: String](implicit ord: scala.math....

    Dbutils项目实例

    1 对于数据表的读操作 他可以把结果转换成List Array Set等java集合 便于程序员操作; 2 对于数据表的写操作 也变得很简单(只需写sql语句) 3 可以使用数据源 使用JNDI 数据库连接池等技术来优化性能 重用已经构建...

    Objective-c对象组装XML

    [map setObject:@"a" forKey:@"author"]; [map setObject:@"b" forKey:@"title"]; [map setObject:@"c" forKey:@"content"]; 或者 NSMutableArray *list = [[NSMutableArray alloc]init]; NSMutableDictionary...

    Java 最常见的 208 道面试题:第二模块答案

    20. List、 Set、 Map 之间的区别是什么? 21. HashMap 和 Hashtable 有什么区别? 22. 如何决定使用 HashMap 还是 TreeMap? 23. 说一下 HashMap 的实现原理? 24. 说一下 HashSet 的实现原理? 25. ArrayList 和 ...

    Scala的集合算子

    Scala集合提供了丰富的计算算⼦,⽤于实现集合/数组的计算,这些计算⼦⼀般针对于List、Array、Set、Map、Range、Vector、Iterator等都可以适⽤。 排序 ① sorted scala&gt; var a = Array(1,3,4,2,5) a: Array[Int] = ...

    c++头文件大全.txt

    &lt;array&gt;:数组 &lt;bitset&gt;:位集 &lt;chrono&gt;:时间和日期 &lt;complex&gt;:复数 &lt;deque&gt;:双端队列 &lt;forward_list&gt;:前向列表 &lt;functional&gt;:函数对象 &lt;iterator&gt;:迭代器 &lt;list&gt;:链表 &lt;map&gt;:映射 &lt;memory&gt;:内存管理 ...

    数据结构Advanced-Data-Structures

    List 29 Stack 32 Queue 61 Deque 63 Priority queue 66 Map 70 Bidirectional map 73 Multimap 74 Set 75 Tree 80 Arrays 85 Array data structure 85 Row-major order 91 Dope vector 93 Iliffe vector 94 Dynamic...

    Java泛型使用详细分析.pdf

    1. 常用集合:list、set、map 2. 泛型语法:集合&lt;String&gt; 比如 list 下面是一个使用泛型的示例代码: ```java @Test public void testList() { List&lt;String&gt; list = new ArrayList(); list.add("aaa"); list....

    ExcelUtils-2.00.zip

    4、#each ${model} ${width1},${width2},model can be a Map,JavaBean,Collection or Array object, #each key will show all property of the model.${width?} means merge ${width?} cells. If only one ...

    hibernate集合的映射

    hibernate集合的映射 集合的映射 set list array map

    Immutable详解及React中实践.pdf

    Immutable.js 是 Facebook 工程师 Lee Byron 花费 3 年时间打造的库,内部实现了一套完整的 Persistent Data Structure,还有很多易用的数据类型,如 Collection、List、Map、Set、Record、Seq。同时,API 也尽量与 ...

Global site tag (gtag.js) - Google Analytics