判断某个值是否在给出的区间列表中

判断某个值是否在给出的区间列表中

 

需求说明:

今天在做解析用户上传的Excel文件时,遇到一个问题需要去判断解析出来的一行的投资金额是否落在页面录入的某个投资金额区间内,Excel文件的格式如下:

页面录入的环节如下:

 

待解决问题:

在解析完Excel文件后,循环解析出来的列表,每一行都是一个对象,取出对象的投资金额,这时候是可以拿到页面录入的产品投资金额区间列表,两个重要的数据都已经获取到,那么怎么取判断这个投资金额会落在某个投资区间内呢或者投资金额不会落在任何一个区间。

 

解决方案:

可以设置一个count计数器来累加这个投资金额是否在这个区间列表中的每一个区间中,然后判断这个count计算器的值,如果等于0,说明这个投资金额没有落在任何一个投资区间中,那么这个就是不合规数据,相反只要count是大于0 的数,那么该投资金额就是符合规范的数据。

 

代码实现;

测试类:

package com.qdfae.jdk.collections;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import com.qdfae.jdk.domain.ListingTradeInvestVo;

/**
 * 判断某个值是否在给出的区间列表中
 *
 * @author hongwei.lian
 * @date 2018年8月6日 下午7:15:26
 */
public class TradeInvestListTest {
	
	/**
	 * 判断某个值是否在给出的区间列表中
	 *
	 * @author hongwei.lian
	 * @date 2018年8月6日 下午7:15:48
	 */
	@Test
	public void test1() {
		//-- 存放投资金额区间列表
		List<ListingTradeInvestVo> tradeInvestList = new ArrayList<>();
		tradeInvestList.add(new ListingTradeInvestVo(new BigDecimal("10.00"), new BigDecimal("20.00")));
		tradeInvestList.add(new ListingTradeInvestVo(new BigDecimal("30.00"), new BigDecimal("50.00")));
		tradeInvestList.add(new ListingTradeInvestVo(new BigDecimal("60.00"), new BigDecimal("100.00")));
		//-- 解析Excel得出的每一行投资金额
		BigDecimal tradeMoney = new BigDecimal("45");
		//-- 在认购区间列表的个数
		int inCount = 0;
		for (int j = 0; j < tradeInvestList.size(); j++) {
			ListingTradeInvestVo tradeInvest = tradeInvestList.get(j);
			boolean moreEqualCompare = tradeMoney.compareTo(tradeInvest.getInvestAmountMin()) >= 0;
			boolean lessCompare = tradeMoney.compareTo(tradeInvest.getInvestAmountMax()) < 0;
			boolean lessEqualCompare = tradeMoney.compareTo(tradeInvest.getInvestAmountMax()) <= 0;
			if (j == tradeInvestList.size()-1) {
				//-- 如果是最后一组,则使用lessEqualCompare
				if (moreEqualCompare && lessEqualCompare) {
					inCount++;
				}
			} else {
				//-- 使用lessCompare
				if (moreEqualCompare && lessCompare) {
					inCount++;
				}
			}
		}
		if (inCount == 0) {
			System.out.println(tradeMoney + "不在认购区间内");
		}
		if (inCount > 0) {
			System.out.println(tradeMoney + "在认购区间内");
		}
	}

}

实体类:

package com.qdfae.jdk.domain;

import java.io.Serializable;
import java.math.BigDecimal;

import org.springframework.format.annotation.DateTimeFormat;

/**
 * 项目交易起投金额个对应年化收益率
 *
 * @author hongwei.lian
 * @date 2018年4月13日 上午10:35:48
 */
public class ListingTradeInvestVo implements Serializable {

	private static final long serialVersionUID = 1273211355896054195L;
	
	/**
	 * 唯一id,自增主键
	 */
	private Integer id;
	/**
	 * 挂牌产品Id
	 */
	private Integer projectId;
	/**
	 * 起投金额
	 */
	private java.math.BigDecimal investAmountMin;
	/**
	 * 对应起投金额的上限
	 */
	private java.math.BigDecimal investAmountMax;
	/**
	 * 对应年化收益率,没有乘以%
	 */
	private java.math.BigDecimal investProfit;
	/**
	 * 创建时间
	 */
	@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")  
	private java.util.Date createTime;
	/**
	 * 创建操作人Id
	 */
	private Integer createOperatorId;
	/**
	 * 更新时间
	 */
	@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")  
	private java.util.Date updateTime;
	/**
	 * 更新操作人Id
	 */
	private Integer updateOperatorId;
	
	public ListingTradeInvestVo() {}
	
