java—从列表中提取小于1%的百分比值

xqkwcwgp  于 2021-06-29  发布在  Java
关注(0)|答案(3)|浏览(556)

我有一份 DataModel pojo类。 percentage 字段处于 String 格式:

public class DataModel implements Serializable {
    private String percentage;

    public String getPercentage() {
        return this.percentage;
    }

    public void setPercentage(String percentage) {
        this.percentage = percentage;
    }
}

数据示例:

List<DataModel> list = new ArrayList();
DataModel d1 = new DataModel();
d1.setPercentage("20.34%");
DataModel d2 = new DataModel();
d2.setPercentage("30.04%");
DataModel d3 = new DataModel();
d3.setPercentage("10.34%");
DataModel d4 = new DataModel();
d4.setPercentage("0.34%");
list.add(d1);
list.add(d2);
list.add(d3);
list.add(d4);

我可以按以下相反顺序对列表排序:

list = list.sort(comparing(r ->
        Double.parseDouble(
                r.getPercentage().substring(0, r.getPercentage().length() - 1)),
        Comparator.reverseOrder()));

List<DataModel> fullList = new ArrayList();
List<DataModel> lessList = new ArrayList();

我需要填写以下清单:

fullList ---> percentage > 1 (20.04%,20.34%,10.34%)
lessList ---> percentage < 1 (0.34%)
fdbelqdn

fdbelqdn1#

我建议你使用这个百分比作为双精度字符串,这样你就可以比较容易。您还可以为该对象创建一个comparator类。你可以这样做:

public class DataModelComparator implements Comparator<DataModel> {
    @Override
    public int compare(DataModel m1, DataModel m2) {
        return m1.getPercentage().compareTo(m2.getPercentage());
    }
}

所以在这里你将能够比较这个物体的百分比。你可以这样使用这个比较器:

ArrayList<DataModel> models = new ArrayList<>();
DataModel d1 = new DataModel(2.56);
DataModel d2 = new DataModel(45.5);
models.add(d1, d2);
// now you have two objects created
// so if you want to sort them by their
// percentages you can do it like this:
Collections.sort(models, new DataModelComparator());

在内部,如果对象相等,comparator类将发送0,如果对象小于,则发送-1,如果对象大于,则发送1。
在这之后,你的列表将按它的百分比排序,你可以得到你需要的数据与一些if语句或左右。

wmomyfyw

wmomyfyw2#

最好将百分比字段转换为 double 使比较它们和添加 toString 方法以保持视图不变。然后您可以轻松访问和表示所需的数据:

public class DataModel implements Serializable {
    private double percentage;

    public DataModel(double percentage) {
        this.percentage = percentage;
    }

    public double getPercentage() {
        return percentage;
    }

    @Override
    public String toString() {
        return percentage + "%";
    }
}
public static void main(String[] args) {
    List<DataModel> list = List.of(
            new DataModel(20.34),
            new DataModel(30.04),
            new DataModel(10.34),
            new DataModel(0.34));

    System.out.println(list); // [20.34%, 30.04%, 10.34%, 0.34%]

    List<DataModel> fullList = list.stream()
            .filter(dm -> dm.getPercentage() > 1)
            .collect(Collectors.toList());

    System.out.println(fullList); // [20.34%, 30.04%, 10.34%]

    List<DataModel> lessList = list.stream()
            .filter(dm -> dm.getPercentage() < 1)
            .collect(Collectors.toList());

    System.out.println(lessList); // [0.34%]
}

另请参阅:如何有效地查找数组中的重复元素?

jogvjijk

jogvjijk3#

Map<Boolean, List<DataModel>> results = list.stream()
        .collect(Collectors.partitioningBy(
                p -> p.getPercentage().startsWith("0.")));

List<DataModel> fullList = results.get(false);
List<DataModel> lessList = results.get(true);

相关问题