JAVA 取自增序号(可用于实体类排序)
1 //import java.util.concurrent.atomic.AtomicInteger; //引入 2 3 4 //将List按照某一字段升序排序 5 List<SrmSfdBidItemLadderVO> itemRank=itemRank.stream().sorted(Comparator.comparing(SrmSfdBidItemLadderVO::getLadderNoTaxPrice)).collect(Collectors.toList()); 6 7 //设置自增值 8 AtomicInteger index = new AtomicInteger(0); 9 10 //设置序号 11 itemRank.stream().forEach(ro->{ 12 ro.setLadderBidRank(index.incrementAndGet()); 13 });
//输出展示
AtomicInteger index = new AtomicInteger(0); System.out.println(index.incrementAndGet()); System.out.println(index.incrementAndGet()); System.out.println(index.incrementAndGet()); System.out.println(index.incrementAndGet());