Java Programmer successful Interview cheats

Source: Internet
Author: User
Tags add numbers

[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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.