On the sixth day of learning data structure (a) (including some not learning, learning points after the guidance)

Now look at the role of set and map, and the role of treeset.

What also needs to learn for the future:

 

For the analysis corresponding time complexity for the respective ordered content, it is needed to see again.

What is the best, the worst and the like.

Since write data structures to achieve the appropriate code leetcode have to learn, in which parts of the private class in which to write part to write private interface, you have to learn.

For learned before computer networks, operating systems and other knowledge, we need to review the already learned. And then add content to go according to the interview.

 

The corresponding content set and map of:

If appropriate to write their own implementation,

        Set<String> set = new HashSet<>();

        Set<String> set = new TreeSet<>();

        Map<String, Student> map = new HashMap<>();

        Map<String, Integer> map = new TreeMap<>();

Said here is: if their own to write, what you should use, you should use: HashSet ---- "do not need to sort of set

treeset --- need to sort of set. hashMap --- not the sort of map, treemap --- need to sort of map.

 

Then the words to know: When should I use set: hope into the inside of the elements without repetition, you should use set

When should I use the map: want to establish is: when one mapping, then use the map.

Then the difference between the pair and map, they would know that using a more lightweight pair.

 

By keycalculating an index way it is to call the keyobject's hashCode()method, which returns an intinteger. HashMapIt is located by the direct method keycorresponding to valuethe index, and then directly returned value.

Therefore, the proper use Mapmust ensure that:

  1. As the keyobject must override the correct equals()method, two identical keyinstances call equals()must return true;

  2. As the keyobject must also override the right hashCode()method, and hashCode()the method should strictly abide by the following specifications:

  • If two objects are equal, then the two objects hashCode()must be equal;
  • If two objects are not equal, then the two objects hashCode()as not equal.

Here say is: put, what map to use when the attention point is:

If using a container to any function to find an element (search, contains), then the object of this class needs to implement equals function.

The reason: they are equal, it is necessary to define the content. For basic data types, string types cited, they are already written inside this part.

 

 

  1. As the keyobject must also override the right hashCode()method, and hashCode()the method should strictly abide by the following specifications:

  • If two objects are equal, then the two objects hashCode()must be equal;
  • If two objects are not equal, then the two objects hashCode()as not equal.

HashCode function effect: according to the object, they get the hash code. Then list Find

We put different keywith the same hashCode()situation called hash collision. In time of conflict, one of the most simple solution is Listto store hashCode()the same key-value. Obviously, the greater the probability that if the conflict, the Listlonger

Said here is: Why map needs to override hashcode function, because the map is based on the object, find hashcode value, go find the specified object.

That is: If you need to find something, then write equals, if the map, in addition to this, if you as a key, then we still have to override hashcode function.

 

So if they go to write come true:

So linkedlist as map, as the realization set in.

bst as map, as the realization set, then set it inside the thing is sorted, map the key is to sort before. (Because adding a binary search tree which is follow the order to add)

 

BSTMap<K extends Comparable<K>, V> implements Map<K, V>

This sentence is very good, because: if the element is held inside need to be compared, so that the first element extends comparable need to go to class.

And in their entirety, as a realization of the map.

 

 

 

 

 

If the structure is divided into four files, then

A bottom container

2. How to achieve the underlying container interface, remember, this position is the position held by the bottom of the container, as in the above second FIG.

3. Interface: Write only abstract functions

 

Here it is to teach how to write their own interfaces, how to write interfaces.

 

The following is a double file structure:

 

 In fact, the main function has not written, the equivalent of three to the file structure.

 

 The node is stored as a private class

 

 Gave the corresponding function directly put to, the corresponding function is linkedlist come directly merge.

Is to: hold the bottom of the container, which part of the contents changed. Do not hold the bottom of the container, write directly to the bottom of the container is how to achieve.

 

 

The following is a single configuration file, commonly used: leetcode codes or the like.

But also note: leetcode code, usually written directly available to achieve it, there are already existing implementations, and go directly to the call on the line.

 

1. The interface also turn out to be held in private

2. The appropriate implementation of the interface, has become the holder of a private class.

3. Of course, also the bottom of the container, to integration into the implementation of this interface stuff inside.

 

Here that is the practice of a single file structure

 This section hang onto this, and then continue to analyze and then go after the appropriate to do.

Guess you like

Origin www.cnblogs.com/startFrom0/p/12614570.html