On Java Collection Class (iii) "Set,queue"

Source: Internet
Author: User
Tags comparable

Set is an interesting collection. It looks a bit like a list, but the actual operation is a bit like a map. Set is also a collection of records of a series of values, but it is the same as the map key, it cannot be duplicated, otherwise it will fail to insert. So, we can often see that keyset () can be called in the map. Speaking of which, it seems to have been able to do a lot of things. If you are interested in doing web crawlers, there is a problem of filtering duplicate links to be considered. Set seems to be a very simple solution, we can use it to come and go heavy.

Well, let's see how it's written:

Boolean Add (E Element)//Adds a new element, if set contains the same element, returns FALSE, successfully returns Trueboolean AddAll (collection<? extends e> c) Adds all the elements in the collection to the set, returns True to add a successful Boolean remove (Object o)//Removes the specified element, and returns True if the element is contained in the set

Now let's talk about his main implementation class: Hashset,treeset.

HashSet class

Hash we said a lot, he, is a list of lists, each linked list is called a bucket. by calling Hashcode () we can figure out which poke he is in, that is, the total number of hashcode% barrels. Determining which bucket is not enough, because in the same bucket, it could be the same element, or it could just be calculated in the same bucket by hashcode, by calling EQUASL () to know if it was in the same bucket. If we write a class, rewrite Hashcode (). Is it always returns true, then he is just a linkedlist, and there are a few more steps, so the efficiency is certainly very bottom. As before, you can specify an initial size, and a reload factor. The system default initial size is 16, filling factor 0.75. When we specify the initial size, the system automatically turns to the next power of 2, and we start by initializing it, recommending 75%~150%.

TreeSet class

It is an ordered set (sorted collection) that can insert elements into the collection and output them in a certain order. Since it is orderly, it is necessary to compare methods, you can implement the Comparale interface in the passed-in class, or pass directly to an implementation class of the comparator interface. The usage is similar to the previous introduction.

Java.uitl.sortedset<e> interface comparator<? Extends E>comparator ()//Gets the comparer used to sort the elements, and returns Nulle first () E last () if the element is implemented with comparable CompareTo method Returns the largest or smallest element in the collection Java.util.navigableset<e>e Higer (e value) e lower (e value)//returns the smallest element greater than value or the largest element less than value. If no suitable element is returned Nulle ceilling (e value) e floor (e value)//returns the smallest element greater than or equal to value or the largest element less than or equal to value. There is no appropriate return Nulle Pollfirst () E polllast ()//delete and return the largest or smallest element in this collection. Returns nulliterator<e> Descendingiterator ()//Returns an iterator that iterates through the collection in descending order

Queue is a first-in-one data structure, and we'll talk about it:

Let's take a look at its interface:

Java.uitl.queue<e> Interface: Boolean Add (e Element) Boolean offer (e Element)//If the queue is not full, add the given element to the tail of the double-ended queue and return true. If the queue is full, the first method runs out of IllegalStateException, and the second Returns Falsee remove () E poll ()//If the queue is not empty, delete and return elements of this queue header, if the queue is empty, The first one will throw Nosuchelementexception, the second method returns Nulle element () E peek ()//If the queue is not empty, return the header element of this queue. If NULL, the first one will throw Nosuchelementexception, the second method returns the Nulljava.util.deque<e> interface: void AddFirst (e element) void AddLast (E Element) Boolean Offerfirst (e Element) Boolean offerlast (e Element)//Adds the specified element to the head or end of the queue. If the team is full, the first two will throw IllegalStateException, and the latter two return Falsee Removefirst () e removelast () e Pollfirst () e polllast ()//If the queue is not empty, Delete and return to Team head or tail. If the team is empty, the first two methods will be thrown illegalstatusexception, and the latter two will return Falsee GetFirst () e getlast () e Peekfirst () e peeklast ()//If the team is not empty, return to the team head or the tail element, If NULL, the first two will throw Nosuchelementexception, and the latter two will return null
Queue, it is our default understanding of the queues, that is, only one head, one tail, and deque is a double-headed queue, and our linkedlist is one of its implementation class
Next, we'll look at the Priorityqueue, which is a priority queue and is an orderly one. Because it is a priority, you also need to have a comparison method, similar to the one described earlier. In the absence of an incoming comparator interface implementation class, the default is to call the CompareTo method of the comparable implemented by the queue object.

I think he's still a fun thing to play. We randomly insert elements and then we can output them in the order we want.


OK, look at this, need to remind a bunch to say add,get,element .... The method of the parent class in the case of an unsatisfied condition, generally throws an exception, and the subclass's own Peek,poll,offer: And so on, the general method is to return false directly.


On Java Collection Class (iii) "Set,queue"

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.