comparable

Java HashSet is allowing dupes; problem with comparable?

孤者浪人 提交于 2019-12-05 02:28:01
I've got a class, "Accumulator", that implements the Comparable compareTo method, and I'm trying to put these objects into a HashSet. When I add() to the HashSet, I don't see any activity in my compareTo method in the debugger, regardless of where I set my breakpoints. Additionally, when I'm done with the add()s, I see several duplicates within the Set. What am I screwing up, here; why is it not Comparing, and therefore, allowing the dupes? Thanks, IVR Avenger What am I screwing up, here? HashSet is based on hashCode() , not on compareTo() . You may be confusing it with TreeSet . In both cases

implementing Comparable interface from abstract class with concrete subclasses

白昼怎懂夜的黑 提交于 2019-12-05 02:10:20
问题 package geometricobject; public abstract class GeometricObject implements Comparable { private String color = "white"; private boolean filled; private java.util.Date dateCreated; protected GeometricObject(){ dateCreated = new java.util.Date(); } protected GeometricObject(String color, boolean filled){ dateCreated = new java.util.Date(); this.color = color; this.filled = filled; } public String getColor(){ return color; } public void setColor(String color){ this.color = color; } public boolean

List with Comparable Vs TreeSet

前提是你 提交于 2019-12-04 23:37:58
问题 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. 回答1: The most important differences: Criterion | List with Collections.sort | TreeSet ----------------+----------------------------+---------

Java 8: implementing Comparable

夙愿已清 提交于 2019-12-04 22:12:34
问题 I like the new static factory methods of Comparator , as they allow to implement Comparators in a very concise and less error-prone way. But what is the recommended way to implement Comparable ? Should we use Comparators inside the Comparable implementation? public MyClass implements Comparable<MyClass>{ ... public int compareTo(MyClass other){ Comparator<MyClass> naturalOrderComparator = Comparator.comparing(MyClass::getFoo) .thenComparing(MyClass::getBar); return naturalOrderComparator

Cannot use comparable with father-son-grandson inheritance

*爱你&永不变心* 提交于 2019-12-04 18:15:21
问题 Given the following code : public abstract class Participant { private String fullName; public Participant(String newFullName) { this.fullName = new String(newFullName); } // some more code } public class Player extends Participant implements Comparable <Player> { private int scoredGoals; public Player(String newFullName, int scored) { super(newFullName); this.scoredGoals = scored; } public int compareTo (Player otherPlayer) { Integer _scoredGoals = new Integer(this.scoredGoals); return

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

你说的曾经没有我的故事 提交于 2019-12-04 14:59:36
问题 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

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

戏子无情 提交于 2019-12-04 06:35:31
问题 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

LINQ: Use .Except() on collections of different types by making them convertible/comparable?

笑着哭i 提交于 2019-12-04 06:08:04
Given two lists of different types, is it possible to make those types convertible between or comparable to each other (eg with a TypeConverter or similar) so that a LINQ query can compare them? I've seen other similar questions on SO but nothing that points to making the types convertible between each other to solve the problem. Collection Types: public class Data { public int ID { get; set; } } public class ViewModel { private Data _data; public ViewModel(Data data) { _data = data; } } Desired usage: public void DoMerge(ObservableCollection<ViewModel> destination, IEnumerable<Data> data) { /

Sorting ArrayList of Arraylist<String> in java

孤街浪徒 提交于 2019-12-04 03:40:44
问题 I have an ArrayList of ArrayList of String. In Outer ArrayList on each index each Inner ArrayList has four items have four parameters. Contacts Id Contacts Name Contacts Adress Contacts Number Now I want to sort the complete ArrayList of the on the basis of Contact Name Parameter. Means I want to access the outer Arraylist and the inner ArrayList present on each index of outer Arraylist should be sorted according to contact Name. Comparator / Comparable Interfaces not likely to help me.

implementing Comparable interface from abstract class with concrete subclasses

℡╲_俬逩灬. 提交于 2019-12-03 21:09:27
package geometricobject; public abstract class GeometricObject implements Comparable { private String color = "white"; private boolean filled; private java.util.Date dateCreated; protected GeometricObject(){ dateCreated = new java.util.Date(); } protected GeometricObject(String color, boolean filled){ dateCreated = new java.util.Date(); this.color = color; this.filled = filled; } public String getColor(){ return color; } public void setColor(String color){ this.color = color; } public boolean isFilled(){ return filled; } public void setFilled(boolean filled){ this.filled = filled; } public