Holding Your Objects

# Holding your objects

Java provides a number of ways to hold objects :

>1. An array associates numberical indexes to objects. It holds objects
of a known type so that you don‘t have to cast the result when you‘re looking up
an object. It can be multidimensional, and it can hold primitives. However,
**its size cannot be changed once you create it**.

>2. A **Collection** holds single elements, and a **Map** holds
associated pairs. With Java generics, you specify the type of object to be held
in the containers, so you can‘t put the wrong type into a container and you
don‘t have to cast elements when you fetch them out of a container. Both
**Collections** and **Maps** automatically resize themselves as you add more
elements. **A container won‘t hold primitives, but autoboxing takes care of
translating primitives back and forth to the wrapper types held in the
container**.

>3. Like an array, a **List** also associates numerical indexes to
objects - thus, **arrays and Lists are ordered containers**.

>4. Use an **ArrayList** if you‘re doing a lot of random accesses, but a
**LinkedList** if you will be doing a lot of insertions and removals in the
middle of the list.

>5. The behavior of **Queues** and **Stacks** is provided via the
**LinkedList**.

>6. A **Map** is a way to accociate not integral values, but *objects*
with other objects. **HashMaps** are designed for rapid access, whereas a
**TreeMap** keeps its keys in sorted order, and thus is not as fast as a
HashMap. A **LinkedHashMap** keeps its elements in insertion order, but provides
repid access with hashing.

>7. A **Set** only accepts one of each type of object. **HashSets**
provide maximally fast lookups, whereas **TreeSets** keep the elements in sorted
order. **LinkedHashSets** keep elements in insertion order.

>8. There‘s no need to use the legacy classes **Vector**, **Hashtable**,
and **Stack** in new code.

Here‘s a simpleified diagram of the Java containers.

![Alt Java
Containers](http://knerd.qiniudn.com/thinking_in_java_SimpleContainerTaxonomy.png)

You can see that there are really only 4 basic container components -
**Map**, **List**, **Set**, and **Queue** - and only 2 or 3 implementations of
each one.

来自为知笔记(Wiz)

Holding Your Objects,布布扣,bubuko.com

时间: 2024-10-16 15:07:24

Holding Your Objects的相关文章

TIJ英文原版书籍阅读之旅——Chapter Eleven:Holding Your Objects

Java Provides a number of ways to hold objects: An array associates numerical indexes to objects. It holds objects of a known type so that you don't have to cast the result when you're looking up an object. It can be multidimensional, and it can hold

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十一)之Holding Your Objects

To solve the general programming problem, you need to create any number of objects, anytime, anywhere. So you can't rely on creating a named reference to hold each one of your objects. Java has several ways to hold objects: 1. the compiler-supported

Java性能提示(全)

http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLists and ArrayLists (and Vectors) (Page last updated May 2001, Added 2001-06-18, Author Jack Shirazi, Publisher OnJava). Tips: ArrayList is faster than

「Python」Numpy equivalent of MATLAB's cell array

转自Stackoverflow.备忘用. Question I want to create a MATLAB-like cell array in Numpy. How can I accomplish this? Answer Matlab cell arrays are most similar to Python lists, since they can hold any object - but scipy.io.loadmat imports them as numpy objec

引用与垃圾回收

reference /'ref?r?ns/ an indicator that orients you generally java.lang.ref.Reference<T> Abstract base class for reference objects. This class defines the operations common to all reference objects. Because reference objects are implemented in close

Python integer objects implementation

http://www.laurentluce.com/posts/python-integer-objects-implementation/ Python integer objects implementation May 15, 2011 This article describes how integer objects are managed by Python internally. An integer object in Python is represented interna

java.util.Objects 源码学习

Objects 与 Object 区别 Object 是 Java 中所有类的基类,位于java.lang包. Objects 是 Object 的工具类,位于java.util包.它从jdk1.7开始才出现,被final修饰不能被继承,拥有私有的构造函数. 它由一些静态的实用方法组成,这些方法是null-save(空指针安全的)或null-tolerant(容忍空指针的),用于计算对象的hashcode.返回对象的字符串表示形式.比较两个对象. Objects 各方法介绍与分析 equals

Holding Bin-Laden Captive!(1.多重背包 2.母函数)

Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 41 Accepted Submission(s): 29   Problem Description We all know that Bin-Laden is a notorious terrorist, and he has disap

Python 数据查询 objects.all() ,objects.get() ,objects.filter()之间的区别

rs=Person.objects.all() all返回的是QuerySet对象,程序并没有真的在数据库中执行SQL语句查询数据,但支持迭代,使用for循环可以获取数据. rs=Person.objects.get(id='1') get返回的是Model对象,类型为列表,说明使用get方法会直接执行sql语句获取数据 Person.objects.filter() filter和get类似,但支持更强大的查询功能