Containers in Depth


Full container taxonomy

  • You can usually ignore any class that begins with "Abstract."

Filling containers

  • This fill( ) just duplicates a single object reference throughout the container. In addition, it only works for List objects.
  • The fill( ) method is made even less useful by the fact that it can only replace elements that are already in the List and will not add new elements.

A Generator solution

  • Virtually all Collection subtypes have a constructor that takes another Collection object, from which it can fill the new container.
  • A LinkedHashSet maintains a linked list holding the insertion order.

Using Abstract classes

  • Each java.util container has its own Abstract class that provides a partial implementation of that container, so all you must do is implement the necessary methods in order to produce the desired container.
  • You use a flyweight when the ordinary solution requires too many objects, or when producing normal objects takes up too much space.

Collection functionality

  • There’s no get( ) method for random-access element selection.
  • Thus, if you want to examine the elements of a Collection, you must use an iterator.

Optional operations

  • The implementing class is not required to provide functioning definitions for these methods.
  • An "optional" operation says that calling some methods will nor perform meaningful behavior.
  • If an operation is optional, the compiler still restricts you to calling only the methods in that interface.
  • Unsupported operations are a special case that can be delayed until necessary.
  • The design does provide a "back door" if you want to create a new Collection without providing meaningful definitions for all the methods in the Collection interface, and yet still fit it into the existing library.

Unsupported operations

  • Because Arrays.asList( ) produces a List that is backed by a fixed-size array, it makes sense that the only supported operations are the ones that don’t change the size of the array.
  • Arrays.asList( ) returns a fixed-sized List, whereas Collections.unmodifiableList( ) produces a list that cannot be changed.

Sets and storage order

  • A Set needs a way to maintain storage order.
  • Different Set implementations not only have different behaviors, they have different requirements for the type of object that you can put into a particular Set.
  • In the absence of other constraints, HashSet should be your default choice because it is optimized for speed.
  • The HashSet keeps the elements in some mysterious order.
  • The LinkedHashSet keeps the elements in the order in which they were inserted.
  • The TreeSet maintains the elements in sorted order.
  • The only reliable way to ensure the correctness of such a program is to incorporate unit tests into your build system.

SortedSet

  • Note that SortedSet means "sorted according to the comparison function of the object," not "insertion order."

Queues

  • With the exception of the priority queues, a Queue will produce elements in exactly the same order as they are placed in the Queue.
  • There are methods in LinkedList that support deque operations, but there is no explicit interface for a deque in the Java standard libraries.
  • You can create a Deque class using composition, and simply expose the relevant methods from LinkedList.

Understanding Maps

  • The standard Java library contains different basic implementations of Maps: HashMap, TreeMap, LinkedHashMap, WeakHashMap, ConcurrentHashMap, and IdentityHashMap.
  • The number of implementations of the Map interface should tell you something about the importance of this tool.
  • The keys are guaranteed to be in sorted order in SortedMap.
  • A LinkedHashMap can be configured in the constructor to use a leastrecently- used (LRU) algorithm based on accesses, so elements that haven’t been accessed (and thus are candidates for removal) appear at the front of the list.

Performance

  • Instead of a slow search for the key, it uses a special value called a hash code.
  • In the absence of other constraints, HashMap should be your default choice because it is optimized for speed.
  • Note that keys must be unique, but values may contain duplicates.

Hashing and hash codes

  • It is Object’s hashCode( ) method that is used to generate the hash code for each object. By default this just uses the address of its object.
  • equals( ) is used by the HashMap when trying to determine if your key is equal to any of the keys in the table.
  • The default Object.equals( ) simply compares object addresses.
  • To use your own classes as keys in a HashMap, you must override both hashCode( ) and equals( ).
  • The hashCode( ) is not required to return a unique identifier.

Hashing for speed

  • One of the solutions to the problem is to keep the keys sorted and then use Collections.binarySearch( ) to perform the lookup.
  • From the key object, a number will be derived that will index into the array.
  • To solve the problem of the fixed-size array, more than one key may produce the same index.
  • It doesn’t matter how big the array is; any key object’s hash code will land somewhere in that array.
  • If you could guarantee that there were no collisions (which is possible if you have a fixed number of values), then you’d have a perfect hashing junction, but that’s a special case.
  • Instead of searching through the entire list, you quickly jump to a slot where you only have to compare a few entries to find the value.

Overriding hashCode()

  • You don’t control the creation of the actual value that’s used to index into the array of buckets.
  • Regardless of when hashCode( ) is called, it produces the same value for a particular object every time it is called.
  • You’ll want to use information in the object that identifies the object in a meaningful way.
  • It makes sense that the hashCode( ) produced by two separate instances of the String "hello" should be identical.
  • For a hashCode( ) to be effective, it must be fast and it must be meaningful; that is, it must generate a value based on the contents of the object.
  • A good hashCode( ) should result in an even distribution of values.
  • Writing a proper hashCode( ) and equals( ) for a new class can be tricky.

