Map集合
map集合通过映射的方式来存储数据。
Map<K,V>,数据具有一个键值K,相当于下标,还具有一个数值V。
Map集合的通用方法
添加
put<K,V>:将指定的数据添加到map集合中
删除
remove:删除键值K的数据
判断有无数据
containsKey:判断是否有简直为K的数据,如果有则返回true,没有就返回false
获取指定数据
get:获取指定键值的数据。
Map集合的特殊迭代方法:keySet和entrySet
Map集合没有迭代器,所以我们需要使用set集合中的迭代器来取出数据。
1.keySet方法:将Map集合中的键值取出,生成一个新的Set集合,通过Set集合的迭代器来访问键值取出数据。
HashMap<Integer,String> hm=new HashMap<Integer, String>();
hm.put(1,"A");
hm.put(2,"B");
hm.put(3,"C");
hm.put(4,"D");
Set<Integer> keyset=hm.keySet();
Iterator it=keyset.iterator();
while (it.hasNext())
{
System.out.println(hm.get(it.next()));
}
2.entrySet方法:
HashMap:数据底层是哈希表的Map集合
Map集合的底层数据是哈希表,具有无序,不可重复的特点。
自定义存储HashMap:通过复写equals和hashCode方法。
import java.util.*;
class student
{
private String name;
private int age;
student(String name,int age)
{
this.name=name;
this.age=age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public int hashCode()
{
return this.name.hashCode()+this.age*5;
}
public boolean equals(Object obj)
{
if(!(obj instanceof student))
return false;
student s=(student)obj;
return this.name.equals(s.name) && this.age==s.age;
}
}
class HashMapDemo
{
public static void main(String[] args)
{
HashMap<student,String> hm=new HashMap<student,String>();
hm.put(new student("li",12),"nanjing");
hm.put(new student("wang",12),"xi'ann");
hm.put(new student("ni",12),"beijing");
hm.put(new student("li",13),"nanjing");
Set<student> smp=hm.keySet();
Iterator it=smp.iterator();
while(it.hasNext())
{
System.out.println(hm.get(it.next()));
}
}
}
Map的扩展,Map嵌套Map:Map的数据类型中有Map。
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
class keySetDemo
{
public static void main(String[] args)
{
HashMap<String,HashMap<String,String>> collage=new HashMap<String,HashMap<String,String>>();
HashMap<String,String> shanxi =new HashMap<String, String>();
HashMap<String,String> beijing=new HashMap<String, String>();
collage.put("shanxicollage",shanxi);
collage.put("beijingcollage",beijing);
shanxi.put("sxStudent1","sx1");
shanxi.put("sxStudent2","sx2");
shanxi.put("sxStudent3","sx3");
beijing.put("bjStudent1","bj1");
beijing.put("bjStudent2","bj2");
beijing.put("bjStudent3","bj3");
Set<String> key=collage.keySet();
Iterator<String> it=key.iterator();
while (it.hasNext())
{
String place=it.next();
sop(collage.get(place));
}
}
public static void sop(HashMap<String,String> plcollage)
{
Iterator<String> it=plcollage.keySet().iterator();
while(it.hasNext())
{
String place=it.next();
System.out.println(plcollage.get(place));
}
}
}
Map集合参数中含有List集合:
import sun.plugin.viewer.context.IExplorerAppletContext;
import java.util.*;
class student
{
private String name;
private String id;
student(String name,String id)
{
this.name=name;
this.id=id;
}
public String getMessage()
{
return name+"---"+id;
}
}
class keySetDemo
{
public static void main(String[] args)
{
HashMap<String, ArrayList<student>> collage=new HashMap<String,ArrayList<student>>();
ArrayList<student> class1=new ArrayList<student>();
ArrayList<student> class2=new ArrayList<student>();
collage.put("class1",class1);
collage.put("class2",class2);
class1.add(new student("A","01"));
class1.add(new student("B","02"));
class2.add(new student("C","01"));
class2.add(new student("D","02"));
Set<String> key=collage.keySet();
Iterator<String> it=key.iterator();
while (it.hasNext())
{
String classnum=it.next();
System.out.println(classnum);
ArrayList<student> al=collage.get(classnum);
Iterator it2=al.iterator();
while(it2.hasNext())
{
student s=(student) it2.next();
System.out.println(s.getMessage());
}
}
}
}
Collections
Collections中的sort方法
调用此方法,按照自然顺序将List集合中的元素进行排序,它还可以用Comparator接口进行自定义排序。
Collecions.sort(List);
import sun.plugin.viewer.context.IExplorerAppletContext;
import java.io.CharArrayReader;
import java.util.*;
class student
{
private String name;
private String id;
student(String name,String id)
{
this.name=name;
this.id=id;
}
public String getMessage()
{
return name+"---"+id;
}
}
class keySetDemo
{
public static void main(String[] args)
{
ArrayList al=new ArrayList();
al.add("qwe") ;
al.add("qwe4") ;
al.add("qwer") ;
al.add("sadce") ;
al.add("zxc") ;
Collections.sort(al);
System.out.println(al);
Collections.sort(al,new diycompare());
System.out.println(al);
}
}
class diycompare implements Comparator<String>
{
public int compare(String s1,String s2)
{
int num=new Integer(s1.length()).compareTo(s2.length());
if(num==0)
{
return s1.compareTo(s2);
}
return num;
}
}
Collections中的max方法
找出集合中最大的元素,在没有Comparator比较器的时候,它是按照自然顺序的最大元素,如果人为加入Comparator比较器,那么它就会按照比较器中的规则排序,最大值就改变。
ArrayList al=new ArrayList();
al.add("qwe") ;
al.add("qwe4") ;
al.add("qwer") ;
al.add("sadce") ;
al.add("zxc") ;
System.out.println(Collections.max(al));
Collections中的binarySearch(List,“xxx”)方法
该方法二分查找,会查找指定参数,找到,则返回其角标,如果没有,则返回
ArrayList al=new ArrayList();
al.add("qwe") ;
al.add("qwe4") ;
al.add("qwer") ;
al.add("sadce") ;
al.add("zxc") ;
System.out.println(Collections.binarySearch(al,"qwee"));
Collections中的fill(List,“xxx”)方法
将集合所有的数据全部用指定元素替代。
Collections中的replaceAll(List,“old”,“new”)方法
把List集合中所有出现的old值替换成new值。
Collections中的reverse(List)方法
将集合的顺序反转,头变尾,尾变头。
Collections中的reverseOrder类方法
返回一个比较器,逆转所有顺序,这个方法适用于TreeSet这类不具有比较性的数据时,反转,可以对现有的比较器进行反转。
new reverseOrder(new diycompare());
如果diycompare是一个从小到大的比较器,那么这个语句就返回了一个从大到小的比较器。
Arrays工具类
这个工具类用于操作数组,里面的方法全是静态的
Arrays.toString(arr):将数组转成String类
Arrays.asList(arr):将数组转成List集合
这个方法如果数组是基本数据类型:比如int,double等,转之后的集合,集合中的元素是这个数组,而不是数组中的元素,也就是说,集合的元素存放的是这个数组的地址。
为什么把数组转成集合??
因为转成集合之后,很多操作变得简单,比如查找,数组情况下必须遍历数组查找,使用集合的方法直接查找就很方便。
把数组转成集合之后,不可以使用集合的增删方法,因为数组的长度是固定的。
将集合转成数组
Collection的toArray方法
class ArrayDemo
{
public static void main(String[] args)
{
ArrayList<String> al=new ArrayList<String>();
al.add("a");
al.add("b");
al.add("c");
al.add("d");
String[] arr=al.toArray(new String[4]);
System.out.println(Arrays.toString(arr));
}
}
转成数组 可以限制对元素的增删。
增强for循环
对Iterator的简化书写
ArrayList<String> al=new ArrayList<String>();
al.add("a");
al.add("b");
al.add("c");
al.add("d");
for(String s : al)
{
System.out.println(s);
}
可变参数:就是一种高效的参数简写形式,不用每一次都建立对象,也不用定义多个数组,也不用定义多个重载方法。
书写方法:int…arr
如果要定义多个参数,可变参数要定义在后面,否则容易出现问题。
class changedWords
{
public static void main(String [] args)
{
show(1,2,3,4);
}
public static void show(int...arr)
{
System.out.println(arr.length);
}
}
System类
System中的成员都是静态的
System类的特有方法:
System.getProperties();获取系统属性信息
System.setProperties();设定系统属性信息
Properties是Hashtable的子类,可以将其中的信息取出来。
Runtime类
该类中没有提供构造函数,所以不能new子类,本类中的方法都是静态的。
Runtime r=Runtime.getRuntime()
Runtime类中有一个方法,获取Java程序运行时的相关对象,这个对象是程序运行时就建立好的。
本类的其他方法:
exec(“command”):执行指定命令,返回一个Process类型的值
Process类下有一个方法destroy()可以杀死所有子进程。
Process q=r.exec("");
q.destroy();
Date类
Date类用于获取时间
获取时间:
Date d=new Date();
这个对象获取了本地时间,但是打印格式是标准格式,如果我们想个性化显示日期,那么还需要用到DateFormat的子类SimpleDateFormat,中的SimpleDateFormat(pattern)方法,该方法的参数pattern是想要显示的格式。
然后SimpleDateFormat新建的子类调用方法format(d),将已经获取到时间的对象d封装成指定格式对象,用字符串来接收,最后打印该字符串就达到了自定义输出格式输出的效果。
public static void main(String[] args)
{
Date d=new Date();
SimpleDateFormat SDF=new SimpleDateFormat("yyyy-MM-dd");
String time =SDF.format(d);
System.out.println(time);
}
Calendar类
根据本地地区建立一个日历,然后利用Calendar.get()方法来获取想要的日期。
Calendar d=Calendar.getInstance();
System.out.println(d.get(Calendar.DAY_OF_WEEK));
Math
数学方法
Math.ceil(num):返回大于指定数据的最小整数
Math.floor(num):返回小于指定数据的最大证书
Math.round(num):四舍五入数据
Math.pow(a,b):计算a的b次方
Math.random():返回一个随机值。
Random随机数
Randrom r=new Random();
r.nexrInt(10);返回一个0到10之间的整数
IO流
IO流中的FileWriiter:文件读写
新建一个FileWriter的对象:
FileWriter fw=new FileWriter("ioDemo.txt");
在初始化fw对象时,会在目录下搜索ioDemo.txt这个文件,如果有,就覆盖它,如果没有,就新建这个文件。
现在,fw这个对象就可以使用FileWriter中对文件的读写方法了。
1.向文件中写入数据:
fw.write("strings");
这条语句,向输入流中写入了字符串strings,然而文件中并没有写入它,所以要用 fw.flush()来刷新输入流,将流中的数据刷新到文件中。
fw.close():刷新流中信息并关闭流,关闭流,并且在之前先调用一次flush()。
IO流异常处理
由于FileWriter对象新建时,给定的文件目录不一定存在等情况,我们需要对输入流进行异常处理。
class ioDemo
{
public static void main(String[] args)
{
FileWriter fw=null;//将对象建立放到try-catch的外面,这样才能让fw对象作用于整个函数。
try
{
fw=new FileWriter("ioDemo.txt");
fw.write("i am iodemo!");
}
catch(IOException e)
{
System.out.println(e.toString());
}
finally
{
try//对colse进行try,因为可能发生new对象的时候就失败的异常
{
if(fw!=null)//空文件不需要关闭,所以if
fw.close();
}
catch(IOException e)
{
System.out.println(e.toString());
}
}
}
}
在文件输入结束后,要关闭流,所以finally代码块中写close。
文件的续写
在上一次的结尾继续写入数据
fw=new FileWriter("ioDemo.txt",true);
第二个Boolean型参数,如果为true就在文件的末尾进行续写。
文件的读取
FileReader会读取文件中的数据,
read方法每次读一个字符,如果读到了就返回一个正数,没有读到(文件结束)就返回-1
FileReader fr=new FileReader("ioDemo.txt");
int ch;
while((ch=fr.read())!=-1)
{
System.out.println((char)ch);
}
read(arr)可以将文件中的数据读取到数组中。
read(arr)会返回一个整数,这个整数代表着一共读取了多少个字符,所以定义一个数组,长度最好是1024的整数倍,在写入的时候,进行长度的限制。
文件的拷贝
就是对一个文件进行读取,然后写入到另一个文件。
class ioDemo
{
public static void main(String[] args)
{
FileWriter fw=null;
FileReader fr=null;
int num;
char []arr=new char[1024];
try
{
fw=new FileWriter("copy1.txt");
fr=new FileReader("ioDemo.txt");
while((num=fr.read(arr))!=-1)
{
fw.write(arr,0,num);
}
}
catch (IOException e)
{
}
finally
{
try
{
fr.close();
fw.close();
}
catch (IOException e)
{
}
}
}
}
BufferedWriter
缓冲区读入,它是一个提高流效率的方法。
因为提高的是流的效率,所以新建BufferedWriter对象时,要传入流作为参数,在缓冲区关闭的时候,关闭的也是流,所以不用再写流的关闭。
FileWriter fw=new FileWriter("ioDemo.txt");
BufferedWriter bw=new BufferedWriter(fw);
bw.write("okokkokok!");
bw.close();
BufferedReader
FileReader fr=new FileReader("ioDemo.txt");
BufferedReader br=new BufferedReader(fr);
String line=null;
while((line=br.readLine())!=null)
System.out.print(line);
br.close();
这个缓冲区提供了一个更佳的方法,readLine(),读取一行数据,当读取到文件结束,返回null;