[TOC] Java Programmer successful interview cheats
tags (space-delimited): Java Fourth to write the core algorithm
The difference between comparable and comparator interfaces
Test:
@Test public
void Sortints () {
final int[] numbers = { -3, -5, 1, 7, 4,-2};
Final int[] expected = { -5, -3,-2, 1, 4, 7};
Arrays.sort (numbers); Ascending rank
assertarrayequals (expected, numbers);
Collections.sort (Numbers, new Reversenumericalorder ());//descending order
assertequals (expected, numbers);
Reversenumericalorder.java descending order
Import Java.util.Comparator;
public class Reversenumericalorder implements comparator<integer> {
@Override public
int Compare ( Integer O1, integer o2) {return
o2-o1;
}
equals omitted
}
About Compare (Object o1,object O2)
Comparator's compare several sort bubble sort Insert sort quick sort Merge sort
Quick Sort. Java implementation method. It's very java.
Import Org.junit.Test;
Import java.util.ArrayList;
Import Java.util.Arrays;
Import java.util.List;
Import static junit.framework.Assert.assertEquals;
public class Quicksort {public
static list<integer> Quicksort (list<integer> numbers) {
if ( Numbers.size () < 2) {return
numbers;
}
Final Integer pivot = numbers.get (0);
Final list<integer> lower = new arraylist<> ();
Final list<integer> higher = new arraylist<> ();
for (int i = 1; i < numbers.size (); i++) {
if (numbers.get (i) < pivot) {
Lower.add numbers.get (i));
else {
Higher.add (Numbers.get (i));}
Final list<integer> sorted = quicksort (lower);
Sorted.add (pivot);
Sorted.addall (Quicksort (higher));
return sorted;
}
Fifth chapter data Structure
Array implementation of the ArrayList list (attributes of the array)
LinkedList List's bidirectional linked list realization (bidirectional linked list's characteristic)
JVM system.arraycopy
Queue
Queue
Hash
HASHMAP: The storage location of the key is determined by the hashcode of the object
The order of the Treemap:treemap keys is unchanged (binary tree)
Linkedhashmap work the same way as HashMap, a linked list is implemented, which means that the traversal insertion sequence is the same
Concurrenthashmap: Thread-safe, multithreaded shared data usage
Hash
Comparison of several maps
Set
HashSet-->hashmap: The storage location of the key is determined by the hashcode of the object
TreeSet--> Treemap:treemap The order of the keys is unchanged (binary tree)
Linkedhashset--> linkedhashmap Work the same way as HashMap, the linked list is implemented, which means that the traversal insertion sequence is the same
Several set