java中常用工具类equalsdateMatharraylist,Collections多种排序map集合遍历
Posted weixin_ancenhw
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java中常用工具类equalsdateMatharraylist,Collections多种排序map集合遍历相关的知识,希望对你有一定的参考价值。
1、如果使用equals方法判断两个数据是否相等,如果有一方为null,会出现java.lang.NullPointerException空指针异常。采用Objects.equals()工具类,可以避免报错的出现
public static void main(String[] args)
String a=null;
String b="aa";
//空指针异常
System.out.println(a.equals(b));
boolean equals = Objects.equals(a, b);
System.out.println(equals);
2、Date 工具类使用说明,时间比较,时间转换毫秒计算
public static void main(String[] args)
//获取当前时间
getTime();
//计算时间差值,可以将时间转换为毫秒相减,在转换为时间
long currentTimeMillis = System.currentTimeMillis();
//即 1970 年 1 月 1 日 00:00:00 GMT,CST: CST (中国标准时间) 是 UTC+8 时区,比 UTC(协调世界时) 提前 8 个小时与 UTC 的时间偏差可写为 +08:00
System.out.println(new Date(0l));
long time = new Date(0l).getTime();
System.out.println(time);
System.out.println(currentTimeMillis);
System.out.println(time - currentTimeMillis);
//时间比较
Date date1 = new Date();
Date date2 = new Date(System.currentTimeMillis() + 1000 * 60);
compareMethod(date1,date2);
//比较两个时间的大小
private static void compareMethod(Date date1, Date date2)
int compareTo = date1.compareTo(date2);
if (compareTo==1)
System.out.println("date1大于date2");
else if(compareTo==0)
System.out.println("date1等于date2");
else if (compareTo==-1)
System.out.println("date1小于date2");
//获得当前时间毫秒
private static void getTime()
Date date = new Date();
System.out.println(date.getTime());
计算两个日期的天数
public static void main(String[] args) throws ParseException
Scanner scanner = new Scanner(System.in);
System.out.println("请输入日期格式:");
String next = scanner.next();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date yearDate = dateFormat.parse(next);
Date currentDate = new Date();
long diffDate = currentDate.getTime() - yearDate.getTime();
long l = diffDate / 1000 / 60 / 60 / 24;
System.out.println("相差日期:"+l);
利用系统自带的拷贝数组的工具类arrayCopy
//利用系统自带的拷贝数组
public static void main(String[] args) throws ParseException, Exception
String arrTag[]="a","b","c","g";
String arrRes[]="d","e","f","h";
//拷贝数组
arraycopy(arrTag,0,arrRes,2,2);
System.out.println(Arrays.toString(arrRes));
3、Math方法的使用
public static void main(String[] args)
Float a=13.5F;
System.out.println(Math.round(a)); //环绕,四舍五入
System.out.println(Math.floor(a)); //地板,向下取整
System.out.println(Math.ceil(a)); //天花板,向上取整
打印结果
14
13.0
14.0
Process finished with exit code 0
4、集合添加多个数据,打乱集合拍寻
public static void main(String[] args)
ArrayList<String> arrayList = new ArrayList<>();
//向集合中添加多个字符
Collections.addAll(arrayList, "a", "b", "c", "d");
System.out.println(arrayList);
//打乱集合的排序
Collections.shuffle(arrayList);
System.out.println(arrayList);
5、Collections多排序规则
public static void main(String[] args)
ArrayList<String> arrayList = new ArrayList<>();
//向集合中添加多个字符
Collections.addAll(arrayList, "b", "a", "d", "c");
Collections.sort(arrayList);
System.out.println(arrayList);
ArrayList<student> studentArrayList = new ArrayList<>();
studentArrayList.add(new student("A", 23));
studentArrayList.add(new student("B", 13));
studentArrayList.add(new student("bC", 63));
studentArrayList.add(new student("aC", 63));
studentArrayList.add(new student("dC", 63));
//按年龄排序,再按名称第一个字符排序
Collections.sort(studentArrayList, new Comparator<student>()
@Override
public int compare(student o1, student o2)
int i = o2.age - o1.age;
if (i == 0)
i = o1.name.charAt(0) - o2.name.charAt(0);
return i;
);
System.out.println(studentArrayList);
static class student implements Comparable<student>
private String name;
private int age;
public student()
@Override
public String toString()
return "student" +
"name='" + name + '\\'' +
", age=" + age +
'';
public student(String name, int age)
this.name = name;
this.age = age;
public String getName()
return name;
public void setName(String name)
this.name = name;
public int getAge()
return age;
public void setAge(int age)
this.age = age;
@Override
public int compareTo(student o)
return this.getAge() - o.getAge();
6、map集合遍历的几种方式
public static void main(String[] args)
Map<String,String> map=new HashMap<>();
map.put("A","value1");
map.put("B","value2");
map.put("C","value3");
map.put("D","value4");
map.put("E","value5");
// System.out.println(map);
Set<String> keySet = map.keySet();
keySet.forEach(items->
System.out.println(items+map.get(items));
);
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext())
String next = iterator.next();
System.out.println(map.get(next));
for (String s : keySet)
System.out.println(map.get(s));
Set<Map.Entry<String, String>> entrySet = map.entrySet();
for (Map.Entry<String, String> stringStringEntry : entrySet)
System.out.println(stringStringEntry.getKey());
以上是关于java中常用工具类equalsdateMatharraylist,Collections多种排序map集合遍历的主要内容,如果未能解决你的问题,请参考以下文章
java中常用的包类以及包中常用的类方法属性---util包
java中常用的包类以及包中常用的类方法属性----lang包