计划把 Java 基础的有些部分再次看一遍,巩固一下,下面以及以后就会分享自己再次学习的一点笔记!不是有关标题的所有知识点,只是自己觉得模糊的一些知识点。

1.  对于泛型类而言,你若没有指明其类型,默认为Object;

2.  在继承泛型类以及接口的时候可以指明泛型的类型,也可以不指明;

3.   泛型也数据库中的应用:

      写一个 DAO 类对数据库中的数据进行增删改查其类型声明为 <T> 。每张表对应一个类,对应每一张表实现一个类继承该 DAO 类并指明 DAO 泛型为该数据表对应的类,再实现一个与该表匹配的 DAO 操作类,这样就不必在每一个数据表的操作实现类中去实现增删改查的基本方法。例如(实际应用中大概就是这思想,下面的举例并不完整):

 //数据表对应的类
public class Customer{
private int id;
private String name;
...
} //所有数据表的操作类都要实现的 DAO 基类
public class DAO<T> {
//增
public void add(T t) {


} public T get(int index) {
//查
return null;
} public void delete() {
//删

} public List<T> getForList(int index) {
//查
return null;
} //数据表操作对应的实现类
public class CustomerDao extends DAO<Customer> { } //测试类
public class Test {
public static void mian(String[] args) {
CustomerDao cus = new CustomerDao;
Cus.add(new Customer);
}
}

4.   静态方法中不可以使用泛型(static)

      因为static 声明的方法或者类以及变量都是在类初始化的时候初始化,而泛型是在运行的时候才回去初始化的,所以就出现了问题(后出现的调用了先出现的)。

public T t;
//Error
public static void show() {
System.out.println(t);
}

5.  通配符

      可以读取声明为通配符的集合,但不可往里写入元素(读的时候可以把元素都认为是 Object,但写的时候其声明为 ?,不是 Object,也就是说写什么类型都是错的,但可以存 null),例如

Public void testList() {
List<String> strList = new ArrayList<>();
strList.add(“Hello”);
strList.add(“World”); //correct
List<?> list = strList; //error
list.add(“hi”);
list.add(123);
//correct
list.add(null);
}

6.  集合Map 的遍历

package com.java.map.test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set; public class MapEntry { public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1, "a");
map.put(2, "b");
map.put(3, "c");
map.put(4, "d");
map.put(5, "e");
// 得到 map 所有键的集合
Set<Integer> keys = map.keySet(); for (Integer key : map.keySet()) {
System.out.println(map.get(key));
} // 利用迭代器 遍历
Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator(); while (it.hasNext()) {
Map.Entry<Integer, String> entry = it.next();
System.out.println(entry.getKey() + " -> " + entry.getValue());
} // 第三种
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println(entry.getValue());
} // 遍历所有的 value 值
Collection<String> values = map.values(); for (String val : values) {
System.out.println(val + ">>-");
} Iterator<String> i = values.iterator();
while (i.hasNext()) {
System.out.println(i.next() + "-->");
} List<String> lists = new ArrayList<>();
lists.add("1");
lists.add("2");
lists.add("3");
lists.add("4"); Iterator<String> it2 = lists.iterator(); while (it2.hasNext()) {
System.out.println(it2.next());
} Collections.reverse(lists);
Iterator<String> it3 = lists.iterator();
// Comparator comparator = new while (it3.hasNext()) {
System.out.println(it3.next() + "<->");
}
}
}

7.  利用反射获取方法名和属性名,利用反射还可以获取构造器等其他信息

package com.java.reflct.test;

