Common collections in Java

Source: Internet
Author: User

There are sequences Allow elements to repeat no
Collection Whether Is
List Is Is
Set Abstractset Whether Whether
HashSet
TreeSet Yes (sort by binary tree)
Map Abstractmap Whether

Use Key-value to map and store data,

Key must be unique,value can be repeated

HashMap
TreeMap Yes (sort by binary tree)

Several interview FAQs:
What is the difference between 1.q:arraylist and vectors? What's the difference between HashMap and Hashtable?
A:vector and Hashtable are thread-synchronized (synchronized). In performance, ArrayList and HashMap are better than vectors and Hashtable respectively.

2.Q: Roughly explaining the architecture of the Java collection
A:list, set, and map are the main three interfaces in this set system.
Where list and set inherit from the collection interface.
Set does not allow elements to be duplicated. HashSet and TreeSet are the two main implementation classes.
The list is ordered and allows elements to be duplicated. ArrayList, LinkedList, and vectors are the three main implementation classes.
The map also belongs to the collection system, but differs from the collection interface. A map is a collection of key pairs of value, where the key column is a collection. Key cannot be duplicated, but value can be repeated. HashMap, TreeMap, and Hashtable are the three main implementation classes.
The SortedSet and SortedMap interfaces sort the elements by the specified rules, sortedmap the key column.

3.q:comparable and Comparator differences
A: When you call the Java.util.Collections.sort list method to sort, the object in the list must implement the comparable interface.
Java.util.Collections.sort (List list,comparator c), you can temporarily declare a Comparator to implement sorting.
Collections.sort (ImageList, New Comparator () {
public int Compare (object A, object B) {
int ordera = Integer.parseint ((Image) a). Getsequence ());
int orderb = Integer.parseint ((Image) b). Getsequence ());
return ordera-orderb;
}
});
If you need to change the sorting order
Change to return Orderb-ordera.

4.Q: Brief description of Equals () and Hashcode ()
A:... I don't know. Tell

public interface
Collection
Extends Iterable


public interface
List
Extends Collection


Public abstract class
Abstractlist
Extends Abstractcollection
Implements List


public class
Vector
Extends Abstractlist
Implements List,
Randomaccess,
Java.lang.Cloneable,
Java.io.Serializable
based on Array
it's "sychronized."


public class
ArrayList
Extendsabstractlist
ImplementsList,
Randomaccess,
Cloneable,
Java.io.Serializable
based on Array
the ArrayList is non-synchronous. So it's better than vector in performance.


public class
LinkedList
Extends Abstractsequentiallist
Implements List,
Queue,
Cloneable,
Java.io.Serializable
Not based on array

Array-based list (vector,arraylist) is suitable for querying, while LinkedList (linked list) is suitable for adding, deleting operations




The list is basically based on an array. But set is implemented on the basis of HashMap, which is the fundamental difference between set and list.
Public abstract class Abstractset
Extends Abstractcollection
Implements Set


public class HashSet
Extends Abstractset
Implements Set, Cloneable, java.io.Serializable
The hashset is stored in the HashMap key as the corresponding storage of the set


public class Linkedhashset
Extends HashSet
Implements Set, Cloneable, java.io.Serializable


public class TreeSet
Extends Abstractset
Implements SortedSet, Cloneable, java.io.Serializable
It's done by SortedMap .




public interface map<k,v>


Public abstract class Abstractmap<k,v>
Implements Map<k,v>


public class Hashmap<k,v>
Extends abstractmap<k,v>
Implements Map<k,v>, Cloneable, Serializable


public class Treemap<k,v>
Extends abstractmap<k,v>
Implements Sortedmap<k,v>, Cloneable, java.io.Serializable

HashMap quickly find its content through hashcode, and all elements in TreeMap remain in a fixed order, and if you need to get an ordered result you should use TREEMAP (the order of the elements in HashMap is not fixed)

Common collections in Java

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.