Java 基于权重按比例分配算法

public class WeightRandomStrategy<K, V extends Number> {
    private TreeMap<Double, K> weightMap = new TreeMap<>();

    public WeightRandomStrategy(List<Pair<K, V>> list) {
        for (Pair<K, V> pair : list) {
            double lastWeight = this.weightMap.size() == 0 ? 0 : this.weightMap.lastKey();
            this.weightMap.put(pair.getValue().doubleValue() + lastWeight, pair.getKey());
        }
    }

    public K random() {
        double randomWeight = this.weightMap.lastKey() * Math.random();
        SortedMap<Double, K> tailMap = this.weightMap.tailMap(randomWeight, false);
        return this.weightMap.get(tailMap.firstKey());
    }
}
List<Pair<String, Integer>> list = new ArrayList<>();
list.add(new ImmutablePair<>("TR", 90));
list.add(new ImmutablePair<>("TX", 10));
WeightRandomStrategy<String, Integer> strategy = new WeightRandomStrategy<>(list);
int a = 0, b = 0;
for (int i = 0; i < 10000; i++) {
    switch (strategy.random()) {
        case "TR":
            a++;
            break;
        case "TX":
            b++;
            break;
        default:
            break;
    }
}
System.out.println("a=" + a + ", b=" + b);
System.out.println("a+b=" + (a + b));

---------------------------------------------------output
a=8993, b=1007
a+b=10000

ThreeTen Extra Java 时间日期处理利器

最近一些需求有对多个时间区间进行判断,例如交集之类的,而2个时间区间可以多达13种情况,实现起来特别容易绕晕,正好找到这样一个工具类可以满足需求,只需要一个方法便可计算出结果,很方便。ThreeTen 的设计里面 Instant 表示时间点,Interval 表示时间段,使用Interval即可对区间进行判断。

例如:判断是否有交集(Overlaps)

Instant startA = Instant.parse("2018-08-01T00:00:00Z");
Instant stopA = Instant.parse("2018-08-10T00:00:00Z");
Instant startB = Instant.parse("2018-07-30T00:00:00Z");
Instant stopB = Instant.parse("2018-08-02T00:00:00Z");

Interval areaA = Interval.of(startA, stopA);
Interval areaB = Interval.of(startB, stopB);
boolean flag1 = areaA.overlaps(areaB);

同样的,还有是否邻接、包含、相等、之前,之后等等。当然,除了Interval,还有别的类可以用,非常强大。官方文档也非常详细。

官网链接:https://www.threeten.org/threeten-extra/index.html

Maven

<dependency>
    <groupId>org.threeten</groupId>
    <artifactId>threeten-extra</artifactId>
    <version>1.4</version>
</dependency>

Ubuntu Linux中连接Android真机调试

首先确保用数据线链接后能识别设备,输入:lsusb,可以看到类似输出

dorole@ubuntu:~$ lsusb
Bus 002 Device 006: ID 0bb4:0c87 High Tech Computer Corp.
Bus 002 Device 004: ID 0402:9665 ALi Corp.
Bus 002 Device 003: ID 04fc:05da Sunplus Technology Co., Ltd
Bus 002 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0951:1643 Kingston Technology
Bus 001 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

这样手机的vendor id和描述就出来了,第一条即是,htc的机器硬件id一般都是0bb4。

在/etc/udev/rules.d/目录下建立50-android.rules规则文件,内容如下:

SUBSYSTEM==”usb”, ATTR{idVendor}==”0bb4″, MODE=”0666″

修改后重启udev服务

dorole@ubuntu:/$ sudo /etc/init.d/udev restart

切换到android sdk的platform-tools目录下,重启adb服务,再输入adb devices就应该能找到设备了。

dorole@ubuntu:~/android-sdk-linux_x86/platform-tools$ sudo ./adb kill-server
dorole@ubuntu:~/android-sdk-linux_x86/platform-tools$ sudo ./adb start-server
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
dorole@ubuntu:~/android-sdk-linux_x86/platform-tools$ sudo ./adb devices
List of devices attached
SH0CDPL00575    device

这样就可以在eclipse中的DDMS来调试android真机了,有些步骤是需要root权限的。