TreeSet<T> 简单实现



package com.mypack.ds;

import java.util.Random;

public class TreeSet<T>
{

/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
TreeSet<Integer> tree = new TreeSet<Integer>();
for (int i = 0; i < 20; i ++)
{
tree.add(new Random().nextInt(100));
}
System.out.println(tree);
}

private Entry<T> root;

private volatile Entry<T> entry;

public TreeSet()
{

}

public void add(T node)
{
if (root == null)
{
entry = root;
final Entry<T> tmp = entry;
synchronized (TreeSet.class)
{
if (tmp == null)
{
root = new Entry<T>(node);
}
}
}
else
{
insertNode(root, node);
}
}

public String toString()
{
StringBuilder sb = new StringBuilder();

travel(root, sb);

return sb.toString();
}

void travel(Entry<T> root, StringBuilder sb)
{
if (root.left == null)
{
sb.append(root.value).append(",");
}
else
{
travel(root.left, sb);
}
sb.append(root.value).append(",");
if (root.right == null)
{
sb.append(root.value).append(",");
}
else
{
travel(root.right, sb);
}
}

synchronized void insertNode(Entry<T> obj, T node)
{
Comparable<T> c = (Comparable<T>)node;

// right
if (c.compareTo(obj.value) > 0)
{
if (obj.right != null)
{
insertNode(obj.right, node);
}
else
{
Entry<T> en = new Entry<T>(node);
obj.right = en;
}
}
// left
else
{
if (obj.left == null)
{
Entry<T> en = new Entry<T>(node);
obj.left = en;
}
else
{
insertNode(obj.left, node);
}
}
}

static class Entry<T>
{
Entry left;
T value;
Entry right;

Entry(T value)
{
this.value = value;
}
}

}


private void processData() { this.filterReqsData(); if (ValueCheckUtil.isNullORZeroLength(this.onhandReq)) { return; } // 按实际结存维度处理查询条件 OnhandDimVO[] dimvos = new OnhandDimVO[this.onhandReq.length]; for (int i = 0; i < this.onhandReq.length; i++) { dimvos[i] = this.onhandReq[i].getOnhandDim(); } dimvos = OnhandVOTools.getRealOnhandDim(new ICBSContext().getInvInfo(), dimvos); String orgpickflag = dimvos[0].getVfree9(); // 收集zytdjh和vbatchcode值 List<Pair<String, String>> vfreeValues = Arrays.stream(dimvos) .filter(Objects::nonNull) .map(dimvo -> { Pair<String, String> pair = new Pair<>(dimvo.getVfree8(), dimvo.getVbatchcode()); dimvo.setVfree8(null); dimvo.setVfree9(null); return pair; }) .collect(Collectors.toList()); OnhandVO[] handvos = this.getOnhandRes().loadOnhandRes(dimvos); if (ValueCheckUtil.isNullORZeroLength(handvos)) { return; } // 5. 处理排序逻辑 if("Y".equals(orgpickflag)) { List<OnhandVO> resultList = new ArrayList<>(); // 先处理需要特殊排序的记录 for (Pair<String, String> pair : vfreeValues) { String zytdjh = pair.getFirst(); String vbatchcode = pair.getSecond(); if (!StringUtil.isSEmptyOrNull(zytdjh) && StringUtil.isSEmptyOrNull(vbatchcode)) { // 1. 优先处理完全匹配zytdjh的记录 List<OnhandVO> exactMatches = Arrays.stream(handvos) .filter(vo -> !StringUtil.isSEmptyOrNull(vo.getVbatchcode()) && vo.getVbatchcode().equals(zytdjh)) .collect(Collectors.toList()); resultList.addAll(exactMatches); // 2. 处理MTS开头的批次 List<OnhandVO> mtsBatches = Arrays.stream(handvos) .filter(vo -> !StringUtil.isSEmptyOrNull(vo.getVbatchcode()) && vo.getVbatchcode().startsWith("MTS") && !exactMatches.contains(vo)) .sorted((vo1, vo2) -> { UFDate dinbounddate1 = getInboundDate(vo1); UFDate dinbounddate2 = getInboundDate(vo2); if (dinbounddate1 != null && dinbounddate2 != null) { return dinbounddate1.compareTo(dinbounddate2); } return 0; }) .collect(Collectors.toList()); resultList.addAll(mtsBatches); } } // 添加未处理的记录(包括空批次号) List<OnhandVO> allProcessed = new ArrayList<>(resultList); allProcessed.addAll(Arrays.stream(handvos) .filter(vo -> !resultList.contains(vo)) .collect(Collectors.toList())); handvos = allProcessed.toArray(new OnhandVO[0]); } this.reqsmatch = new DimMatchUtil<T>(this.onhandReq, OnhandDimVO.getDimContentFields(), new String[] { OnhandDimVO.PK_GROUP, OnhandDimVO.PK_ORG, OnhandDimVO.CWAREHOUSEID, OnhandDimVO.CMATERIALOID, OnhandDimVO.CMATERIALVID, }); this.mapResults = new HashMap<T, OnhandBalanceResult<T>>(); handvos = this.preAllocHandRes(handvos); if (ValueCheckUtil.isNullORZeroLength(handvos)) { this.reqsmatch = null; return; } this.processOnhandData(handvos); } private UFDate getInboundDate(OnhandVO vo) { if (!StringUtil.isSEmptyOrNull(vo.getPk_batchcode())) { BatchcodeVO[] batchcodeVOS = new VOQuery<BatchcodeVO>(BatchcodeVO.class) .query(new String[]{vo.getPk_batchcode()}); if (batchcodeVOS.length > 0) { return batchcodeVOS[0].getDinbounddate(); } } return null; } private static class Pair<K, V> { private final K first; private final V second; public Pair(K first, V second) { this.first = first; this.second = second; } public K getFirst() { return first; } public V getSecond() { return second; } }
最新发布
06-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值