Choosing an implementation

  • The distinction between containers often comes down to what they are "backed by"—that is, the data structures that physically implement the desired interface.
  • You choose the implementation based on the behavior you need.
  • One way to look at the differences between container implementations is with a performance test.

Choosing between Lists

  • Clearly, linked lists are not a good choice if you will be performing many random accesses.
  • An ArrayList must create space and copy all its references forward during an insertion. This becomes expensive as the ArrayList gets bigger.
  • The output that the cost of insertion and removal in a LinkedList is quite cheap and doesn’t vary with the list size, but with an ArrayList, insertions especially are very expensive, and the cost increases with list size.
  • The best approach is probably to choose an ArrayList as your default and to change to a LinkedList if you need its extra functionality or you discover performance problems due to many insertions and removals from the middle of the list.
  • If you are working with a fixed-sized group of elements, either use a List backed by an array (as produced by Arrays.asList( )), or if necessary, an actual array.

Microbenchmarking dangers

  • When writing so-called microbenchmarks, you must be careful not to assume too much, and to narrow your tests so that as much as possible they are only timing the items of interest.
  • You should not be so concerned with absolute numbers as with the performance comparisons between one type of container and another.

Choosing between Sets

  • The performance of HashSet is generally superior to TreeSet, but especially when adding elements and looking them up, which are the two most important operations.
  • TreeSet exists because it maintains its elements in sorted order, so you use it only when you need a sorted Set.

Choosing between Maps

  • Insertions for all the Map implementations except for IdentityHashMap get significantly slower as the size of the Map gets large.
  • lookup is much cheaper than insertion.
  • A TreeMap is a way to create an ordered list.
  • When you’re using a Map, your first choice should be HashMap, and only if you need a constantly sorted Map will you need TreeMap.

HashMap performance factors

  • HashMap and HashSet have constructors that allow you to specify the load factor.
  • When this load factor is reached, the container will automatically increase the capacity (the number of buckets) by roughly doubling it and will redistribute the existing objects into the new set of buckets (this is called rehashing).
  • The default load factor used by HashMap is 0.75.
  • A higher load factor decreases the space required by the table but increases the lookup cost.

Utilities

  • Use a ListIterator to trim off the last elements.
  • If you sort using a Comparator, you must binarySearch( ) using the same Comparator.
  • The Collections class allows you to do this by passing the original container into a method that hands back a read-only version.
  • You must fill the container with meaningful data before you make it read-only.
  • Once it is loaded, the best approach is to replace the existing reference with the reference that is produced by the "unmodifiable" call.
  • The Collections class contains a way to automatically synchronize an entire container.
  • It is best to immediately pass the new container through the appropriate "synchronized" method.
  • The Java containers library uses a fail-fast mechanism that looks for any changes to the container other than the ones your process is personally responsible for.

Holding references

  • There are three classes inherited from the abstract class Reference: SoftReference, WeakReference, and PhantomReference.
  • You have an ordinary reference on the stack that goes right to the object, but you might also have a reference to an object that has a reference to the object in question.
  • If an object isn’t reachable, there’s no way for your program to use it, so it’s safe to garbage collect that object.
  • If the garbage collector discovers that an object is reachable through an ordinary reference, it will not release that object.
  • In the order of SoftReference, WeakReference, and PhantomReference, each one is "weaker" than the last and corresponds to a different level of reachability.

The WeakHashMap

  • In such a mapping, you are saving storage by creating only one instance of a particular value. When the program needs that value, it looks up the existing object in the mapping and uses that.

Java 1.0/1.1 containers

  • Although you should never use the old containers when writing new code, you’ll still need to be aware of them.

Vector & Enumeration

  • Even though you should always use Iterator when you can in your own code, you must be prepared for libraries that want to hand you an Enumeration.

HashTable

  • There’s no reason to use Hashtable instead of HashMap in new code.

Stack

  • It has all of the characteristics and behaviors of a Vector plus some extra Stack behaviors.
  • By virtue of inheritance, a Stack is a Vector. Thus, all operations that can be performed on a Vector can also be performed on a Stack.

BitSet

  • You’re better off creating your own class, or just an array, to hold your flags if size is an issue.
  • An EnumSet is usually a better choice than a BitSet if you have a fixed set of flags that you can name.

