您的位置:首页 > 编程语言

Arrays工具类中的toString/binarySearch/sort/asList示例代码

2017-05-10 11:05 411 查看
****************************************测试类****************************************
package com.**.equalsDemo;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
* @author Ckinghan
*  @描述:Arrays工具类的常用方法
*/
public class ArraysDemo {

public static void main(String[] args) {
Integer[] integers = {1,3,35,32,3,2,34,23,42,234,223,34,234,24,34};
/**
* Arrays工具类的asList方法,将数组转换为list集合
*/
List<Integer> list = Arrays.asList(integers);
for(Integer integer : list){
System.out.println("数组通过Arrays.asList转换为list集合输出结果:"+integer);
}

System.out.println("------------------------------------------------我是分隔线------------------------------------------------");

/**
* Arrays.sort可以对数组进行自然排序(从大到小)
*/
Arrays.sort(integers);

/**
* Arrays.toString可以将数组转换为String字符串,注意,它返回的值与数组.toString返回的值不同,
* 数组.toString返回的值是一个hashCode地址
* Arrays.toString返回的是数组的值
*/
String arrayString = Arrays.toString(integers);
System.out.println("对数组进行自然排序后的结果并使用Arrays.toString转换后的结果为:"+arrayString);
System.out.println("数组integers.toString返回的值是:"+integers.toString());

System.out.println("------------------------------------------------我是分隔线------------------------------------------------");

/**
* Arrays.binarySearch可以查找数组中值存在的索引位置
* 查找的方式为折半查询(也称为二分查找法,叫法不同,都是一个意思)
* 这种查找的方式速度很快,但有个前提,数组必须是有序的。
*/
Integer valIndex = Arrays.binarySearch(integers, 35);
System.out.println("通过Arrays.binarySearch折半算法查询到的索引位置为:"+valIndex);

System.out.println("------------------------------------------------我是分隔线------------------------------------------------");

/**
* 注意:如果自己创建的类想使用Arrays.sort排序时,必须实现Compareble接口中的comparaTo接口
*/
Entity[] entities = {new Entity(1, "employeeInfo", new Date()),
new Entity(2, "employeeInfo", new Date()),
new Entity(3, "employeeInfo", new Date()),
new Entity(1, "employeeInfo", new Date())};
Arrays.sort(entities);
for(Entity entity : entities){
System.out.println(entity);
}
}

}

****************************************实体类****************************************
package com.**.equalsDemo;

import java.util.Date;

public class Entity implements Comparable<Entity>{

public Integer id;

public String tableName;

public Date createTime;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getTableName() {
return tableName;
}

public void setTableName(String tableName) {
this.tableName = tableName;
}

public Date getCreateTime() {
return createTime;
}

public void setCreateTime(Date createTime) {
this.createTime = createTime;
}

@Override
public String toString() {
return "entity [id=" + id + ", tableName=" + tableName + ", createTime=" + createTime + "]";
}

public Entity(Integer id, String tableName, Date createTime) {
super();
this.id = id;
this.tableName = tableName;
this.createTime = createTime;
}

public Entity() {
super();
}

/**
* 如果想在set集合中,对比存放对象的具体内容,须要重写equals
*/
public boolean equals(Object obj) {
if(obj instanceof Entity){
Entity entity = (Entity)obj;
if(entity.id == this.id && this.tableName != null && this.tableName.equals(entity.tableName) && (this.createTime.compareTo(entity.getCreateTime())) == 0){
return true;
}
}
return false;
}

/**
* 还须要重写hashCode,因为equals在判断是否相等时,还会判断hashCode
*/
public int hashCode() {
return 100000;
}

/**
* 如果想使用Arrays中的sort排序,必须重写compareTo方法,否则会报错
*/
@Override
public int compareTo(Entity o) {
Integer val = this.id - o.id;
if(val == 0 && this.tableName != null){
val = this.tableName.compareTo(o.tableName);
if(val == 0){
val = this.createTime.compareTo(o.createTime);
}
}
return val;
}

}

****************************************执行结果****************************************
数组通过Arrays.asList转换为list集合输出结果:1
数组通过Arrays.asList转换为list集合输出结果:3
数组通过Arrays.asList转换为list集合输出结果:35
数组通过Arrays.asList转换为list集合输出结果:32
数组通过Arrays.asList转换为list集合输出结果:3
数组通过Arrays.asList转换为list集合输出结果:2
数组通过Arrays.asList转换为list集合输出结果:34
数组通过Arrays.asList转换为list集合输出结果:23
数组通过Arrays.asList转换为list集合输出结果:42
数组通过Arrays.asList转换为list集合输出结果:234
数组通过Arrays.asList转换为list集合输出结果:223
数组通过Arrays.asList转换为list集合输出结果:34
数组通过Arrays.asList转换为list集合输出结果:234
数组通过Arrays.asList转换为list集合输出结果:24
数组通过Arrays.asList转换为list集合输出结果:34
------------------------------------------------我是分隔线------------------------------------------------
对数组进行自然排序后的结果并使用Arrays.toString转换后的结果为:[1, 2, 3, 3, 23, 24, 32, 34, 34, 34, 35, 42, 223, 234, 234]
数组integers.toString返回的值是:[Ljava.lang.Integer;@4c76ec2d
------------------------------------------------我是分隔线------------------------------------------------
通过Arrays.binarySearch折半算法查询到的索引位置为:10
------------------------------------------------我是分隔线------------------------------------------------
entity [id=1, tableName=employeeInfo, createTime=Wed May 10 11:00:45 CST 2017]
entity [id=1, tableName=employeeInfo, createTime=Wed May 10 11:00:45 CST 2017]
entity [id=2, tableName=employeeInfo, createTime=Wed May 10 11:00:45 CST 2017]
entity [id=3, tableName=employeeInfo, createTime=Wed May 10 11:00:45 CST 2017]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息