, refer to the above map interface. Here are some methods in the map interface.According to the implementation of the set framework, the hash table is a single-chain table as an array of elements. Two or more entries with the same index value are linked together to the single-chain table. The hash table declaration is as follows:Private entry [] table;The component type entry is the implementation of the
significantly improve the performance
TreeMap (): as long as it is a Tree, it must be sorted. TreeMap is the only method with SubMap (), so it can return a subtree.
Example of Map-HashCode:
Import java. util. *; public class hashcode {public static void main (String [] args) {HashMap h2 = new HashMap (); for (int I =
description.
CollectionShortlist│ Invalid parameter list│ ├ Arraylist│ Vector│ Elastic StackSorted setMap├ Hashtable├ Hashmap└ Weakhashmap
Collection InterfaceCollection is the most basic collection interface. A collection represents a group of objects, namely, elements of the collection ). Some collections allow the same elements while others do not. Some can be sorted, while others cannot. The Java SDK d
The Map we usually use can only store the same Key in the Map. The same key we save later will overwrite the original key value, as shown in the following example.
[Java]Import java. util. HashMap;Import java. util.
Collection interface: Collection is the most basic collection interface, declaring a common method for Java collections (including set and list only). Both set and list inherit Conllection,map.
Methods for collection interfaces:
Boolean Add (Object O): Adding a reference to an object in the collectionvoid Clear (): Deletes all objects in the collection, that is, they no longer hold references to these obj
, the first record must be inserted first.
* It is slower than hashmap when traversing.
* (4) TreeMap is able to sort the records it saves according to the key, by default it is sorted in ascending order, or a sort comparer can be specified. When traversing TreeMap with Iteraor,
* The resulting records are sequenced.
*/
/**
* Demonstrate the implementation class for each map
*/
public class
The class hierarchy is as follows:Collection objectsShortlist│ Invalid parameter list│ ├ ArrayList│ Vector│ Elastic StackSorted SetMap├ Hashtable├ HashMap└ WeakHashMap
Next we will introduceCollection InterfaceCollection is the most basic Collection interface. A Collection represents a group of objects, namely, Elements of the Collection ). Some collections allow the same elements while others do not. Some can be sorted, while others cannot. The
Java Collection classes are divided into the following three types:
Category 1: Array, Arrays
Class 2: Collection: List, SetCategory 3: Map: HashMap, HashTable
I. Array, Arrays
Array is the most efficient method for Java's "Storage and random access to a series of objects.
1,High efficiency, but the capacity is fixed and cannot be changed dynamically.Another disadvantage of array is that it cannot determin
On the Internet by chance to see an article about Java commonly used data structure, the analysis is very thorough, hereby note that see:) linear table, linked list, hash table is a commonly used data structure, in Java development, the JDK has provided us with a series of corresponding classes to achieve the basic data structure. These classes are in the Java.util package. This paper attempts to explain th
compare (T O1,to2) method of the interface can be sorted, which is mainly based on the first parameter O1, less than, equal to, or greater than O2 to return negative integers respectively , 0, or positive integer.
(C), Parameters:O1-The first object to compare.O2-The second object to compare.(D), return:Returns a negative integer, 0, or positive integer, respectively, based on the first argument less than, equal to, or greater than the second argume
The map interface differs from the collection interface in that the map interface holds objects that are pairs, similar to Key-value. Common subclasses of the map interface:Take HashMap as an example using map:Import java.util.HashMap; import java.util.Map;p ublic class hashmapdemo01{public static void Main (String arg
CollectionBytesList│ Invalid parameter list│ ├ Arraylist│ Vector│ Elastic StackSorted setMap├ Hashtable├ Hashmap└ Weakhashmap
Collection Interface
Collection is the most basic collection interface. A collection represents a group of objects, namely, elements of the collection ). Some collections allow the same elements while others do not. Some can be sorted, while others cannot. The Java SDK does not provi
Source code analysis of java Collection classes: Map (2), javamap
This section mainly discusses the differences and usage of Several implementation classes of the Map interface.1. Thread Security
Hashtable is thread-safe (similar to Stringbuffer and Vector), but not others. As for why, read some previous articles, which have been described in detail earlier.2. Ap
can map keys to corresponding values. is now replaced by the map interface.TreeMap: Ordered hash list, implements SORTEDMAP interface, bottom layer through red black tree realization. HashMap: is a hash table based on the "zipper method" implementation. The underlying is implemented with an array + linked list.Weakhashmap: A hash table based on the "zipper Method". HashTable: A hash table based on
Use the stream example in Java 8 to sort a Map by keys or values. 1. Quick Start
In Java 8, follow this procedure to sort the map. Convert Map to Stream sort it Collect and return a new Linkedhashmap (keep order)
The Java collection classes fall into three main categories:First Category: Array, ArraysClass II: Collection:list, SetCategory III: MAP:HASHMAP, HashTableOne, Array, ArraysArray is the most efficient of all Java "storage and random access to a series of objects".1, high efficiency, but the capacity is fixed and can not be changed dynamically.One drawback of array is that it is not possible to determine how
class Treemaptest {public
static void Main (string[] args) {
map
The results of the operation are as follows:
D:AAAAA
c:bbbbb
B:CCCCC
a:ddddd
Third, the order of value
The above example is sorted according to the key value of the treemap, but sometimes we need to sort according to the value of TreeMap. To order value we need to use the c
requirement can be thought of as a defined rule or weight.Principle: Place all the elements in the map to be sorted into a list, then use a static method of collections sort (listTo arrange the list, the comparison rules are also defined by the comparator. The elements in the sorted list are then loaded into map, in o
The collections in Java consist of three classes, set, list, and map, all in the Java.util package, and set, list, and map are interfaces that have their own implementation classes. The implementation class of set mainly has the HashSet and Treeset,list implementation class mainly has the Arraylist,map implementation c
Java-15.7 Map (7)-TreeMap and LinkedHashMap
In this chapter, we will discuss two commonly used implementations of Map: TreeMap and LinkedHashMap.
1. TreeMap
Feature: sort by key
package com.ray.ch14;import java.util.Comparator;import java.util.TreeMap;public class Test {public static void main(String[] args) {TreeMap
ma
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.