《Think in Java》17~18
chapter 17 容器深入研究
填充容器
package cn.test; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; class StringAddress{
private String s;
public StringAddress(String s) {this.s=s;};
public String toString() {
return super.toString()+" "+s;
} }
public class FillingLists {
public static void main(String[] args) {
List<StringAddress> list=new ArrayList<StringAddress>(Collections.nCopies(4, new StringAddress("Hello")));
System.out.println(list);
Collections.fill(list, new StringAddress("World!"));
System.out.println(list);
}
}
一种Generator解决方案
public interface Generator<T> {
T next();
}
package cn.test;
import java.util.ArrayList;
public class CollectionData<T> extends ArrayList<T> {
public CollectionData(Generator<T> gen,int quantity) {
for(int i=0;i<quantity;i++) {
add(gen.next());
}
}
public static <T> CollectionData<T>
list(Generator<T> gen,int quantity){
return new CollectionData<T>(gen,quantity);
}
}
Set和存储顺序

队列
package cn.test; import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.PriorityBlockingQueue; public class QueueBehavior {
private static int count=10;
static <T> void test(Queue<T> queue,Generator<T> gen) {
for(int i=0;i<count;i++) {
queue.offer(gen.next());
while(queue.peek()!=null) {
System.out.print(queue.remove()+" ");
}
}
System.out.println();
}
static class Gen implements Generator<String>{
String[] s=("one two three four five six seven "
+ "eight nine ten").split(" ");
int i;
public String next() {
return s[i++];
} }
public static void main(String[] args) {
test(new LinkedList<String>(),new Gen());
test(new PriorityQueue<String>(),new Gen());
test(new ArrayBlockingQueue<String>(count),new Gen());
test(new ConcurrentLinkedQueue<String>(),new Gen());
test(new LinkedBlockingQueue<String>(),new Gen());
test(new PriorityBlockingQueue<String>(),new Gen()); } }
优先级队列
理解Map
性能


