`

<Java.JavaEE面试整理>(12) 对Collections FrameWork的理解(二)

阅读更多

<Java.JavaEE面试整理>(12) 对Collections FrameWork的理解(二)

 

++++++++++++++++++++++++++++++++++++++++++

真是抱歉,没有把那个<Java.JavaEE面试整理>(11) 对Collections FrameWork的理解(一)(以下简称"理解一")这样核心部分的内容放到频道首页来让更多的朋友看到,而只是把个英文原文放到这里.

应该说"理解一"更为值得关注.

为了答谢大家的支持,鄙人会将这段时间对这个Collection Framework的理解心得整理出来与大家交流.

++++++++++++++++++++++++++++++++++++++++++

 

这个话题所涉及到的问题很重要,先把英文的原话写在这时在,等过些天有时间了,再结合iterator设计模式好好研究这个问题.

 

Q. 为什么用Itetator时,会抛出ConcurrentModificationException这样的异常呢?
    Why do you get a ConcurrentModificationException when using an iterator? CO

    
Problem: The java.util Collection classes are fail-fast, which means that if one thread changes a collection while another
thread is traversing it through with an iterator the iterator.hasNext() or iterator.next() call will throw
ConcurrentModificationException.  Even the synchronized collection wrapper classes  SynchronizedMap and
SynchronizedList are only conditionally thread-safe, which means all individual operations are thread-safe but compound
operations where flow of control depends on the results of previous operations may be subject to threading issues.  
 
Collection<String> myCollection = new ArrayList<String>(10);
 
myCollection.add("123");
myCollection.add("456");
myCollection.add("789");
 
for (Iterator it = myCollection.iterator(); it.hasNext();) {
    String myObject = (String)it.next();
    System.out.println(myObject);  
    if (someConditionIsTrue) {
        myCollection.remove(myObject);  //can throw ConcurrentModificationException in single as  
                                        //well as multi-thread access situations.     
    }   
}  
 
Solutions 1-3:  for multi-thread access situation:
 
Solution 1: You can convert your list to an array with list.toArray() and iterate on the array. This approach is not
recommended if the list is large.
 
Solution 2: You can lock the entire list while iterating by wrapping your code within a synchronized block. This approach
adversely affects scalability of your application if it is highly concurrent.  
 
Solution 3: If you are using JDK 1.5 then you can use the ConcurrentHashMap and CopyOnWriteArrayList  classes,
which provide much better scalability  and the iterator returned by ConcurrentHashMap.iterator() will not throw
ConcurrentModificationException while preserving thread-safety. (这个倒是不错!)
 
Solution 4:  for single-thread access situation:
 
Use:             
         it.remove();       // removes the current object via the Iterator “it” which has a reference to  
                       // your underlying collection “myCollection”. Also can use solutions 1-3.   
                       
Avoid:    
        myCollection.remove(myObject); // avoid by-passing the Iterator. When it.next() is called, can throw  the exception      
                                                                           // ConcurrentModificationException
 
 
Note: If you had used any Object to Relational (OR) mapping frameworks like Hibernate, you may have encountered this
exception “ConcurrentModificationException”  when you tried to remove an object from a collection such as a java.util Set
with the intention of deleting that object from the underlying database. This exception is not caused by Hibernate but
rather caused by your java.util.Iterator (i.e. due to your it.next() call). You can use one of the solutions given above.      
 
 
Q. What is a list iterator?  
 
The java.util.ListIterator is an iterator for lists that allows the programmer to traverse the list in either direction (i.e.
forward and or backward) and modify the list during iteration.
 

What are the benefits of the Java Collections Framework? Collections framework provides flexibility, performance,
and robustness.   
 
    .  Polymorphic algorithms – sorting, shuffling, reversing, binary search etc.  
    .  Set algebra - such as finding subsets, intersections, and unions between objects.  
    .  Performance - collections have much better performance compared to the older Vector and Hashtable classes with
the elimination of synchronization overheads.
    .  Thread-safety - when synchronization is required, wrapper  implementations are provided for temporarily
synchronizing existing collection objects. For J2SE 5.0 use java.util.concurrent package.
    .  Immutability - when immutability is required wrapper implementations are provided for making a collection
immutable.  
    .  Extensibility - interfaces and abstract classes provide an excellent starting point for adding functionality and
features to create specialized object collections.  
 
 
Q. What are static factory methods? CO
 
Some of the above mentioned features like searching, sorting, shuffling, immutability etc are achieved with
java.util.Collections class and java.util.Arrays utility classes.  The great majority of these implementations are provided
via  static factory methods in a single, non-instantiable (i.e. private constrctor) class. Speaking of  static factory
methods, they are an alternative to creating objects through constructors. Unlike constructors, static factory methods are
not required to create a new object (i.e. a duplicate object) each time they are invoked (e.g. immutable instances can be
cached) and also they have a more meaningful names like valueOf, instanceOf, asList etc. For example:
 
Instead of:
String[] myArray = {"Java", "J2EE", "XML", "JNDI"};
for (int i = 0; i < myArray.length; i++) {
     System.out.println(myArray[i]);
}
 
You can use:
String[] myArray = {"Java", "J2EE", "XML", "JNDI"};
System.out.println(Arrays.asList(myArray)); //factory method Arrays.asList(…)
 
 
For example:  The following static factory method (an alternative to a constructor) example converts a boolean primitive
value to a Boolean wrapper object.

public static Boolean valueOf(boolean b) {
    return (b ? Boolean.TRUE : Boolean.FALSE)   
}

  • 描述: Java colleciont framework
  • 大小: 1.6 MB
分享到:
评论

相关推荐

    160G!全新升级版 JAVAEE云计算全栈就业班课程 完美试炼JAVAEE企业级云计算应用

    ├&lt;阶段1 java语言基础&gt; │ ├&lt;1-1-Java基础语法&gt; │ ├&lt;1-2 -面向对象和封装&gt; │ └&lt;1-3-Java语言高级&gt; ├&lt;阶段2 JavaWeb·&gt; │ ├&lt;01 HTML和CSS&gt; │ ├&lt;02 JavaScript&gt; │ ├&lt;03 BootStrap&gt; │ ├&lt;04 XML&gt; │ ├...

    IntelliJ IDEA 12创建Maven管理的JavaEE Web项目.pdf

    ### IntelliJ IDEA 12 创建 Maven 管理的 JavaEE Web 项目 #### 一、简介 本篇文章将详细介绍如何使用 IntelliJ IDEA 12 来创建一个由 Maven 管理的 JavaEE Web 项目。这不仅适用于初学者了解整个项目创建流程,...

    cactus.core.framework.uberjar.javaEE.14-1.8.1.jar

    一个变态的jar包,按照我当前的工程环境,应该在weblogic8底下,但就是找不到,所以只好在网上找了,记录在这里。 包含的类: org.apache.cactus.server.ServletTestRedirector

    框架ssm整合

    &lt;groupId&gt;org.springframework&lt;/groupId&gt; &lt;artifactId&gt;spring-context&lt;/artifactId&gt; &lt;version&gt;5.3.19&lt;/version&gt; &lt;/dependency&gt; &lt;!-- SpringMVC --&gt; &lt;dependency&gt; &lt;groupId&gt;org.springframework&lt;/groupId&gt; ...

    IntelliJ IDEA maven 构建简单springmvc项目(图文教程)

    &lt;groupId&gt;org.springframework&lt;/groupId&gt; &lt;artifactId&gt;spring-core&lt;/artifactId&gt; &lt;version&gt;4.3.5.RELEASE&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.springframework&lt;/groupId&gt; &lt;artifactId&gt;spring...

    spring_MVC源码

    02.&lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee ...

    生活轨迹SSH服务端

    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;!-- 默认找的文件名 --&gt; &lt;/listener&gt; &lt;!-- Context Configuration locations for Spring XML files --&gt; &lt;context...

    springmvc搭建demo

    &lt;groupId&gt;org.springframework&lt;/groupId&gt; &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt; &lt;version&gt;5.3.23&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.springframework&lt;/groupId&gt; &lt;artifactId&gt;spring-jdbc...

    cxf 集成spring例子java代码

    &lt;groupId&gt;org.springframework&lt;/groupId&gt; &lt;artifactId&gt;spring-context&lt;/artifactId&gt; &lt;version&gt;5.3.9&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.springframework&lt;/groupId&gt; &lt;artifactId&gt;spring-web...

    javaee面试题集锦 面试题 java

    JavaEE是企业级应用开发的重要框架,它基于Java平台,为构建分布式、多层的Web应用程序提供了全面的解决方案。这份“javaee面试题集锦”涵盖了广泛的JavaEE相关知识点,对于求职者或开发者来说,是提升技能和准备...

    Java和JavaEE技术面试题

    Java和JavaEE技术面试题是求职者准备面试的重要参考资料,涵盖了广泛的Java基础知识、JSP、Servlet、EJB、数据库、设计模式等多个领域。以下是对这些知识点的详细解释: 1. **面向对象的特征**:封装、继承、多态是...

    最简化velocity的web工程

    &lt;servlet-name&gt;velocity&lt;/servlet-name&gt; &lt;servlet-class&gt;org.apache.velocity.tools.view.VelocityViewServlet&lt;/servlet-class&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;velocity&lt;/servlet-name&gt; ...

    SpringMVC环境搭建

    &lt;groupId&gt;org.springframework&lt;/groupId&gt; &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt; &lt;version&gt;5.3.23&lt;/version&gt; &lt;/dependency&gt; &lt;!-- Servlet API --&gt; &lt;dependency&gt; &lt;groupId&gt;javax.servlet&lt;/groupId&gt; ...

    从java走向javaEE

    《从Java走向JavaEE》这本书的附带代码资源旨在帮助初学者逐步理解并掌握Java企业级应用开发。Java和JavaEE是两个不同的概念,Java是一种广泛使用的编程语言,而JavaEE(Java Platform, Enterprise Edition)是Java...

    JavaEE主流开源框架-Struts部分rmvb格式.zip

    JavaEE主流开源框架-Struts部分rmvb格式. JavaEE主流开源框架-Struts部分rmvb格式. JavaEE主流开源框架-Struts部分rmvb格式. JavaEE主流开源框架-Struts部分rmvb格式. JavaEE主流开源框架-Struts部分rmvb格式. ...

    最全面的JAVA和JAVAEE面试题

    【描述】:这是一份精心整理的JAVA和JAVAEE面试题集锦,包含了我在多次面试过程中遇到的各类问题,旨在为准备面试的朋友提供全面且实用的参考资料。这份文档将帮助你深入了解Java编程语言以及Java Enterprise ...

    Java最简单的maven版helloword

    &lt;name&gt;Java Maven Hello World&lt;/name&gt; &lt;dependencies&gt; &lt;!-- 添加必要的依赖,例如Servlet API --&gt; &lt;dependency&gt; &lt;groupId&gt;javax.servlet&lt;/groupId&gt; &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt; &lt;version&gt;...

    JavaEE Struts2整合Spring的小案例

    &lt;groupId&gt;org.springframework&lt;/groupId&gt; &lt;artifactId&gt;spring-context&lt;/artifactId&gt; &lt;version&gt;5.x.x.RELEASE&lt;/version&gt; &lt;/dependency&gt; &lt;!-- 其他相关依赖 --&gt; &lt;/dependencies&gt; ``` 接下来,我们需要配置Struts...

    javaEE面试宝典自整理

    JavaEE面试宝典是每一位Java开发者在求职过程中必备的参考资料,涵盖了从基础知识到高级技术的全方位面试准备。这里,我们将深入探讨其中的关键知识点,帮助你更好地理解和掌握。 首先,我们从基础开始。Java语言的...

    spring MVC框架搭建

    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;servlet&gt; &lt;servlet-name&gt;spring&lt;/servlet-name&gt; &lt;servlet-class&gt;org.springframework.web.servlet....

Global site tag (gtag.js) - Google Analytics