//实体类
public class Person { private String id; private String name; public int phone; public void setId(String id) {
this.id = id;
} public String getId() {
return id;
} public void setName(String name) {
this.name = name;
} public String getName() {
return name;
} public void setPhone(int phone) {
this.phone = phone;
} public int getPhone() {
return phone;
} private void print() {
System.out.println("your id is " + id + ", your name is " + name + ", your phone is " + phone + "!");
} @Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", phone=" + phone + "]";
}
} package com.java.reflct.test;
//测试类 import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; public class TestReflect { public static void main(String[] args) {
try {
// 通过 Class.forName("全类名"); 获取 Class,还以利用 对象名.getClass() 类名.class(); 获取Class
Class cla = Class.forName("com.java.reflct.test.Person");
Class cla2 = Person.class; // 获取所有的 变量,返回数组,包括私有变量
Field[] fields = cla2.getDeclaredFields();
// 遍历变量数组
for (Field fie : fields) {
System.out.println(fie+"-..-");
} // 获取所有的方法,返回数组,包括私有方法
Method[] methods = cla.getDeclaredMethods(); for (Method met : methods) {
System.out.println(met);
} try {
// 获取单个私有属性
Field field = cla.getDeclaredField("id");
// 打破封装
field.setAccessible(true);
System.out.println(field + "<<>>");
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} Method method = null; try {
// 获取单个私有方法
method = cla.getDeclaredMethod("print");
// 打破封装
method.setAccessible(true);
System.out.println(method + ">><<");
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} try {
// 通过cla.newInstance(); 获取类的对象
Person person = (Person) cla.newInstance();
person.setId("1");
person.setName("yinyin");
person.setPhone(110); System.out.println(person + "__>>__");
try {
// 执行 person 对象的中 method 所对应的方法
method.invoke(person);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} } catch(ClassNotFoundException e) {
System.out.println("There is no class " + e);
}
}
}

8.  Comparator  类的使用(利用  Comparator  实现集合的自定义排序)

      注意区分 Collections (集合的处理类)和 Collection (集合基类)

package com.java.collection.test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List; /*
* Collections 是 Collection 的操作类
*/ public class CollectionComparator { public static void main(String[] args) { Comparator<Customer> com = new Comparator<Customer>(){ @Override
public int compare(Customer o1, Customer o2) {
// 将 id 的比较值放入参数中,使得 id 相等时有其他的处理方法
int i = o1.getId().compareTo(o2.getId()); // 当 Id 相等的时候比较名字的顺序
if (i == 0) {
// 给 return 添加一个 - 号可以实现 “从大到小”
return o1.getName().compareTo(o2.getName());
}
// Id 不相等时返回其值
return i;
}
}; List<Customer> lists = new ArrayList<>();
lists.add(new Customer("yinyin", "110", 1001));
lists.add(new Customer("zhao", "10086", 1002));
lists.add(new Customer("ls", "10010", 1001));; Collections.sort(lists, com); // 利用匿名类实现自定义排序
/*
Collections.sort(lists, new Comparator<Customer>(){ @Override
public int compare(Customer o1, Customer o2) {
// 将 id 的比较值放入参数中,避免 id 相等没有其值
int i = o1.getId().compareTo(o2.getId()); // 当 Id 相等的时候比较名字的顺序
if (i == 0) {
return o1.getName().compareTo(o2.getName());
}
return i;
}
});
*/
//利用迭代器遍历集合
Iterator<Customer> it = lists.iterator();
while(it.hasNext()) {
System.out.println(it.next());
} }
}

9.  IO

      读取目标文本文件的字节数