Thinking in Java——笔记(17)的更多相关文章

  1. Java笔记17:导出可执行jar包

    这里介绍Java程序的两种导出可执行jar包的方式,一种是图形界面打包,另一种是命令行打包. 一.图形界面打包 1 建立名为JarDemo的JavaProject,在src目录下建立com.abc的P ...

  2. JAVA自学笔记17

    JAVA自学笔记17 1.Map接口 1)概述 将键映射到值的对象,一个映射不能包含重复的键,每个键最多只能映射到一个值.可以存储键值对的元素 2)与Collection接口的不同: ①Map是双列的 ...

  3. golang学习笔记17 爬虫技术路线图,python,java,nodejs,go语言,scrapy主流框架介绍

    golang学习笔记17 爬虫技术路线图,python,java,nodejs,go语言,scrapy主流框架介绍 go语言爬虫框架:gocolly/colly,goquery,colly,chrom ...

  4. java笔记00-目录

    --2013年7月26日17:49:59 学习java已久,趁最近有空,写一个总结: java笔记01-反射:

  5. SQL反模式学习笔记17 全文搜索

    目标:全文搜索 使用SQL搜索关键字,同时保证快速和精确,依旧是相当地困难. SQL的一个基本原理(以及SQL所继承的关系原理)就是一列中的单个数据是原子性的. 反模式:模式匹配 使用Like 或者正 ...

  6. java笔记整理

    Java 笔记整理 包含内容     Unix Java 基础, 数据库(Oracle jdbc Hibernate pl/sql), web, JSP, Struts, Ajax Spring, E ...

  7. Effective Java笔记一 创建和销毁对象

    Effective Java笔记一 创建和销毁对象 第1条 考虑用静态工厂方法代替构造器 第2条 遇到多个构造器参数时要考虑用构建器 第3条 用私有构造器或者枚举类型强化Singleton属性 第4条 ...

  8. Ext.Net学习笔记17:Ext.Net GridPanel Selection

    Ext.Net学习笔记17:Ext.Net GridPanel Selection 接下来是Ext.Net的GridPanel的另外一个功能:选择. 我们在GridPanel最开始的用法中已经见识过如 ...

  9. 转 Java笔记:Java内存模型

    Java笔记:Java内存模型 2014.04.09 | Comments 1. 基本概念 <深入理解Java内存模型>详细讲解了java的内存模型,这里对其中的一些基本概念做个简单的笔记 ...

随机推荐

  1. IOS单元测试,OCMock

    1.环境搭建 使用官方推荐,手动搭建.. 1.下载所需文件 http://ocmock.org/download/ 根据自己的需要下载不同的版本. 打开iOS library文件夹,里面就有需要用到的 ...

  2. python接收图片变成缩略图

    python图像处理库:Pillow初级教程 Image类 Pillow中最重要的类就是Image,该类存在于同名的模块中.可以通过以下几种方式实例化:从文件中读取图片,处理其他图片得到,或者直接创建 ...

  3. 二叉树建立,遍历和二叉排序树的判断【c++】

    // test.cpp : Defines the entry point for the console application. // #include "stdafx.h" ...

  4. java21

    1:字符流(掌握) (1)字节流操作中文数据不是特别的方便,所以就出现了转换流. 转换流的作用就是把字节流转换字符流来使用. (2)转换流其实是一个字符流 字符流 = 字节流 + 编码表 (3)编码表 ...

  5. 为什么operator>>(istream&, string&)能够安全地读入长度未知的字符串?

    一般而言,实现"读入用户输入的字符串",程序中自然不能对用户输入的长度有所限定.这在C++中很容易实现,而在C中确没那么容易. 这一疑问,我在刚学C++的时候也在脑中闪现过:不过很 ...

  6. Laravel 创建数据库

    1.根目录输入 php artisan migrate 2.创建表 php artisan migrate:make create_authors_table --table authors --cr ...

  7. python_配置

    代码示例:https://pan.baidu.com/s/1pLjLPSv 1.自动补全功能 许多人都知道 iPython 有很好的自动补全能力,但是就未必知道 python 也同样可以 Tab 键补 ...

  8. php面试题2

    php面试题及答案(原创)收藏 基础题: 1.表单中 get与post提交方法的区别? 答:get是发送请求HTTP协议通过url参数传递进行接收,而post是实体数据,可以通过表单提交大量信息. 2 ...

  9. 算法系列:FFT 002

    转载自http://blog.jobbole.com/58246/ 快速傅里叶变换(Fast Fourier Transform)是信号处理与数据分析领域里最重要的算法之一.没有正规计算机科学课程背景 ...

  10. 铁区MES部分页面展示

    激活码: 76231722-2e7554593-b750-07e2f4844531 TIP: 若您激活不成功,可能是因为您所安装的软件版本较低,请尝试以下激活码 激活码: RXWY-A25421-K5 ...