comparable

Java: Sort a Collection using a CollatorKey

纵饮孤独 提交于 2019-12-03 14:41:57
what I would like to achieve is to sort a colletion of objects by a string value. However in a locale dependant way using a collator. Due to performance reasons I do not want to use the Collator compare() method (as below in the code) rather the CollationKey class, as the java API states the using a CollationKey is much faster. But how do I implement the compareTo() method using the CollationKey? As far as I understood it, I have to completely write all the comparison Methods on my own if I will be using a CollationKey. So I will even no longer be able to use the Collections.sort() methods...

List with Comparable Vs TreeSet

人盡茶涼 提交于 2019-12-03 14:20:09
Option 1: Make a list which implements Comparable and sort it using collections.sort(List l) every time you add a value. Option 2: Make a TreeSet (which keeps itself sorted all the time). Which one will be faster? I am asking this because List gives me the option of ListIterator which I need in my case, since it lets me add an element while iterating. The most important differences: Criterion | List with Collections.sort | TreeSet ----------------+----------------------------+--------- cost of adding | O(n log n) | O(log n) equal sort keys | permitted | eliminated as duplicates iteration |

Why is Stream.sorted not type-safe in Java 8?

馋奶兔 提交于 2019-12-03 09:21:34
This is from the Stream interface from Oracle's implementation of JDK 8: public interface Stream<T> extends BaseStream<T, Stream<T>> { Stream<T> sorted(); } and it is very easy to blow this up at run time and no warning will be generated at compile time. Here is an example: class Foo { public static void main(String[] args) { Arrays.asList(new Foo(), new Foo()).stream().sorted().forEach(f -> {}); } } which will compile just fine but will throw an exception at run time: Exception in thread "main" java.lang.ClassCastException: Foo cannot be cast to java.lang.Comparable What could be the reason

Java 中 Comparable 和 Comparator 比较

泄露秘密 提交于 2019-12-02 20:26:47
本文,先介绍 Comparable 和 Comparator 两个接口,以及它们的差异;接着,通过示例,对它们的使用方法进行说明。 Comparable 简介 Comparable 是排序接口。 若一个类实现了Comparable接口,就意味着“ 该类支持排序 ”。 即然实现Comparable接口的类支持排序,假设现在存在“实现Comparable接口的类的对象的List列表(或数组)”,则该List列表(或数组)可以通过 Collections.sort(或 Arrays.sort)进行排序。 此外,“实现Comparable接口的类的对象”可以用作“有序映射(如TreeMap)”中的键或“有序集合(TreeSet)”中的元素,而不需要指定比较器。 Comparable 定义 Comparable 接口仅仅只包括一个函数,它的定义如下: package java.lang; import java.util.*; public interface Comparable<T> { public int compareTo(T o); } 说明: 假设我们通过 x.compareTo(y) 来“比较x和y的大小”。若返回“负数”,意味着“x比y小”;返回“零”,意味着“x等于y”;返回“正数”,意味着“x大于y”。 Comparator 简介 Comparator 是比较器接口。

Comparing different type of Objects with comparable

隐身守侯 提交于 2019-12-02 15:12:06
问题 A.java public class A implements Comparable { private String id; private String name; public A(String a, String b) { id = a; name = b; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int compareTo(Object o) { A a = (A) o; return id.compareTo(a.getId()); } } B.java public class B implements Comparable { private String b_id; private String other; public B

Why am i getting a class cast exception(with generics, comparable)?

百般思念 提交于 2019-12-02 12:30:13
I am trying to implement a sorted and unsorted array list. Both extend a class called AbstractArrayMyList which contains common operations/implementations - toString, clear, etc.... Here is my code for AbstractArrayMyList(which implements a generic interface I defined) public abstract class AbstractArrayMyList<E> implements MyList<E> { protected E[] elementData; ..... } I chose to make elementData protected so that sorted and unsorted specialized array lists can access and perform operations on it. Here is my declaration/code for sorted array list public class ArrayListSorted<E extends

Comparing different type of Objects with comparable

*爱你&永不变心* 提交于 2019-12-02 07:40:58
A.java public class A implements Comparable { private String id; private String name; public A(String a, String b) { id = a; name = b; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int compareTo(Object o) { A a = (A) o; return id.compareTo(a.getId()); } } B.java public class B implements Comparable { private String b_id; private String other; public B(String a, String b) { b_id = a; other = b; } public String getBId() { return b_id; } public void setBId

How to sort two different objects in a collection?

陌路散爱 提交于 2019-12-02 06:30:53
问题 Suppose I have two classes CLassA and CLassB. And they have one atributte in common, for example the number of elements that each class holds. How can i create a collection from objects of ClassA and CLassB and sort by that attribute (ascending of descending order, doesn't matter)? I made a collection of type but when I try to implement the Comparable Interface i can't acess to that method (a get that returns the nr of elements for example). What solutions do I have? Thanks for your help! 回答1

Comparing generic types using Comparable and Comparator

倖福魔咒の 提交于 2019-12-02 04:08:49
问题 I've run into a headache I'm having difficulty debugging. I am trying to compare two generic values so I can insertion sort them according to values into an array. This is my first time working with the Comparable and Comparator interfaces so any additional advice surrounding these issues would be great. This is how my class is set up: public class SVStore<K, V extends Comparable<V>> implements Pairs<K, V>, Iterable<K>, Comparable<V>, Comparator<V> { The put() method: @Override public V put(K

Consistent Equals() results, but inconsistent TreeMap.containsKey() result

末鹿安然 提交于 2019-12-02 02:53:51
问题 I have the following object Node : private class Node implements Comparable<Node>(){ private String guid(); ... public boolean equals(Node o){ return (this == o); } public int hashCode(){ return guid.hashCode(); } public int compareTo(Node o){ return (this.hashCode() - o.hashCode()); } ... } And I use it in the following TreeMap : TreeMap<Node, TreeSet<Edge>> nodes = new TreeMap<Node, TreeSet<Edge>>(); Now, the tree map is used in a class called Graph to store nodes currently in the graph,