public void addElement(E obj):在vector的末尾添加一个新的元素
public E elementAt(int index):获取指定位置处的元素
public Enumeration<E> elements():就类似于迭代器样,vector的特有方法 Enumeration接口的两个特有方法:
boolean hasMoreElements():是否有更多的组件(元素)可以遍历----类似于迭代器接口中的boolean hasNext()
E nextElement():获取下一个元素--- 类似于迭代器接口中的 E next()
因此,list集合多了两种遍历方式:
方式一:public E elementAt(int index)和size()方法相结合普通for
方式二: public Enumeration<E> elements():Vector的特有的迭代器的操作
Enumeration<String> enumeration = vector.elements();
while(enumeration.hasMoreElements()){
String s = enumeration.nextElement();
System.out.println(s);
}
3.LinkedList集合特有功能
底层数据结构是一个链表实现:查询慢,增删快
从线程角度考虑:线程不安全的类---->此实现不同步---->执行效率高
单线程只考虑效率问题,多线程环境必须先考虑安全性;
特有功能:
public void addFirst(Object e):将任何类型的元素每次添加到链表的开头
public void addLast(Objet e):将任何类型的元素每次条件到链表的末尾
public Object removeFirst()从此列表中删除并返回第一个元素。
public Object removeLast()从此列表中删除并返回最后一个元素
public Object getFirst():获取链表的第一个元素
public Object getLast):获取链表的最后个元素
自然排序:使用treeSet无参构造方法,自定义对象实现compareable接口,重写compareable接口的compareTo方法,重写是要加主要条件和次要条件
public class TreeSetDemo2 {
public static void main(String[] args) {
TreeSet<Student> t = new TreeSet<>();
Student s = new Student("张三",98,95,90);
Student s1 = new Student("李四",95,98,90);
Student s2 = new Student("王五",95,98,91);
Student s3 = new Student("赵六",91,98,95);
t.add(s);
t.add(s1);
t.add(s2);
t.add(s3);
for(Student st:t) { System.out.println(st.getName()+"\t"+st.gettotol()+"\t"+st.getChinaGrade()+"\t"+st.getEnglishGrade()
+"\t"+st.getMathGrade());}
Student类:
public class Student implements Comparable<Student>{
private String name;
private int chinaGrade ;
private int mathGrade;
private int englishGrade ;
public int gettotol(){
return chinaGrade+mathGrade+englishGrade;
}
public Student() {
}
public String getName() {
return name;
}
public int getChinaGrade() {
return chinaGrade;
}
public int getMathGrade() {
return mathGrade;
}
public int getEnglishGrade() {
return englishGrade;
}
public Student(String name, int chinaGrade, int mathGrade, int englishGrade) {
this.name = name;
this.chinaGrade = chinaGrade;
this.mathGrade = mathGrade;
this.englishGrade = englishGrade;
}
@Override
public int compareTo(Student s) {
int num = s.gettotol()-this.gettotol();
int num1 = (num==0)?s.chinaGrade-this.chinaGrade:num;
int num2 = (num1==0)?s.mathGrade-this.mathGrade:num1;
int num3 = num2==0?s.englishGrade-this.englishGrade:num2;
return num3;
}
}
结果:
王五 284 95 91 98
赵六 284 91 95 98
张三 283 98 90 95
李四 283 95 90 98
4.3.3 自定义的treeSet方法二:选择器排序
选择器排序:使用treeSet(comparator接口)有参构造方法(参数就是comperator对象),接口作为形式参数,接口的匿名内部类李或者子实现类实现compare方法
//测试类
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet<Student> t = new TreeSet<>(new Comparator<Student>() {//匿名内部类
@Override
public int compare(Student s1, Student s2) {
int num = s2.gettotol() - s1.gettotol();
int num1 = (num == 0) ? s2.getChinaGrade() - s1.getChinaGrade() : num;
int num2 = (num1 == 0) ? s2.getMathGrade() - s1.getMathGrade() : num1;
int num3 = num2 == 0 ? s2.getEnglishGrade() - s1.getEnglishGrade() : num2;
return num3;
}
});
Student s = new Student("张三", 98, 95, 90);
Student s1 = new Student("李四", 95, 98, 90);
Student s2 = new Student("王五", 95, 98, 91);
Student s3 = new Student("赵六", 91, 98, 95);
t.add(s);
t.add(s1);
t.add(s2);
t.add(s3);
for (Student st : t) {
System.out.println(st.getName() + "\t" + st.gettotol() + "\t" + st.getChinaGrade() + "\t" + st.getEnglishGrade() + "\t" + st.getMathGrade());
}
}
}
//学生类正常提供构造方法和set/get即可
Map是一个接口不能实例化,通过具体的子实现类来实例化:
通用的子实现类HashMap--->基于哈希表的实现Map接口
特点:允许null的值和null键--->说明---HashMap是一个线程不安全的--->不同步---->执行效率高!
HashMap不能保证迭代次序恒久不变
功能:
int size():获取集合是元素数
当HashMap的值是空的时候,则返回{}--表示一个空的实体类;
添加:
public V put(K key,V value):添加键值对元素,返回的值
如果key不重复,第一次添加,返回值是--->null
如果key重复,再次添加,则后面的V会覆盖前面的V,并且返回前面的V
通过key是否为null,来判断它是否是重复添加的!
删除:
public V remove(Object key):删除指定的键,返回被删除的键对应的值()如果删除不存在的键值,则返回null
public void clear():暴力删除---清空了(所有键值对象元素删除)
判断:
public boolean containsKey(Object key):判断是否包含指定的键
public boolean containsValue(Object value):是否包含指定的值
public boolean isEmpty():判断集合是否为空
6.2 map的特有功能(遍历)
HashMap是基于HashCode和equals方法,所以当键值是自定义对象时,自定义类必须重写HashCode和equals方法;
方式一(建议):Set<K> keySet():获取所有的键值
然后遍历所有的键值,再根据方法V get(key)获取键值对应的value值
方式二:Set<Map.Entry<K,V>> entrySet():获取所有键值对的对象
然后遍历出所有所有键值对的对象,然后按下边方法取出键值
K getKey():获取此对象相对应的键
V getValue():获取此对象相对应的值:value
public class HashSetDemo {
public static void main(String[] args) {
//创建HashSet对象
HashMap<String, String> map = new HashMap<>();
//添加键值对
map.put("高圆圆","赵又廷");
map.put("文章","姚笛");
map.put("黄海波","高圆圆");
map.put("文章","马伊琍");
//方式一:Set<K> keySet():获取所有的键值
/* Set<String> strings = map.keySet();
for(String str:strings){
String s = map.get(str);
System.out.println(str+"\t"+s);
}*/
//方式二:Set<Map.Entry<K,V>> entrySet():获取所有键值对的对象
Set<Map.Entry<String, String>> entries = map.entrySet();
for(Map.Entry<String,String> en:entries){
String key = en.getKey();
String value = en.getValue();
System.out.println(key+"\t"+value);
}
}
}
public static T max(Collection<? extends T> coll):获取Collection中的最大值
public static <T> int binarySearch(List<? extends Comparable<? super T>> list,T key)
二分搜索法(折半查找):查询集合中指定的元素第一次出现的索引值,(前提:集合必须有序!)
//对list集合进行排序的方法
static <T extends Comparable<? super T>> void sort(List<T> list) :自然排序
static <T> void sort(List<T> list, Comparator<? super T> c) :针对List集合的比较器排序
public static void shuffle(List<?> list):随机置换
6.6 对List集合进行排序的方法
static <T extends Comparable<? super T>> void sort(List<T> list) :自然排序--T如果是自定义类型,必须实现Compareable接口,实现compareTo方法
static <T> void sort(List<T> list, Comparator<? super T> c) :针对List集合的比较器排序
public final void setName(String name):设置线程名称
public final String getName():获取线程名称(在run方法中使用)
public final void join():等待这个线程死亡
throws InterruptedException:需要抛出异常
public final void setPriority(int newPriority):设置优先级
public final int getPriority():获取优先级
public static final int MIN_PRIORITY:最小优先级
public static final int NORM_PRIORITY:默认优先级
public static final int MAX_PRIORITY:最大优先级
public static void yield():暂停当前正在执行的线程并且执行其他线程(使用这个方法的目的是让相同优先级的线程出现一种轮转运行,但是不能保证让步的目的,因为可能一个线程在让步的同时,被另一个线程抢占了执行权)
public static void sleep(long millis):使当前正在执行的线程以指定的毫秒数暂停(暂时停止执行),
throws InterruptedException(只能使用try/catch方法抛出异常)
饿汉式:不会出现问题的单例模式
特点:1)当前类的无参构造方法私有化,外界不能new;
2)在当前类的成员位置,创建一个当前类对象,被静态修饰并且私有化
3)对外提供公共的静态功能,返回值当前类本身
Class Student{
private static Student s = new Student;
private Student(){};
public static Student getStudent(){
return s;
}
}
class studenttest{
public static void main(String[] args){
Student s1 = Student.getStudent();
Student s2 = Student.getStudent();
System.out.println(s1==s2);//结果为ture
}
}
10.2.2 懒汉式单例模式
懒汉式:可能出现问题的单例模式,它不是类一加载就直接创建当前类实例,而是需要判断,用的时候,才创建实例!当前存在延迟加载或者懒加载的时候类似于一种多线程环境了,就会出现线程安全问题,解决办法就是加锁,使用同步方法
Class Teather{
private static Teather t ;
public synchronized static Teather getTeather(){
//锁对象就是Teacher.class;
if(t == null){
t = new Teather();
return t;
}else{
return t;
}
}
}
11. IO流的分类(input and outprint)
11.1 File类的认识和方法
File()类:文件和目录(文件夹)的抽象路径形式
构造方法:File(String pathname)(推荐)
例如File file = new File("E:\\demo\\a.txt")
File的基本方法:
public boolean createNewFile() throws IOException:创建文件
public boolean mkdir():创建目录,如果不存在,则创建,否则返回false;
public boolean mkdirs():创建多个目录,如果父目录不存在,自动创建父类以及里面的子目录
public boolean delete():可以删除文件或者文件夹,删除文件夹的时候,注意,如果目录为空,才能删除!
绝对路径:带盘符的具体路径:在指定的目录中 创建文件或者文件夹
相对路径:如果不带盘符:在当前项目下的路径下创建文件或者目录
判断功能:
public boolean exists():file所表示的路径是否存在
public boolean isFile():是否是文件
public boolean isDirectory():是否是目录
public long length():获取长度
public class InputStreamDemo {
public static void main(String[] args) throws Exception {
//创建字节输入流对象
InputStream is = new FileInputStream("OutputStreamDemo.java");
//创建数组的时候,一般长度是1024或者1024的整数倍,一般1024足够用了
byte[] bytes = new byte[1024];
int len = 0;//从0开始
while((len=is.read(bytes))!=-1){
String string = new String(bytes,0,len);
System.out.println(string);
}
}
}
11.5 实际操作:针对文本文件,图片文件,音频文件,进行读写
public class InputStreamDemo {
public static void main(String[] args) throws Exception {
InputStream is = new FileInputStream("OutputStreamDemo.java");
OutputStream os = new FileOutputStream("fos2.java");
byte[] bytes = new byte[1024];
int len = 0;
while((len=is.read(bytes))!=-1){
//将输入流的内容读取出来并且写进输出流对象中
os.write(bytes,0,len);
}
}
}
11.6.1 使用字节缓冲输入流和输出流读写复制—使用缓冲区使读写更高效
publicclass copyTest {publicstaticvoidmain(String[] args)throwsIOException{BufferedOutputStream bos=newBufferedOutputStream(newFileOutputStream("copy.java"));BufferedInputStream bis =newBufferedInputStream(newFileInputStream("fos4.java"));byte[] bytes =newbyte[1024];int len =0;while((len = bis.read(bytes))!=-1){
bos.write(bytes,0,len);
bos.flush();}
bos.close();
bis.close();}}