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 13
-
Quick start S
-
Rest service S
-
Consuming rest S
-
Templates S
-
Security auth S
-
Command line S
-
Scheduled task S
-
Ajax S
-
Jdbc mysql S
-
Encrypt password S
-
Https S
-
Jwt S
-
Post request S
R
Q
HashSets
With HashSet with have no duplicates, but we loose sorting.
/**
* HashSet have no duplicates, but no sorting.
*/
package com.minte9.collections.sets;
import java.util.HashSet;
public class HashSets {
public static void main(String[] args) {
HashSet<Song> mySet = new HashSet<Song>();
mySet.add(new Song("A", "2"));
mySet.add(new Song("C", "1"));
mySet.add(new Song("B", "4"));
mySet.add(new Song("B", "3"));
System.out.println(mySet); // [A:2, B:3, C:1]
// no duplicates
// hash code
/*
65
67
66
66
*/
}
}
class Song implements Comparable<Song>{
public String title;
public String artist;
@Override public boolean equals(Object o) {
Song s = (Song) o;
return title.equals(s.title); // Look Here
}
@Override public int hashCode() {
return title.hashCode(); // Look Here
}
@Override public int compareTo(Song s) {
return title.compareTo(s.title);
}
public Song(String t, String a) {
title = t;
artist = a;
}
@Override public String toString() {
return title + ":" + artist;
}
}
Trees
TreeSet prevents duplicates and keeps the list sorted.
/**
* TreeSet is similar to HashSet in that it prevents duplicates.
* But it also keeps the list sorted (very small performance hit).
*/
package com.minte9.collections.sets;
import java.util.ArrayList;
import java.util.TreeSet;
public class TreeSets {
public static void main(String[] args) {
ArrayList<A> myList = new ArrayList<>();
myList.add(new A("F", "1"));
myList.add(new A("G", "2"));
myList.add(new A("H", "4"));
myList.add(new A("H", "3"));
TreeSet<A> myTree = new TreeSet<A>();
myTree.addAll(myList);
System.out.println(myTree); // [F:1, G:2, H:4]
}
}
class A implements Comparable<A> {
public String title;
public String artist;
public A(String t, String a) {
title = t;
artist = a;
}
@Override public int compareTo(A a) {
return title.compareTo(a.title);
}
@Override public String toString() {
return title + ":" + artist;
}
}
➥ Questions