SortedMap
散列与散列码
package cn.test; import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
import java.util.Random; class Groundhog{
protected int number;
public Groundhog(int n) {number = n;}
public String toString() {
return "Groundhog # "+number;
}
}
class Prediction{
private static Random rand=new Random(47);
private boolean shadow=rand.nextDouble() > 0.5;
public String toString() {
if(shadow)
return "Six more weeks of Winter!";
else
return "Early Spring!";
}
}
public class SpringDetector {
public static <T extends Groundhog>
void detectSpring(Class<T> type)throws Exception{
Constructor<T> ghog = type.getConstructor(int.class);
Map<Groundhog,Prediction> map=new HashMap<Groundhog,Prediction>();
for(int i=0;i<10;i++)
map.put(ghog.newInstance(i), new Prediction());
System.out.println("map = "+map);
Groundhog gh = ghog.newInstance(3);
System.out.println("Looking up prediction for "+gh);
if(map.containsKey(gh))
System.out.println(map.get(gh));
else
System.out.println("Key not found: "+gh);
}
public static void main(String[] args) throws Exception {
detectSpring(Groundhog.class);
}
}
为速度而散列
覆盖hashCode()
Collection或Map的同步控制
public class Synchronization {
public static void main(String[] args) {
Collection<String> c = Collections.synchronizedCollection(new ArrayList<String>());
List<String> list = Collections.synchronizedList(new ArrayList<String>());
Set<String> s = Collections.synchronizedSet(new HashSet<String>());
SortedSet<String> ss = Collections.synchronizedSortedSet(new TreeSet<String>());
Map<String,String> m=Collections.synchronizedMap(new HashMap<String,String>());
Map<String,String> sm=Collections.synchronizedSortedMap(new TreeMap<String,String>());
}
}
快速报错
public class FailFast {
public static void main(String[] args) {
Collection<String> c=new ArrayList<String>();
Iterator<String> it=c.iterator();
c.add("An object");
try {
String s=it.next();
} catch (ConcurrentModificationException e) {
System.out.println(e);
}
}
}
// java.util.ConcurrentModificationException
在获取迭代器后,容器发生了变化。
持有引用
Hashtable
Hashtable 过时的类,线程安全,键值不为null;
HashMap 线程不安全 ,但可以通过Collections类转为线程安全。Map m = Collections.synchronizeMap(hashMap),键值可以为null;
Stack
package cn.test; import java.util.LinkedList;
import java.util.Stack; enum Month{
january,february,march,april,may,june,july,august,september,october,november
}
public class Stacks {
public static void main(String[] args) {
Stack<String> stack=new Stack<String>();
for(Month m:Month.values())
stack.push(m.toString());
System.out.println("stack = "+ stack);
stack.addElement("The last line");
System.out.println("element 5 = "+stack.elementAt(5));
System.out.println("popping elements : ");
while(!stack.empty())
System.out.println(stack.pop()+" ");
LinkedList<String> lstack=new LinkedList<String>();
for(Month m: Month.values())
lstack.addFirst(m.toString());
System.out.println("lstack = "+lstack);
while(!lstack.isEmpty())
System.out.println(lstack.removeFirst()+" ");
}
}
chapter 18 Java I/O系统
输入和输出
XML
I/O流的典型使用方式
对象序列化
对于某些实现了Serialiazable 又不想被序列化的字段,可以在字段前添加 transient 关键字。
《Think in Java》17~18的更多相关文章
- 读书笔记 之《Thinking in Java》(对象、集合)
一.前言: 本来想看完书再整理下自己的笔记的,可是书才看了一半发现笔记有点多,有点乱,就先整理一份吧,顺便复习下前面的知识,之后的再补上. 真的感觉,看书是个好习惯啊,难怪人家说"书籍是人类 ...
- 《Head First Java》读书笔记(1) - Java语言基础
<Head First Java>(点击查看详情) 1.写在前面的话 这本书的知识点说实话感觉有点散乱,但是贵在其将文字转换成了生动和更容易接受的图片,大量的比喻让人感受到了知识点的有趣之 ...
- 读书笔记 之《Thinking in Java》(对象、集合、异常)
一.前言 本来想看完书再整理下自己的笔记的,可是书才看了一半发现笔记有点多,有点乱,就先整理一份吧,顺便复习下前面的知识,之后的再补上. 真的感觉,看书是个好习惯啊,难怪人家说“书籍是人类进步的阶梯” ...
- 《Play for Java》学习笔记(一)项目框架
从今天开始认真复习<Play for JAVA>,该书以一个案例为主线,以前为应付项目马马虎虎看了一遍,好多地方都不明白!现在跟着这本书再走一遍,认真模拟,当做一个项目啦!! 一.Play ...
- 《图书管理系统——java》
/* (程序头部凝视開始) * 程序的版权和版本号声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved. * 文件名: < ...
- 【书海】《Head First Java》 ——读后总结
<Head First Java> 中文版 (第二版) IT`huhui前言录 <Head First Java>这本书我不算特别细的看了一遍.认为十分适合初学者,甚至是没接触 ...
- 我本人一直以来犯的错误,在看了《Think In Java》后才抓了出来(转)
也许你是只老鸟,也许你的程序编的很精,但是,在你的程序生活,你也许没有注意到一些“常识性”的问题,因为有些时候我们不需要去注意,我们的程序 照样能够运行得飞快,但是如果那天有一个无聊的人问你一个像这样 ...
- 《Thinking in Java》 And 《Effective Java》啃起来
前言 今天从京东入手了两本书,<Thinking in Java>(第四版) 和 <Effective Java>(第二版).都可以称得上是硬书,需要慢慢啃的,预定计划是在今年 ...
- D1——初读《Head First Java》
今天随便看了点<Head First Java>,发觉这本书的风格真是有趣.打算先把这本书踏踏实实的看了.学习切忌好高骛远.心浮气躁,尤其入门基础阶段更应该踏踏实实地学习知识.下面随便谈谈 ...
随机推荐
- MongoDB常用增删改查语句
数据库database 创建及查看库 1.有则使用这个数据库,没有就创建 use DATABASE_NAME 2. 查看当前选择的数据库,默认是test db 3.查看数据库,默认有admin.loc ...
- 使用JavaScript弹出Confirm对话框
方法1: 这个比较简单,一句话: <a href="error.htm" onclick="javascript:return confirm('are you s ...
- 团队作业4Alpha冲刺
仓库地址:https://gitee.com/ILoveFunGame/game_strategy_network.git 第一天 2018/6/13 1.1 今日完成任务情况以及遇到的问题. 1.1 ...
- java基于feemarker 生成word文档(超级简单)
问题由来: 开发个新需求,需要按规定导出word文档,文档截图如下 因为之前没做过这个,一脸懵B啊,导出excel和txt倒是经常接触到,对于这个word这种格式不严谨的文件怎么处理呢? 技术选型:可 ...
- 【BZOJ3998】弦论 【后缀自动机】
题意 给定一个长度为n的字符串,求他的第k小子串是什么. 分析 T=0的时候,这个题跟SPOJ-SUBLEX的做法一样,当T=1的时候,不同位置的子串算多个,那么初始化的时候d[u]=cnt[u],没 ...
- Python中super详解
转至:https://mozillazg.com/2016/12/python-super-is-not-as-simple-as-you-thought.html 说到 super, 大家可能觉得很 ...
- 校准liunx时间简单好用的命令
查看时间服务器的时间# rdate time-b.nist.gov 设置时间和时间服务器同步# rdate -s time-b.nist.gov 查看硬件时间 # hwclock 将系统时间写入硬件时 ...
- 各种异常 及异常类和Object类 Math类
Day05 异常 Object类 equals方法,用于比较两个对象是否相同,它其实就是使用两个对象的内存地址在比较.Object类中的equals方法内部使用的就是==比较运算符. 2. 描述人这个 ...
- Fix: The account is not authorized to log in from this station
If you have more the one computers running Windows, then its possible to connect them using HomeGrou ...
- XSS的原理分析与解剖:第三章(技巧篇)【转】
0×01 前言: 关于前两节url: 第一章:http://www.freebuf.com/articles/web/40520.html 第二章:http://www.freebuf.com/art ...