package com.java.io.file.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream; public class MyIoTest { public static void main(String[] args) { // 在 IO 中出现的异常最好都使用 try-catch 包裹起来,不要 throw,因为这样可以保证流的关闭在任何时候都可以正常执行
InputStream fileStream = null;
int count = 0;
try {
fileStream = new FileInputStream(new File("hello.txt"));
// 读取文件的下一个字节
while (fileStream.read() != -1) {
count++;
}
// 打印目标文件的字节数
System.out.println(count);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fileStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

      实现文件的复制(InputStream 、OutputStream 和 Reader 、Writer)。文本文件的操作使用 Reader Writer(字符流) 去实现,效率高。但是不可以去操作媒体文件;媒体文件使用 InputStream OutputStream 去实现,也可以对文本文件进行操作,但是没有字符流高效。

package com.java.io.file.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; public class CopyFile { public static void main(String[] args) {
// 设置一次从目标文件中读取多少字节
byte[] buffer = new byte[1024]; int len = 0;
File file = new File("C:/Users/lenovo/Desktop/123.wmv");
InputStream fileInput = null;
OutputStream fileOut = null; try {
fileInput = new FileInputStream(file);
fileOut = new FileOutputStream(new File("C:/Users/lenovo/Desktop/trave2.wmv")); // len 的作用是防止读取文件时最后一次其长度不够读取被置为零,read() 返回读入缓冲区的字节总数
while ((len = fileInput.read(buffer)) != -1) {
fileOut.write(buffer, 0, len);
}
System.out.println("SUCC");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fileOut.close();
fileInput.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} } //利用 Reader Writer 实现
package com.java.io.file.test; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer; public class ReaderWriter { public static void main(String[] args) {
File file = new File("hello.txt");
int len = 0;
Reader fileReader = null;
Writer fileWriter = null;
char[] ch = new char[125]; try {
fileReader = new FileReader(file);
fileWriter = new FileWriter(new File("world.txt")); while((len = fileReader.read(ch)) != -1) {
fileWriter.write(ch, 0, len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fileWriter.close();
fileReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

10.  利用缓冲流实现文件的复制操作,效率更高

package com.java.io.file.test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; public class TestBufferedCopy {
// 使用缓冲流实现文件的复制
public static void main(String[] args) { int len = 0;
byte[] buffer = new byte[2048]; File file = new File("C:/Users/lenovo/Desktop/123.rmvb");
InputStream inputFile = null;
OutputStream outputFile = null; BufferedInputStream bufferedInput = null;
BufferedOutputStream bufferedOutput = null; try {
inputFile = new FileInputStream(file);
outputFile = new FileOutputStream("C:/Users/lenovo/Desktop/456.rmvb"); bufferedInput = new BufferedInputStream(inputFile);
bufferedOutput = new BufferedOutputStream(outputFile); while((len = bufferedInput.read(buffer)) != -1) {
bufferedOutput.write(buffer, 0, len);
} System.out.println("SUCC");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
// 只需关闭复制文件用到的就可以,即最后两个
bufferedOutput.close();
bufferedInput.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

上面的代码总结啥的都是自己平常练习过程中的代码和心得,对于知识点讲解覆盖的并不全面,还望谅解。初来乍到不知道该如何去写!

Java 基础 -- 泛型、集合、IO、反射的更多相关文章

  1. java基础-Map集合

    java基础-Map集合 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Map集合概述 我们通过查看Map接口描述,发现Map接口下的集合与Collection接口下的集合,它 ...

  2. Java基础之 集合体系结构(Collection、List、ArrayList、LinkedList、Vector)

    Java基础之 集合体系结构详细笔记(Collection.List.ArrayList.LinkedList.Vector) 集合是JavaSE的重要组成部分,其与数据结构的知识密切相联,集合体系就 ...

  3. 备战金三银四!一线互联网公司java岗面试题整理:Java基础+多线程+集合+JVM合集!

    前言 回首来看2020年,真的是印象中过的最快的一年了,真的是时间过的飞快,还没反应过来年就夸完了,相信大家也已经开始上班了!俗话说新年新气象,马上就要到了一年之中最重要的金三银四,之前一直有粉丝要求 ...

  4. 第6节:Java基础 - 三大集合(上)

    第6节:Java基础 - 三大集合(上) 本小节是Java基础篇章的第四小节,主要介绍Java中的常用集合知识点,涉及到的内容包括Java中的三大集合的引出,以及HashMap,Hashtable和C ...

  5. java基础技术集合面试【笔记】

    java基础技术集合面试[笔记] Hashmap: 基于哈希表的 Map 接口的实现,此实现提供所有可选的映射操作,并允许使用 null 值和 null 键(除了不同步和允许使用 null 之外,Ha ...

  6. Java基础---泛型、集合框架工具类:collections和Arrays

    第一讲     泛型(Generic) 一.概述 1.JDK1.5版本以后出现的新特性.用于解决安全问题,是一个类型安全机制. 2.JDK1.5的集合类希望在定义集合时,明确表明你要向集合中装入那种类 ...

  7. java基础—泛型

    一.体验泛型 JDK1.5之前的集合类中存在的问题——可以往集合中加入任意类型的对象,例如下面代码: 1 package cn.gacl.generic.summary; 2 3 import jav ...

  8. Java基础--说集合框架

    版权所有,转载注明出处. 1,Java中,集合是什么?为什么会出现? 根据数学的定义,集合是一个元素或多个元素的构成,即集合一个装有元素的容器. Java中已经有数组这一装有元素的容器,为什么还要新建 ...

  9. java基础-泛型举例详解

    泛型 泛型是JDK5.0增加的新特性,泛型的本质是参数化类型,即所操作的数据类型被指定为一个参数.这种类型参数可以在类.接口.和方法的创建中,分别被称为泛型类.泛型接口.泛型方法. 一.认识泛型 在没 ...

随机推荐

  1. Shiro第一篇【Shiro的基础知识、回顾URL拦截】

    Shiro基础知识 在学习Shiro这个框架之前,首先我们要先了解Shiro需要的基础知识:权限管理 什么是权限管理? 只要有用户参与的系统一般都要有权限管理,权限管理实现对用户访问系统的控制,按照安 ...

  2. linux (1)基本知识/目录/磁盘格式/文件系统

    一.linux基本知识介绍1.命令行格式:(按两次tab可以知道有多少个可执行命令,我的有1980个,用户有1960个)[用户名@linux主机名 ~(当前目录)]$ 命令 选项 参数1 参数2[ro ...

  3. Oracle日期时间操作大全

    本文出自:http://www.cnblogs.com/hl3292/archive/2010/11/03/1868159.html oracle sql日期比较: 共三部分: 第一部分:oracle ...

  4. 嵌入系统squashfs挂载常见问题总结

    由于squahsfs的一些优点,嵌入系统常常直接使用squashfs作为initrd挂载到/dev/ram,作为rootfs.这里对常见的一些问题进行一些分析. 1. kernel启动出现错误 RAM ...

  5. 1 Spring Cloud Eureka服务治理

    注:此随笔为读书笔记.<Spring Cloud微服务实战> 什么是微服务? 微服务是将一个原本独立的系统拆分成若干个小型服务(一般按照功能模块拆分),这些小型服务都在各自独立的进程中运行 ...

  6. [python学习笔记] py2exe 打包

    遇坑 之前经过折腾,pyinstaller打包文件可以在别的windows7上运行.但是,mfk, 客户说是xp系统.崩溃 使用pyinstaller各种折腾,打包出来的依然是不是有效的win32程序 ...

  7. NDK调试

    第一种(控制台输出): 1.配置好环境变量,这是为了方便起见.将你sdk和ndk的根目录放到环境变量path中.配置完成之后可以来个小检测: 在命令行分别输入adb和ndk-stack后点击回车,只要 ...

  8. SpringAop详解

    近几天学习了一下SpringAop在网上找了一些资料,此链接为原文链接http://www.cnblogs.com/xrq730/p/4919025.html AOP AOP(Aspect Orien ...

  9. Quartz学习——Spring和Quartz集成详解(三)

    Spring是一个很优秀的框架,它无缝的集成了Quartz,简单方便的让企业级应用更好的使用Quartz进行任务的调度.下面就对Spring集成Quartz进行简单的介绍和示例讲解!和上一节 Quar ...

  10. 第5章 不要让线程成为脱缰的野马(Keeping your Threads on Leash) ---简介

    这一章描述如何初始化一个新线程,如何停止一个执行中的线程,以及如何了解并调整线程优先权.    读过这一章之后,你将有能力回答一个 Win32 多线程程序设计的最基本问题.你一定曾经在 Usenet ...