It form arrangement 4 (Collection)

Source: Internet
Author: User
Tags comparable

1. Introduce the structure of the Collection framework ..

I collected a reasonable answer from the Internet.

Collection)

1. The largest interfaces of the class set framework: Collection, MAP, iterator, and enumeration

2. Collection: stores the single value

  |-List: Repeated content is allowed and ordered.

      |-Arraylist: asynchronous processing, new operation class, non-thread security.

      |-Vector: synchronous processing, old operation class, thread security. Support enumeration output

   |-Set: duplicate content is not allowed and unordered. Duplicate by hashcoke () and equals () is serious.

      |-Hashset: unordered Storage

       |-Treeset: ordered storage and comparable sorting

3. MAP: stores a pair of values.

  |-Hashmap: New Class, asynchronous processing, non-thread security, null allowed

  |-Hashtable: The old class, synchronous processing, thread security, and null is not allowed

        |-Properties: attribute operation class

   |-Treemap: ordered by keys, specified by comparable

4. iterator:

  |-Iterative output, which depends on the iterator method in the collection interface, is a new output Standard

5. enumeration: Old output Standard

Excerpt from the book:

(1) There are two main types of Java container class libraries: Collection and map. The difference between the two is that the number of elements stored in each "slot" is different. The former has only one element, and the latter is a key-value pair.

(2) The container can only hold the object reference, instead of copying the object information to a certain position in the sequence. Once the object is placed in the container, the category information of the object is lost.

I have prepared a detailed explanation on the Internet, "// www.jb51.net/article/32152.htm" (HTTP)

2. What interfaces should be implemented for comparison in the Collection framework.

Two Comparison interfaces derived from sorting sets: comparable (in the Java. lang Package) and comparator (in the Java. util package)

1: Purpose:
If different objects of a class need to be compared, You need to implement these two interfaces. Define comparison rules based on business needs.

2: usage differences
(1) Comparable Interface
Is implemented by the comparison class. For example, if the defined student class needs to be compared, the student class needs to implement this interface and implement the comparato () method.

(2) comparator Interface
Customizes a comparator class to implement the compare of this interface. Then, when a set object is generated, a comparator object is used as a parameter to bind it to the set. For example:
Classaimplementscomparator {} // defines a comparator Class AA = newa (); // generates a comparator object
Setset = newtreeset (a); // when constructing a collection object, pass in the comparator

3. Differences between arraylist and vector.

(1) arraylist and vector functions are similar: In general, arraylist is the "streaking new version" of vector. Vector exists from Java 1.0 and arraylist exists from Java 1.2. Therefore, vector can be used in various minor versions such as javame and card, while arraylist cannot. Arraylist does not have the thread synchronization security, but it is fast, so it is called streaking. Vector provides thread security.
(2) An explanation that makes sense: // www.cnblogs.com/wanlipeng/archive/2010/10/21/1857791.html" (+ HTTP)

First, we can see that both of these classes implement the list interface, and the list interface has three implementation classes: arraylist, vector, and sorted list. List is used to store multiple elements, maintain the order of elements, and allow repeated elements. The differences between the three implementation classes are as follows:

(1) arraylist is the most common list implementation class, which is implemented through arrays internally. It allows fast Random Access to elements. The disadvantage of an array is that there cannot be an interval between each element. When the array size does not meet the requirements, you need to increase the storage capability. It is necessary to copy the data of an array to a new bucket. When an element is inserted or deleted from the middle of the arraylist, the array needs to be copied, moved, and the cost is relatively high. Therefore, it is suitable for random search and traversal, and is not suitable for insertion and deletion.

(2) Like arraylist, vector is also implemented through arrays. The difference is that it supports thread synchronization, that is, at a time point, only one thread can write vector, avoid inconsistency caused by simultaneous write of multiple threads, but it is costly to implement synchronization. Therefore, it is slower to access arraylist than to access arraylist.

