Java Big Classroom: a common data structure

Source: Internet
Author: User
Tags comparable

JavaBig Classroom: A common data structureBackgroundin the study of computer science, the data structure is a problem that cannot be bypassed. Then I will be in the next time, concise introduction of the common data structure and some source code. Below I will briefly introduce the contents of this big classroom.

As we all know, a good data structure, not only the implementation of data structure, but also the relevant toolset. For example, STL in C + + has a lot of tool functions, such as sort,accumulate,reduce,for_each,remove_if , Count,count_if. In fact , there are similar practical algorithms in Java. I'll show you a common data structure and a collection of tools.


CollectionFrame

Collectionis the most basic set of interfaces, aCollectionrepresents a group ofObject, i.e.Collectionthe Elements(Elements). SomeCollectionallow the same elements while others do not. Some can sort and others can't. JAVASDKdoes not provide direct inheritance fromCollectionthe class,Java SDKthe provided classes are inherited from theCollection"sub-interfaces" such asListand theSet.

All implementations collection The class of the interface must provide two standard constructors: the parameterless constructor is used to create an empty collection collection The constructor of the parameter is used to create a new collection collection collection collection

The List is an ordered Collection, using this interface to precisely control where each element is inserted. The user is able to access the list by using an index ( where the element is positioned in the list , similar to an array subscript ) element, which is similar to the Java the array.

Unlike the Set mentioned below ,theList allows the same elements.

collection interface required iterator () list also provides a listiterator () method, returns a listiterator interface, and the standard iterator interface, compared to listiterator a few more add ()

implement list Common classes for interfaces are linkedlist , arraylist vector stack

Set is a Collection that contains no duplicate elements , that is, any of the two elements E1 and E2 have E1.equals (E2) =false,Set has a maximum of one null element.

Obviously, the constructor of a Set has a constraint that the passed-in Collection parameter cannot contain duplicate elements.

Note: Variable objects (Mutable object)must be handled with care. If a mutable element in a Set changes its state, causing object.equals (Object) =true will cause some problems.

Map Framework

Map is also an interface, but does not inherit the Collection interface. This interface describes a key-to-value mapping that is never duplicated. the Map interface is used to maintain key / value pairs (key/value pairs).

Feature: It describes a mapping of key-to-value values that are never duplicated.

two important implementation classes: HashMap and the TreeMap

1.HashMap, the Chinese call hash list, based on the hash table implementation, the characteristic is the key value pair's mapping relation. A key corresponds to a Value. The ordering of elements in HashMap is not fixed. More suitable for inserting, deleting, and positioning elements.

2.TreeMap, based on red-black tree implementation. TreeMap keep the elements in a fixed order. More suitable for sequential traversal of elements

Iterators

An iterator is a design pattern that is an object that can traverse and select objects in a sequence, and the developer does not need to know the underlying structure of the sequence. Iterators are often referred to as " lightweight " objects because they are less expensive to create.

CollectionsTool Class

Java.util.Collections is a wrapper class (tool class / helper Class). It contains a variety of static polymorphic methods related to set operations. This class cannot be instantiated, like a tool class, that is used to sort, search, and line Cheng the elements of a collection, serving the Collection framework of Java .

It is similar to the sort,remove , etc. in the STL .

Comparator

The comparator for J Ava has two classes, the comparable interface and the Comparator interface, respectively.

When sorting an array of objects, the role of the comparer is obvious.

The difference is that comparable is implementsby comparing the object itself, adding methods. This is similar to the C + + class internal overloaded operator ' < '

Comparator need to implement a comparison class. This is similar to the C + + class outside the implementation of comparison functions or pseudo functions.


Summarize

When we learn to use Java 's class library, we should not only learn how to use it, but also know why. On the other hand, have a broad vision and write good-looking code. For example, sorting, using the interface provided by the system, rather than implementing it yourself. Try to use Lambda instead of anonymous inner classes. Next, the second will be the list interface in the Collection framework .


Java Big Classroom: a common data structure

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.