Brief analysis of comparable interface and collection

Source: Internet
Author: User

Today LZ in Do Huffman codec, need to do the module has one is, for the characters appear frequency to order from small to large, and then continue to build frequency sort sequence, My last option was to use LinkedList to emulate the Huffmannode queue, so a question was how to sort the objects by their attributes. 1): On the internet to see the discovery can let the Huffmannode class implementation comparable interface, note is the CompareTo (object that) method, the return is an integer, 0, negative. When the object is larger than that, returns an integer, which is smaller than that and returns a negative number; equals, 0.
public class Huffnode implements comparable{
Public Char lett;//Letter
public int freq;//Frequency
Public Huffnode left;//left node
Public Huffnode right;//Right node
Public Huffnode () {
Lett = ' + ';
freq = 0;
left = right =null;
}
Public huffnode (int freq) {
This.freq = freq;
}
/**
* and that comparison frequency size
* @param that
* @return
*/
public int CompareTo (Object) {
Huffnode other = (huffnode) that;
return freq-other.freq;
}
Public String toString () {
return lett+ "/" +FREQ;
}
}2) So how to use the sort? Combined with the problem, I need a queue, then I'll write an interface pqueue

Next write our Custom queue class:


Here I use is linkedlist to simulate, because LinkedList inherited the Queue<e> interface, directly with offer and poll method said so long to everyone see results as follows: 1 2 2 It is true that the analysis is right. The next blog LZ will say a simple Huffman codec how to do, thank you


Brief analysis of comparable interface and collection

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.