Java生成随机编码

在我们的实际开发中,经常会遇到生成随机编码的问题,编码一般是英文和数字的结合,有点类似于UUID那样,我在网上找了一些生成编码的规则,大都是Random random = new Random()去生成,由于项目中要用到,今天总结了一下生成随机编码的规则,这里会用到根据自增ID生成编码;

创建字典:

首先去创建一个随机数的字典,以后我们要取哪个数都可以到字典里面去取;

<?xml version="1.0" encoding="utf-8"?>
<Import>
<!--第一部分-->
	<Row>
		<KEY>0</KEY>
		<VALUE>F</VALUE>
	</Row>
	<Row>
		<KEY>1</KEY>
		<VALUE>M</VALUE>
	</Row>
	<Row>
		<KEY>2</KEY>
		<VALUE>L</VALUE>
	</Row>
	<Row>
		<KEY>3</KEY>
		<VALUE>1</VALUE>
	</Row>
	<Row>
		<KEY>4</KEY>
		<VALUE>3</VALUE>
	</Row>
	<Row>
		<KEY>5</KEY>
		<VALUE>4</VALUE>
	</Row>
	<Row>
		<KEY>6</KEY>
		<VALUE>5</VALUE>
	</Row>
	<Row>
		<KEY>7</KEY>
		<VALUE>2</VALUE>
	</Row>
	<Row>
		<KEY>8</KEY>
		<VALUE>0</VALUE>
	</Row>
	<Row>
		<KEY>9</KEY>
		<VALUE>8</VALUE>
	</Row>
	<Row>
		<KEY>a</KEY>
		<VALUE>X</VALUE>
	</Row>
	<Row>
		<KEY>b</KEY>
		<VALUE>C</VALUE>
	</Row>
	<Row>
		<KEY>c</KEY>
		<VALUE>J</VALUE>
	</Row>
	<Row>
		<KEY>d</KEY>
		<VALUE>D</VALUE>
	</Row>
	<Row>
		<KEY>e</KEY>
		<VALUE>Q</VALUE>
	</Row>
	<Row>
		<KEY>f</KEY>
		<VALUE>S</VALUE>
	</Row>	

	<!--第二部分-->
	<Multiple>
		<KEY>THREEPARTCODE</KEY>
		<VALUE>6,7,9,A,B,E,G,H,K,N,P,R,T,V,W,Y</VALUE>
	</Multiple>
	<!--第三部分-->
	<Multiple>
		<KEY>FOURPARTCODE</KEY>
		<VALUE>0,1,2,3,4,5,6,7,8,9,Q,W,E,R,T,Y,P,A,S,D,F,G,H,J,K,L,X,C,V,B,N,M</VALUE>
	</Multiple>
	
	
</Import>

先解释一下字典的第一部分,因为我们的ID是自增的,有可能会很多,所以我们要把ID转换成16进制的,上面的一部分就是对应的所有的16进制。而第二部分则是我们为16进制的编码第一部分补上一位的编码,第二部分的编码不属于16进制,这样的话我们就避免了前两部分重复,最后一部分是把所缺的编码给补齐;

	private static Map<Character, Character> map = new HashMap<Character, Character>();
	private static final String PATH = "/back-product/back-product-business/codeRegular.xml";
	private static String threePart[] = null; //第二部分
	private static String fourPart[] = null; //第三部分
	private static Map<String, String> twoPart = new HashMap<String, String>();  //第一部分
	private static Logger log = LoggerFactory.getLogger(CodeUtil.class);
	static {
		init();
	}

	private static void init() {
		try {
			String globalPath = System.getProperty("global.config.path");
			InputStream is = new FileInputStream(new File(globalPath + PATH));
			File configDir = new File(globalPath + PATH);
			SAXReader reader = new SAXReader();
			//通过流读取
			Document document = reader.read(is);
			//获得文档的根节点
			Element element = document.getRootElement();
			// 得到所有带Row的行数
			Iterator<Element> iterator = element.elementIterator("Row");
			while (iterator.hasNext()) { //遍历把值存入twoPart里面
				Element ele = iterator.next();
				twoPart.put(ele.element("KEY").getTextTrim(),
						ele.element("VALUE").getTextTrim());
			}
			Iterator<Element> iterator2 = element.elementIterator("Multiple");
			while (iterator2.hasNext()) {
				Element ele = iterator2.next();
				if (ele.element("KEY").getTextTrim().equals("THREEPARTCODE")) {
					// three
					threePart = ele.element("VALUE").getTextTrim().split(",");
				} else {
					// four
					fourPart = ele.element("VALUE").getTextTrim().split(",");
				}
			}
			is.close();
		} catch (Exception e) {
			log.error(e.getMessage());
		}
	}

	/**
	 * 获取产品编码
	 * 
	 * @param type
	 *            产品类型
	 * @param ProductId
	 *            产品id
	 * @return
	 */
	public static String getProductCode(Integer type, Long productId) {
		if (type == null || productId == null) {
			return null;
		}
		StringBuffer sb = new StringBuffer();
		StringBuffer encodeProductId = new StringBuffer();
		sb.append(type);
		//转16进制
		String hexString = Integer.toHexString(productId.intValue());
		char[] charArray = hexString.toCharArray();
		for (char c : charArray) {
			sb.append(twoPart.get(String.valueOf(c))); //添加第一部分
		}
		Random random = new Random();
		int nextInt = random.nextInt(threePart.length);
		sb.append(threePart[nextInt]);  //补上第二部分
		int temp = sb.length();
		for (int i = 0; i < 10 - temp; i++) { //补齐代码
			sb.append(fourPart[random.nextInt(fourPart.length)]);
		}
		return sb.toString();
	}

上面就是根据ID生成不重复编码的规则了;下面会有一个生成随机编码的规则,但是并不能保证其不重复;

/*/
	 * 生成随机编码
	 */
	public static String getWarehouseCode(int count){
		StringBuffer sb = new StringBuffer();
		Random random = new Random(); //生成随机数
		for(int i=0;i<count;i++){
			sb.append(fourPart[random.nextInt(fourPart.length)]);
		}
		return sb.toString();
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值