Java Interview 02| Java Collection

Source: Internet
Author: User

The collections that are generally exposed in Java are as follows:

About concurrent collections in Java are:

(1) Councurrenthashmap

(2) Copyonwritearraylist

(3) Linkedblockingqueue

(4) Arrayblockingqueue

The application of these scenarios and their implementation principles must be mastered.

1, the cause of the deadlock of the hash

Reference: HASHMAP Deadlock Analysis http://github.thinkingbar.com/hashmap-infinite-loop/

2, about the CONCURRENTHASHMAP related issues

The implementation of the Concurrenthashmap 1.7 and 1.8 is very different, you can refer to the article:

(1) Talk about the different realization of ConcurrentHashMap1.7 and 1.8 http://www.jianshu.com/p/e694f1e868ec

(2) https://zhuanlan.zhihu.com/p/21673805

Here are a few questions that you must know about Concurrenthashmap:

(1) Concurrenthashmap Lock segmentation Technology.

(2) Concurrenthashmap read whether to lock, why.

(3) The Concurrenthashmap iterator is a strongly consistent iterator or a weakly consistent iterator.

The iterator iterates through the underlying array. During traversal, the iterator does not throw an concurrentmodificationexception exception if the contents of the array that have been traversed have changed. If the content on an array that is not traversed has changed, it is possible to reflect the iteration process. This is the weak and consistent representation of the Concurrenthashmap iterator.

The structure of the concurrenthashmap is probably shown below.

3, the application of Linkedhashmap

Linkedhashmap maintains a double-link list that runs on all items. This list of links defines the order of the iterations, which can be either the Insertion Order (Insert-order) or the access order, where the default iteration access order is the insertion order, in which the elements can be traversed in the order in which they were inserted. Based on the features of the Linkedhashmap access order, it is possible to construct an LRU (Least recently used) with minimal use of a simple cache recently. There are also some open source cache products such as Ehcache's elimination strategy (LRU) that is extended on the linkedhashmap.

public class lrucache<k, v> extends Linkedhashmap<k, v> {              /** maximum capacity */              private int maxcapacity;                       Public LruCache (int maxcapacity) {                  super (0.75f, true);                  this.maxcapacity = maxcapacity;              }                       public int getmaxcapacity () {                  return this.maxcapacity;              }                       public void setmaxcapacity (int maxcapacity) {                  this.maxcapacity = maxcapacity;              }                       /**              * Returns True when the number of elements in the list is greater than the specified maximum capacity, and deletes the oldest element.              */              @Override              protected Boolean removeeldestentry (Java.util.map.entry<k, v> eldest) {                  if ( Super.size () > Maxcapacity) {                      return true;                  }                  return false;              }          }  

  

public class Lrucachetest {public                       static void Main (string[] args) {                  lrucache<string, object> cache = new Lr Ucache<string, object> (ten);                           for (int i = 1; i <=; i++) {                      cache.put (i + "", I);                  }                           The element                  Cache.get ("ten") of the specified key is accessed at this time;                           iterator<entry<string, object>> Iterator = Cache.entryset (). Iterator ();                  for (; Iterator.hasnext ();) {                      entry<string, object> Entry = Iterator.next ();                      System.out.println ("key=" + entry.getkey () + ", value=" + entry.getvalue ());}}}            

The output is as follows:

key=7,value=7  key=8,value=8  key=9,value=9  key=11,value=11  key=12,value=12  key=13,value=13  key=14,value=14  key=15,value=15  key=10,value=10  

 

Java Interview 02| Java Collection

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.