`
knight_black_bob
  • 浏览: 841789 次
  • 性别: 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;
		}
分享到:
评论

相关推荐

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

    Java 中 List、Array、Map、Set 等集合相互转换 在 Java 中,集合类型的转换是非常常见的操作,例如将 List 转换为 Array、将 Array 转换为 List、将 Map 转换为 List 等等。今天,我们将详细介绍 Java 中 List、...

    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 的区别和联系进行详细的...

    json与bean,array,list,map,简单类型之间的封装、解析

    本主题主要探讨的是如何使用Gson库处理JSON数据与Java中的Bean、Array、List、Map以及简单类型的相互转换。 首先,我们来看JSON与Java Bean之间的转换。Java Bean是一种具有特定属性和方法的对象,它们通常用来封装...

    Java中Collection、List、Set、Map之间的关系总结

    "Java中Collection、List、Set、Map之间的关系总结" Java中Collection、List、Set、Map之间的关系总结是Java编程语言中最基本的集合接口,了解它们之间的关系对于编程语言的学习和应用至关重要。本文将对Collection...

    list.toArray方法

    在深入讲解之前,我们需要理解Java集合框架的基础,它包括Set、List和Map等接口,以及它们的实现类,如ArrayList、LinkedList和HashMap等。 List接口是Collection接口的子接口,它代表了一个有序的集合,允许有重复...

    Scala的List类方法整合

    list.copyToArray(array, 1, 2) // array 结果为 Array(0, 1, 2, 0, 0) ``` #### 10. `def distinct: List[A]` 此方法返回一个只包含列表中不同元素的新列表。 **示例代码:** ```scala val list = List(1, 2, 2, 3...

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

    本文将对比Map、Set与传统的Array和Object,探讨它们在增、查、改、删操作上的差异。 1. **Map与Array的对比** - **增**:Map使用`set`方法添加键值对,例如`map.set('t', 1)`;而Array则通过`push`方法将对象添加...

    java面试宝典

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

    Scala的Map相关方法整合

    val keysToRemove = Set("a", "c"); m -- keysToRemove` 结果为 `Map("b" -&gt; 2)`。 #### 4. `defget(key:A):Option[B]` 该方法返回指定键对应的值,如果没有找到则返回`None`。 - **示例**:`val m = Map("a" -&gt; 1...

    jQuery的Ajax实现异步传输List、Map

    var mapJson = JSON.stringify(Array.from(myMap.entries())); // mapJson 将会是 '[[\"key1\",\"value1\"],[\"key2\",\"value2\"]]' ``` 然后在Ajax请求中: ```javascript $.ajax({ url: 'your_server_...

    JAVA中list,set,数组之间的转换详解

    在Java编程中,数据结构的转换是常见的操作,特别是对于列表(List)、集合(Set)和数组(Array)之间的转换。这些数据结构各有特点,例如List是有序可重复的,Set是无序不重复的,而数组则是一种固定大小的线性...

    hibernate map 集合映射

    正确配置和使用各种集合类型,如List、Set和Map,可以帮助我们更好地处理数据库中的复杂关系,提高代码的灵活性和可扩展性。同时,关注性能优化策略,如懒加载和批处理,对于大型项目尤为重要。

    java集合框架笔记

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

    java-array.zip_源代码;array

    2. 排序:`Arrays.sort()`方法可以对数组进行排序,而集合框架中的`Collections.sort()`方法则适用于`List`接口实现类。 3. 转换:`Arrays.asList()`可以把数组转换为`List`,`ArrayList`的构造函数可以接受一个数组...

    java容器介绍及应用

    Java集合框架,包括List、Set、Map、ArrayList、LinkedList等,构成了Java容器的主要元素。以下是对这些概念的详细解释: 1. **集合框架**:Java集合框架是Java SE库的一部分,它提供了一组接口和类来处理对象的...

    JAVA-SE入门学习——第八讲集合

    以上内容涵盖了Java集合框架的基础知识,包括Collection接口、Set接口、List接口、Map接口的理解和使用,以及泛型、集合与数组的转换、集合的遍历和复制等重要概念。在实际开发中,掌握这些知识对于编写高效、安全的...

Global site tag (gtag.js) - Google Analytics