	public ListingTradeInvestVo(Integer id, Integer projectId, BigDecimal investAmountMin, BigDecimal investAmountMax, BigDecimal investProfit) {
		this.id = id;
		this.projectId = projectId;
		this.investAmountMin = investAmountMin;
		this.investAmountMax = investAmountMax;
		this.investProfit = investProfit;
	}
	
	public ListingTradeInvestVo(BigDecimal investAmountMin, BigDecimal investAmountMax) {
		super();
		this.investAmountMin = investAmountMin;
		this.investAmountMax = investAmountMax;
	}

	public void setId(Integer value) {
		this.id = value;
	}
	public Integer getId() {
		return this.id;
	}
	public void setProjectId(Integer value) {
		this.projectId = value;
	}
	public Integer getProjectId() {
		return this.projectId;
	}
	public void setInvestAmountMin(java.math.BigDecimal value) {
		this.investAmountMin = value;
	}
	public java.math.BigDecimal getInvestAmountMin() {
		return this.investAmountMin;
	}
	public java.math.BigDecimal getInvestAmountMax() {
		return investAmountMax;
	}
	public void setInvestAmountMax(java.math.BigDecimal investAmountMax) {
		this.investAmountMax = investAmountMax;
	}
	public void setInvestProfit(java.math.BigDecimal value) {
		this.investProfit = value;
	}
	public java.math.BigDecimal getInvestProfit() {
		return this.investProfit;
	}
	public void setCreateTime(java.util.Date value) {
		this.createTime = value;
	}
	public java.util.Date getCreateTime() {
		return this.createTime;
	}
	public void setCreateOperatorId(Integer value) {
		this.createOperatorId = value;
	}
	public Integer getCreateOperatorId() {
		return this.createOperatorId;
	}
	public void setUpdateTime(java.util.Date value) {
		this.updateTime = value;
	}
	public java.util.Date getUpdateTime() {
		return this.updateTime;
	}
	public void setUpdateOperatorId(Integer value) {
		this.updateOperatorId = value;
	}
	public Integer getUpdateOperatorId() {
		return this.updateOperatorId;
	}
	@Override
	public String toString() {
		return "ListingTradeInvestVo [id=" + id + ", projectId=" + projectId + ", investAmountMin=" + investAmountMin
				+ ", investProfit=" + investProfit + ", createTime=" + createTime + ", createOperatorId="
				+ createOperatorId + ", updateTime=" + updateTime + ", updateOperatorId=" + updateOperatorId + "]";
	}
}

测试结果:

输入投资金额为45的结果:

输入投资金额为55的结果:

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您的问题是如何在列表判断每个元素的个数,并取出某区间数量。 首先,我们可以使用 Python 的 Counter 函数来统计每个元素在列表出现的次数。具体代码如下: ``` python from collections import Counter lst = [1, 2, 3, 3, 3, 4, 4, 5] count = Counter(lst) print(count) ``` 输出结果为: ``` Counter({3: 3, 4: 2, 1: 1, 2: 1, 5: 1}) ``` 这里我们可以看到,元素 3 在列表出现了 3 次,元素 4 出现了 2 次,元素 1、2、5 分别出现了 1 次。 接下来,如果您取出某区间内的数量,可以使用 Counter 对象的 most_common 函数。该函数可以返回出现次数最多的前 n 个元素以及它们的出现次数。具体代码如下: ``` python from collections import Counter lst = [1, 2, 3, 3, 3, 4, 4, 5] count = Counter(lst) n = 2 # 取出出现次数前的元素 top_n = count.most_common(n) # 返回一个列表,包含前 n 个元素以及它们的出现次数 print(top_n) ``` 输出结果为: ``` [(3, 3), (4, 2)] ``` 这里我们取出了出现次数前的元素,即元素 3 和元素 4,它们分别出现了 3 次和 2 次。如果您取出某个区间内的数量,只需要对列表进行切片即可。例如,如果您取出出现次数第 2 至第 4 多的元素以及它们的出现次数,可以使用以下代码: ``` python from collections import Counter lst = [1, 2, 3, 3, 3, 4, 4, 5] count = Counter(lst) n = 4 # 取出前 4 个元素 m = 1 # 取出从第 2 个元素开始到第 4 个元素的数量 top_n = count.most_common(n)[m:m+3] # 取出从第 2 个元素开始到第 4 个元素的数量 print(top_n) ``` 输出结果为: ``` [(4, 2), (1, 1), (2, 1)] ``` 这里我们取出了出现次数第 2 至第 4 多的元素以及它们的出现次数,即元素 4、1、2,它们分别出现了 2 次、1 次、1 次。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值