Linked Questions

820 votes
15 answers
362k views

What is a raw type and why shouldn't we use it?

Questions: What are raw types in Java, and why do I often hear that they shouldn't be used in new code? What is the alternative if we can't use raw types, and how is it better?
polygenelubricants's user avatar
498 votes
27 answers
966k views

When do you use Java's @Override annotation and why?

What are the best practices for using Java's @Override annotation and why? It seems like it would be overkill to mark every single overridden method with the @Override annotation. Are there ...
Alex B's user avatar
  • 24.8k
177 votes
20 answers
305k views

Java ArrayList - how can I tell if two lists are equal, order not mattering?

I have two ArrayLists of type Answer (self-made class). I'd like to compare the two lists to see if they contain the same contents, but without order mattering. Example: //These should be equal. ...
iaacp's user avatar
  • 4,745
131 votes
12 answers
189k views

Sorting an ArrayList of objects using a custom sorting order

I am looking to implement a sort feature for my address book application. I want to sort an ArrayList<Contact> contactArray. Contact is a class which contains four fields: name, home number, ...
Sameera0's user avatar
  • 1,333
125 votes
19 answers
132k views

When to use Comparable and Comparator

I have a list of objects I need to sort on a field, say Score. Without giving much thought I wrote a new class that implements Comparator, that does the task and it works. Now looking back at this, ...
pkrish's user avatar
  • 2,279
91 votes
5 answers
18k views

Java Integer compareTo() - why use comparison vs. subtraction?

I've found that java.lang.Integer implementation of compareTo method looks as follows: public int compareTo(Integer anotherInteger) { int thisVal = this.value; int anotherVal = anotherInteger....
Vladimir's user avatar
  • 12.9k
58 votes
8 answers
139k views

How to sort a Collection<T>?

I have a generic Collection and am trying to work out how I can sort the items contained within it. I've tried a few things but I can't get any of them working.
Mercer's user avatar
  • 9,886
12 votes
3 answers
13k views

What is the sorting algorithm for Java [closed]

How does OpenJDK internally sort the datatypes and why ? It would be great if the specific algorithms can be mentioned
user avatar
8 votes
2 answers
20k views

sort arraylist of complex objects alphabetically

I know that Collections.sort(myArrayList) can sort an arraylist alphabetically when they are strings, but what about when they are something more complex such as a data object containing two or more ...
CQM's user avatar
  • 43.5k
7 votes
4 answers
4k views

I want to use Collections.sort() twice on two different parameters [duplicate]

I have a list of objects that I want to sort using different properties. @Override public int compareTo(Object obj) { Field tab = (Field) obj; int objx = Integer.parseInt(tab.getX()); // ...
SOP's user avatar
  • 801
4 votes
2 answers
106 views

Customized sort on list of objects

I have list of objects like actors = [Person('Raj' ,'Hindi'), Person('John', 'English'), Person('Michael' 'Marathi'), Person('Terry','Hindi'), Person('...
Shirish's user avatar
  • 53
4 votes
6 answers
475 views

ComperatorFactory using static methods or each in its own class?

I was thinking of implementing multiple Comperators. Now I am not sure about how to do this in a most favourable way, i.e. still having the ability to do dependency injection. Method 1: One class is ...
Benjamin Marwell's user avatar
4 votes
3 answers
2k views

How to understand the order induced by the comparator in Java?

I was so confused about comparator and Collections.sort() in Java. I don't understand the order induced by the comparator. I don't clear about which number the compare function should return to get ...
Minh Tri Le's user avatar
3 votes
7 answers
5k views

How can we pass a variable field /method name in Comparator.comparing

I have a Report {String name, Date date, int score } class. I want to be able to sort a list of reports for any member variable using the new java 8 syntax So java 8 provides this new list.sort(...
dogfish's user avatar
  • 2,776
3 votes
3 answers
808 views

Writing a comparator that sorts based on multiple fields

I have 3 data fields, let's name them Field 1, Field 2, Field 3 Now let's say I have these values: Field 1 Field 2 Field 3 1 3 4 2 3 3 3 3 5 ...
dtgee's user avatar
  • 1,272

15 30 50 per page
1
2 3 4 5 6