(3) The shortlist uses a linked list structure to store data. It is suitable for dynamic insertion and deletion of data, and the random access and traversal speed is relatively slow. In addition, it provides methods not defined in the list interface, which are used to operate the header and table tail elements and can be used as stacks, queues, and bidirectional queues.

(4) During capacity expansion: the differences between arraylist and vector are as follows:

(1) When the memory of arraylist is insufficient, the default number is 50% + 1, and the vector is doubled by default.

(2) vector provides the indexof (OBJ, start) interface, which is not available in arraylist.

(3) vector is thread-safe, but vector is not used in most cases, because thread security requires greater system overhead.

4. Differences between hashmap and hashtable:

Hashmap is a lightweight Implementation of hashtable (non-thread-safe implementation). They all complete the map interface,

(1) The main difference is that hashmap allows null key values. Due to non-thread security, the efficiency may be higher than hashtable.

Hashmap allows null as the key or value of an entry, whereas hashtable does not.

(Hashmap is not classified or sorted. It allows a null key and multiple null values. hashtable is similar to hashmap, but does not allow null keys and null values)
(2) hashtable inherits from the dictionary class, while hashmap is an implementation of the map interface introduced by java1.2. Hashmap removes the contains method of hashtable and changes it to containsvalue (if the corresponding map has multiple keys corresponding to the specified value, the returned value is true) and containskey (if you have specified key, if a map has a corresponding ing, the return value is true ). Because the contains (check whether there is a corresponding value and key in the hash table) method is easy to cause misunderstanding.
(3) the biggest difference is that the hashtable method is synchronize, but hashmap is not. When multiple threads access hashtable, they do not need to implement synchronization for their own methods, hashmap must provide external synchronization (collections. synchronizedmap ). The hash/rehash algorithms used by hashtable and hashmap are roughly the same, so there is no big difference in performance.
5. Remove repeated elements from a vector set.

(1) Method 1: insert restrictions, and repeated values cannot be inserted:

import java.util.Vector;public class DeleteVector { public static void main(String []args){  Vector<String> vector = new Vector<String>();  addObject(vector, "aa");  addObject(vector, "bb");  addObject(vector, "cc");  addObject(vector, "aa");  addObject(vector, "dd");  addObject(vector, "bb");  System.out.println(vector); }  public static void addObject(Vector<String> c,String str){  if(!c.contains(str)){   c.add(str);  } }}
(2) Use hashset for modification after insertion:

Import Java. util. hashset; import Java. util. vector; public class deletevector {public static void main (string [] ARGs) {vector <string> vector = new vector <string> (); vector. add ("1"); vector. add ("2"); vector. add ("2"); vector. add ("3"); vector. add ("4"); vector. add ("5"); vector. add ("4"); vector. add ("5"); // hashset is an implementation class of set. objects are used as elements. Repeated objects are rejected and null elements are allowed. Hashset <string> HS = new hashset <string> (vector); system. Out. println (HS );}}
6. Differences between collection and collections

(1) collection is an interface under java. util. It is the parent interface of various collection structures.

(2) collections is a Java. util class that contains various static methods related to set operations.

The Root Interface in the collection hierarchy. Collection indicates a group of objects. These objects are also called collection objects.Element. Some collections allow repeated elements, while others do not. Some collections are ordered, while others are disordered. JDK does not provide anyDirectImplementation: it provides more specific sub-interfaces (suchSetAndList. This interface is usually used to pass collections and operate these collections where the maximum universality is required.

Collections this class is composed of static methods that operate on the collection or return the collection. It contains the polymorphism algorithm operated on the collection, that is, the package, which returns the new collection supported by the specified collection and a few other contents. If the collection or Class Object provided by methods of this class is null, these methods will throw Nullpointerexception.
7. The two objects have the same value (X. Equals (y) = true), but different hash codes are available, right?

According to API rules: equal and hashcode must be equal; hashcode and equal must also be different; but it can also be different from this agreement.

This may happen if you overwrite the equals method without overwriting the hashcode method.

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.