java中实用类收集与示例

这里用于搜集记录一些实用类的使用示例,便于忘记后查阅。
后续会不断增加
目前加入的有:

  • java数据排序示例
  • java更精确计时示例
  • CountDownTimer类使用示例

java数据排序示例

Array、ArrayList String数据排序示例

1
2
3
4
5
6
7
8
9
10
11
12

//Array 排序
String[] fruits1 = new String[] {"Pineapple","Apple", "Orange", "Banana"};
Arrays.sort(fruits1);

//ArrayList 排序
List<String> fruits2 = new ArrayList<String>();
fruits2.add("Pineapple");
fruits2.add("Apple");
fruits2.add("Orange");
fruits2.add("Banana");
Collections.sort(fruits2);

打印结果:

1
2
3
4
1 : Apple
2 : Banana
3 : Orange
4 : Pineapple

Object集合数据排序示例

可借助以下两个接口:

  • java.lang.Comparable接口重写int compareTo(Fruit compareFruit);方法
  • java.util.Comparator接口重写int compare(T o1, T o2);方法

以下示例(Comparable比较quantity,Comparator比较fruitName):

  • Fruit比较对象

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    import java.util.Comparator;

    public class Fruit implements Comparable<Fruit>{

    private String fruitName;
    private String fruitDesc;
    private int quantity;

    public Fruit(String fruitName, String fruitDesc, int quantity) {
    super();
    this.fruitName = fruitName;
    this.fruitDesc = fruitDesc;
    this.quantity = quantity;
    }

    public String getFruitName() {
    return fruitName;
    }
    public void setFruitName(String fruitName) {
    this.fruitName = fruitName;
    }
    public String getFruitDesc() {
    return fruitDesc;
    }
    public void setFruitDesc(String fruitDesc) {
    this.fruitDesc = fruitDesc;
    }
    public int getQuantity() {
    return quantity;
    }
    public void setQuantity(int quantity) {
    this.quantity = quantity;
    }

    public int compareTo(Fruit compareFruit) {

    int compareQuantity = compareFruit.getQuantity();

    //升序
    return this.quantity - compareQuantity;

    //降序
    //return compareQuantity - this.quantity;

    }

    public static Comparator<Fruit> FruitNameComparator
    = new Comparator<Fruit>() {

    public int compare(Fruit fruit1, Fruit fruit2) {

    String fruitName1 = fruit1.getFruitName().toUpperCase();
    String fruitName2 = fruit2.getFruitName().toUpperCase();

    //升序
    return fruitName1.compareTo(fruitName2);

    //降序
    //return fruitName2.compareTo(fruitName1);
    }

    };
    }
  • 两种排序操作示例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35

    //测试数据
    Fruit[] fruits = new Fruit[4];

    Fruit pineappale = new Fruit("Pineapple", "Pineapple description",70);
    Fruit apple = new Fruit("Apple", "Apple description",100);
    Fruit orange = new Fruit("Orange", "Orange description",80);
    Fruit banana = new Fruit("Banana", "Banana description",90);

    fruits[0]=pineappale;
    fruits[1]=apple;
    fruits[2]=orange;
    fruits[3]=banana;

    //Comparator方式排序
    Arrays.sort(fruits, Fruit.FruitNameComparator);

    /*
    *排序结果
    fruits 1 : Apple, Quantity : 100
    fruits 2 : Banana, Quantity : 90
    fruits 3 : Orange, Quantity : 80
    fruits 4 : Pineapple, Quantity : 70
    */

    //Comparable方式排序
    Arrays.sort(fruits)

    /*
    *排序结果
    fruits 1 : Pineapple, Quantity : 70
    fruits 2 : Orange, Quantity : 80
    fruits 3 : Banana, Quantity : 90
    fruits 4 : Apple, Quantity : 100
    */

java更精确计时示例

1
2
3
4
5
6
 //获取开始计时的值
long begin = System.nanoTime();//仅可用于计时,可能为负,也可能是未来的时间。不等于System.currentTime()
//需要统计耗时性能的代码
...
//计算精度是纳秒级别的时差
System.out.println(System.nanoTime() - begin);

CountDownTimer 帮你正确实现倒计时

1
2
3
4
5
6
7
8
9
10
11
12
public class TimeCount extends CountDownTimer {    

public TimeCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {// 计时完毕
}
@Override
public void onTick(long millisUntilFinished) {// 计时过程 间隔时间执行
}
}

CountDownTimer的使用示例:

1
2
TimeCount countDownTime=new TimeCount(20000, 1000);//总时长20s结束后调用onFinish(),每隔1s执行一次onTick
countDownTime.start();