java data container

 When the design was written today to collect content objects, it got stuck and couldn't decide on a container to store the data. The reason is that I need the internal objects to be sorted from small to large or from large to small, at least in the original order. The HashMap used in the previous design will completely disrupt all the order and give up; Hashtable has an order when the number of elements is less than 20, but once it exceeds 20, the order will be disordered and give up; ArrayList can maintain the original order, but Its key is just an index, so the index must be smaller than the size, give up; TreeMap can be sorted, but it is very depressing, not according to the size of the entire number, but according to the order of the single digits of the number, but think about it carefully, It really can only be arranged in this way, after all, its key is Object. It seems that only two Vectors can be used to install OID and value respectively.
  The following article was found while searching for how to solve this problem. I think it's not bad, and I have summarized several containers that are easily confused.
  
  1. Collection: a set of independent objects
  List: store a set of elements in a specific order [original order]
  Common examples: ArrayList, LinkedList
  Set: elements must not be repeated [reordering]
  Common examples: HashSet
  most commonly used interface: add(element)
  get ()
  iterator()
  
  2. Map: key-value paris, also known as associative array (associative array) [reordering]
  common example: HashMap
  most commonly used interface: put(key, value)
  get()
  
  [container printing]
  
  Provided by the default toString() of each container
  Both Set and Map have an internal ordering mechanism
  
  [container disadvantage: element type is undetermined]
  
  Once an element is put into a container, it will lose its type information and become Object,
  so when an element is removed from the container, the first To convert to the original type, the only exception is String:
  the compiler will automatically call the toString() function
  
  [Iterators]
  
  An iterator is an object whose responsibility is to visit and select a sequence of objects in the sequence
  and iterators Is a "lightweight" object with minimal cost
  
  Collection.iterator() returns an Iterator object
  
  java.util.Iterator [class]
  next() Gets the next element in the sequence, the first call will return the first element
  hasNext() checks if there is the next element in the sequence
  remove() removes the most recent element returned by the iterator
  
  For List there is a more complex ListIterator
  java.util.ListIterator [class]
  add(), remove(), set (),
  hasNext(), next(), nextIndex(),
  hasPrevious(), previous(), previousIndex()
  
  Legacy iterators are Enumeration
  
  [Container taxonomy]
  
  
  [Collection function]
  
  boolean add(Object)* If the argument cannot be added, return false
  boolean addAll(Collection)* As long as there is an element in the argument Collection, return true
  void clear()* Remove (remove) the container
  boolean contains(Object) returns true if the container contains the object represented by the argument
  boolean containsAll(Collection) returns true if the container contains all the elements contained in the argument boolean
  isEmpty()
  Iterator iterator()
  boolean remove(Object )* If the argument value is in the container, remove the element (or one of them). Returns true if removal has occurred
  boolean removeAll(Collection)* Removes all elements in the container. Returns true when the action occurs
  boolean retainAll(Collection)* Retains only the elements in the argument container (intersection). Returns true when the action occurs
  int size() Returns the number of elements in the container
  Object[] toArray() Returns an Array containing all elements in the container
  Object[] toArray(Object[] a) Same as above, but the type of the elements in the Array is the same as The element type of argument a is the same (you still need to change the type of Array yourself)
  
  *optional
  
  [List function]
  
  List order (order) is the most important feature of List; the unique ListIterator (see above)
  ArrayList* allows fast random access; when the insertion/removal occurs in the central position of the List, the efficiency is extremely poor
  . Easy to insert/remove; random access is slow, special functions are as follows:
  addFirst(), addLast(), getFirst(), getLast(), removeFirst(), removeLast()
  Easy to use LinkedList to implement stack, queue, deque
  
  *Default most Best choice
  
  
  [Set function]
  
  Set [interface] Set has the same interface as Collection and
  each element added to Set must be unique - that is, each element must define equals() to judge the uniqueness
  HashSet* A way to look at the search time. Very important Set, each element must define hashCode()
  TreeSet The underlying structure is an ordered (ordered) Set of tree, which can extract an ordered sequence (ordered sequence) from the Set
  
  *The default is the best Select
  
  SortedSet [interface] (TreeSet is its only implementation)
  Comparator comparator() Produces a Comparator "used by this Set", or returns null for sorting in the "natural way".
  Object first() Generates the lowest element
  Object last() Generates the highest element
  SortedSet subSet(fromElement, toElement) Generates a subset of Set, ranging from fromElement (inclusive) to toElement (exclusive)
  SortedSet headSet(toElement) Generates a subset of Set, all elements All are less than toElement
  SortedSet tailSet(fromElement) Generate Set subset, all elements are greater than or equal to fromElement
  
  first last
  [ 0 1 2 3 4 5 6 7 8 ]
  low high
  
  Note when developing your own type (type):
  1. Set needs Maintain the order of its elements in some way, which means you must implement the Comparable interface, and define compareTo()
  2. Don't use the plain and simple comparison form in compareTo(): ​​return i1-i2.
  This spelling is a common mistake. Because only i1 and i2 are unsigned (unsigned, but there is no such keyword in Java) int, this way of writing is correct. Faced with signed int in Java will go wrong. The reason is that a signed int is not large enough to represent the result of subtracting two numbers of the same type. If i is a large enough positive integer and j is a negative integer with a large enough absolute value, then the result of ij will overflow and return a negative value, causing an error.
  
  
  [Map function]
  
  Map [interface] Maintains key-value associations, enabling you to look up values ​​by key
  HashMap* A hash table-based implementation that can be used to replace Hashtable.
  Elements are inserted in constant time; the performance can be adjusted by setting the capacity and load factor through the constructor.
  TreeMap is based on the implementation of red-black tree.
  The results are presented in sorted form (order determined by Comparable[interface] or Comparator[class]); the only Map with subMap()
  
  *Default best choice
  
  Map[interface]
  put(Object key, Object value)
  get(Object key )
  containsKey(Object key)
  containsValue(Object value)
  
  SortedMap [interface] (TreeMap is its only implementation)
  Comparator comparator();
  Object firstKey();
  Object lastKey();
  SortedMap subMap(fromKey, toKey);
  SortedMap headMap(toKey) ;
  SortedMap tailMap(fromKey);
  
  
  [container library: public function]
  
  Java 2 container library: java.util.Collections [class]
  (not to be confused with Collection!)
  
  enumeration(Collection) yields an old-style (Java 1.0/1.1) Enumeration
  max(Collection[, Comparator]) natural is used when no Comparator is specified comparison
  min(Collection[, Comparator])
  reverse() reverse order
  copy(List dest, List src)
  fill(List list, Object o) (only valid for List) Replace the original elements in the List, and copy the reference of o to the List At each position of
  nCopies(int n, Object o) returns a "size n, the content does not change" List, in which all references point to o
  
  to produce a read-only version:
  unmodifiableCollection(Collection c)
  unmodifiableList(List l)
  unmodifiableSet(Set s)
  unmodifiableMap(Map m)
  
  
  Thinking in Java Complementary container library: com.bruceeckel.util.Collection2 [class]
  
  fill(Collection, Generator, int) Use the automatic generator generator to add the specified number of elements to the container.
  
  In addition to the RandXXXGenerator [class] defined in com.bruceeckel.util.Arrays2 can continue to be used,
  (Boolean, Byte, Char, Short, Int, Long, Float, Double, String)
  also provides a new
  RandStringPairGenerator [class] for Map to generate a specified number of random string pairs (String pairs)
  StringPairGenerator [class] will give the given two-dimensional string array ( 2D String Arrays) into string pairs (String pairs)
  
  predefined generator objects:
  rsp RandStringPairGenerator object, generating 10 sets of String pairs
  geography StringPairGenerator object
  countries StringGenerator object
  capitals StringGenerator object
  
  [Java 1.0/1.1 old container]
  
  Vector Corresponding to the container of the old-style iterator Enumeration, "ArrayList with long and unusable function names"
  elements() returns Enumeration
  addElement()
  
  Enumeration [interface]
  boolean hasMoreElements()
  Object nextElement()
  
  Hashtable 类似HashMap
  
  Stack
  
  BitSet

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326945382&siteId=291194637