In this chapter we discuss the map two more common implementations: TreeMap and Linkedhashmap.1.TreeMapFeatures: Sort by keyPackage Com.ray.ch14;import Java.util.comparator;import Java.util.treemap;public class Test {public static void main ( String[] args) {treemapOutput:{1=1, 2=2, 4=4}----------------{a=1, b=3, c=2, d=4}Since TreeMap is sorted by key, we can get some results by sorting through the map, su
Commonalities: HashMap, LinkedHashMap, and TreeMap all belong to Map. Map is mainly used to store key-value pairs and obtain values based on keys. Therefore, duplicate keys are not allowed, the allowed values are repeated. Differences: 1. the key-value pairs stored in HashMap are random during retrieval, which is also our most commonly used Map. it stores data based on the HashCode value of the key, and can directly obtain its value based on the key,
HashMap is that the latter maintains a doubly linked list that runs on all items. This list of links defines the order of iterations, which can be in the order of insertion or in the order of access. For Linkedhashmap, it inherits all the elements with HashMap, the underlying use of a hash table, and a doubly linked list. Its basic operation is similar to the parent class HashMap, which implements its own list of linked properties by overriding the methods associated with the parent class.3.
Import Java.util.scanner;import java.util.set;import java.util.treemap;/* * Requirements: "AABABCABCDABCDE", Gets the number of occurrences of each letter in a string requires results: A (5) B (4) C (3) d (2) e (1) * * Analysis: * A: Define a string (can be improved for keyboard entry) * B: Define a TREEMAP set * key: Character * Value: Integer * C: Convert a string to a character array * D: Iterate through the character array to get each character *
The relationship between TreeSet and TreeMap:
1. TreeSet actually uses treemap to organize the data, because a navigablemap
2. Both TreeSet and treemap use binary tree data structures to store
The data saved in 3.TreeSet and TreeMap requires the implementation of the comparable interf
Java TreeMap source code parsing, javatreemap
This article introduces another important class TreeMap in the Map series. You may feel that there are many articles about HashMap on the Internet, but there are not so many articles about TreeMap. The reason is: on the one hand, there are many HashMap application scenarios; second, compared with HashMap,
One, Common ground:
(1) Hashmap,linkedhashmap,treemap all belong to map;
(2) The MAP is primarily used to store key (key) value pairs, and the keys do not allow keys to repeat, but allow values to repeat. Two, different points:(1) hashmap stored in the key value in the removal of the time is random, is also our most commonly used a map. It stores data according to the hashcode value of the key, which can be to get its value directly, with a fast acc
code example:Packagecom.dt.scala.datasetobjectset_map{defmain (args:Array[String]) :unit={//set the collection, the data in the collection is non-repeatable. But the data is unordered. valdataset=set (1,2,5,4,1,3,2) println (dataSet ) valdataset2=dataset.+ (1) //add elements to the collection, Generate another collection valdataset3=dataset.++ (DataSet2) // Two collection merge, return new collection importscala.collection.mutable// Define variable collection valdata=mutable. set.empty[int]data
UnneCessary formalities as well. That's why electronic commerce are preferable to the prosperity of world economy and it also give birth to SOHO, a special Group of people working at home. The trend towards a promising e-commerce is inevitable. Therefore let's get prepared to embrace this irretrievable trend.idea: First use the regular expression to divide the composition into multiple English words, then map each word with map and calculate the number of times, and finally use
Packagecn.itcast.p1.map.test;ImportJava.util.Iterator;ImportJava.util.Map;ImportJava.util.TreeMap; Public classTestMap {/*** Exercise: * "FDGAVCBSACDFS+++AABBB" gets the number of occurrences of each letter in the string. * Required printing results are: A (2) B (1) ...; * Idea: * The analysis of the results shows that there is a mapping between the letters and the number of times. And it's a lot of relationships. * Many require storage, and the containers that can store the mappings
Just beginning to see HASHTABLE,HASHMAP and TreeMap when more dizzy, feel the role of almost, but to the actual use of time and found there are many differences. So I searched some relevant information to learn, the following is my study precipitation.Java defines an interface java.util.Map for the mappings in the data structure, and HashMap Hashtable and TREEMAP are its implementation classes. A map is an
TREEMAP structure is red black tree1. First introduce the Balanced binary treeIt is characterized by an empty tree or its left and right two sub-tree height difference of the absolute value of not more than 1, and about two sub-trees are a balanced binary tree. That is to say, any one of the two fork tree nodes, the height of the left and right subtree are similar.2. Red black tree is a self-balancing binary search tree(1) Retrieval efficiency O (log
TREEMAP structure is red black tree1. First introduce the Balanced binary treeIt is characterized by an empty tree or its left and right two sub-tree height difference of the absolute value of not more than 1, and about two sub-trees are a balanced binary tree. That is to say, any one of the two fork tree nodes, the height of the left and right subtree are similar.2. Red black tree is a self-balancing binary search tree(1) Retrieval efficiency O (log
For HashMap, Hashtable, TreeMap, linkedhashmap of the internal sorting, found that a lot of people on the internet have misunderstood.
For example, some people think:
Hashtable.keyset () Descending
Treemap.keyset () Ascending
Hashmap.keyset () Disorderly order
Linkedhashmap.keyset () Original sequence
Some people think that the difference between keyset and EntrySet is caused by. So I'm going to go through two traversal ways to give you a test.
Put a
Differences between collection, MAP, hashmap, hashtable, treemap, list, vector, and arraylistCollection interface, including list and set sub-interfacesThe main difference between the collection and map interfaces is that the collection stores a group of objects, while the map stores the keyword/value pairs.In a map object, each keyword can have at most one associated value.Map: it cannot contain two identical keys. One key can be bound to at most one
Hashtable: inherits from the dictionary class and implements the map interface. The key or value is not allowed to be empty and the thread is synchronized;
Hashmap: inherits from the abstractmap class and implements the map interface. The key or value is null and the thread is not synchronized;
Linkedhashmap: inherits from the hashmap class, implements the map interface, allows the key or value to be empty, and stores the record insertion sequence, the thread is not synchronized.
1. Hashtable:(1) is a two-dimensional array containing one-way chain, the table array is entry(2) The value to be placed cannot be null;(3) thread-safe, all methods are modified with synchronized;2. HashMap:(1) equivalent to the upgrade version of Hashtable;(2) can be placed in a null value;(3) Based on the hash table;(4) stored in the order of insertion;(5) Thread is unsafe: because of multithreaded environment, using HASHMAP to put operation will cause a dead loop, resulting in CPU utilization
TreeSet requires that the class to which the object belongs must implement the comparable interface, which provides the CompareTo () method of the comparison element, which is called back when the element is inserted and the method compares the size of the element. treemap requires that keys stored in a key pair map must implement the comparable interface to sort the elements according to the key. The sort method of the collections tool class has two
Hashtable, Hashmap, and TreeMap are the most common map implementations, which are container types that store and manipulate data in the form of key-value pairs.Hashtable is a hash implementation provided by the Java class Library that is inherently synchronous, does not support null keys and null values, and is rarely recommended because of the performance overhead associated with synchronization.HashMap is the application of a more extensive hash ta
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.