☰
-
Php 95
-
Java 94
-
Javascript 37
-
Regex 18
-
Git 17
-
Security 16
-
Docker 07
-
Python 58
-
Machine Learning 14
-
Book Highlights
Java
/
Collections
- 1 Basics 9
-
Classes S
-
Objects S
-
Arrays S
-
Variables S
-
Loops S
-
Numbers S
-
Strings S
-
Exceptions S
-
Regexp S
- 2 OOP 9
-
Inheritance
-
Polymorphism
-
Static S
-
Abstract
-
Interfaces
-
Constructors S
-
Packages
-
Nested Classes
-
Final
- 3 Compiler 2
-
Sublime Text S
-
Apache Ant
- 4 Collections 8
-
Lists
-
Comparable S
-
Sets
-
Maps
-
Generics
-
Properties
-
Streams
-
Json
- 5 Threads 4
-
Create Thread S
-
Sleep
-
Lock
-
Scheduler
- 6 Design Patterns 4
-
Singleton
-
Observer
-
Strategy
-
Mediator
- 7 Swing 12
-
Frame
-
Panel
-
Listener
-
Combo Box
-
Label
-
Image
-
Menu
-
Table
-
Layout
-
Drawing
-
Timer
-
Designer
- 8 I/O 7
-
Streams IO
-
Socket
-
Watching Files
-
Mail
-
Logger
-
Clipboard
-
Encrypt S
- 9 Effective 7
-
Constructors S
-
Dependency Injection
-
Composition
-
Interfaces Default
-
Import Static S
-
Enums
-
Lambdas
- 10 Junit 5
-
About Junit S
-
Test Case
-
Suite Test
-
Annotations
-
Exceptions
- 11 Lambdas 7
-
Expressions S
-
Functional Interfaces
-
Streams
-
Common Operations
-
Default Methods
-
Static Methods S
-
Single Responsibility
- 12 JavaFX 6
-
Openjfx
-
Scene Builder
-
First App
-
Jar Archive
-
On Action
-
Change Listener
- 13 Maven 4
-
Demo
-
Spring Boot
-
Junit
-
Guava
- 14 Spring Boot 8
-
Quick start S
-
Rest service
-
Consuming Rest
-
Templates
-
Security
-
Command Line
-
Scheduling Tasks
-
Ajax
S
R
Q
Java Collections Comparable
ArrayList doesn't have a sort method There is a sort method in the Collections class class A implements Comparable<Song> {} ArrayList<Song> B = new ArrayList<A>() Collections.sort(B)
Sort
p535 ArrayList doesn't have a sort method.
/**
* ArrayList doesn't have a sort method.
*
* There is a sort method in the Collections class ...
* that takes a List as argument
*/
package com.minte9.collections.comparable;
import java.util.ArrayList;
import java.util.Collections;
public class Sort {
public static void main(String[] args) {
ArrayList<String> myList = new ArrayList<>();
myList.add("AB");
myList.add("BC");
myList.add("AD");
Collections.sort(myList); // Look Here
System.out.println(myList); // [AB, AD, BC]
}
}
Comparable
When you are sorting objects, the compiler doesn't know what to sort.
/**
* When you are sorting objects, the compiler doesn't know what to sort
* We need to implements Comparable
*
* Comparator is external to the element type you're comparing ...
* it is a separate class.
*
* You can make as many of these as you like!
*/
package com.minte9.collections.comparable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Album {
public static void main(String[] args) {
ArrayList<Song> myList = new ArrayList<Song>();
myList.add(new Song("A", "1"));
myList.add(new Song("B", "3"));
myList.add(new Song("C", "2"));
Collections.sort(myList);
System.out.println(myList); // [A:1, B:3, C:2]
Collections.sort(myList, new ArtistComparator());
System.out.println(myList); // [A:1, C:2, B:3]
}
}
class Song implements Comparable<Song> {
String title;
String artist;
@Override public int compareTo(Song s) {
return title.compareTo(s.title); // Look Here
// Uses overrided method toString()
}
public Song(String t, String a) {
title = t;
artist = a;
}
@Override public String toString() {
return title + ":" + artist;
}
}
class ArtistComparator implements Comparator<Song> {
@Override public int compare(Song first, Song second) {
return first.artist.compareTo(second.artist); // Look Here
// second comparison using Comparator
}
}
➥ Questions
Last update: 10 days ago