###################################################
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
@AllArgsConstructor
@NoArgsConstructor
@Data
/**
* @author 1
*/
@Accessors(chain = true)
public class SortEntity implements Comparable<SortEntity> {
private int id;
private String ratio;
private int age;
private String percentage;
@Override
public int compareTo(SortEntity o) {
// 按照age倒序,id正序排序
if (this.getAge() > o.getAge()) {
return -1;
} else if (this.getAge() < o.getAge()) {
return 1;
} else {
if (this.getAge() > o.getAge()) {
return 1;
} else {
return -1;
}
}
}
}
###########################################################
import com.csw.mysqldate.entity.SortEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class SortTest {
private static final Logger logger = LoggerFactory.getLogger(SortTest.class);
public static void main(String[] args) {
List<SortEntity> sortEntityList = new ArrayList<>();
SortEntity sortEntity1 = new SortEntity();
sortEntity1.setId(1);
sortEntity1.setRatio("34.23");
sortEntity1.setAge(1);
sortEntity1.setPercentage("34.23%");
sortEntityList.add(sortEntity1);
SortEntity sortEntity2 = new SortEntity();
sortEntity2.setId(2);
sortEntity2.setRatio("33.23");
sortEntity2.setAge(3);
sortEntity2.setPercentage("33.24%");
sortEntityList.add(sortEntity2);
SortEntity sortEntity3 = new SortEntity();
sortEntity3.setId(3);
sortEntity3.setRatio("33.23");
sortEntity3.setAge(2);
sortEntity3.setPercentage("33.33%");
sortEntityList.add(sortEntity3);
SortEntity sortEntity4 = new SortEntity();
sortEntity4.setId(4);
sortEntity4.setRatio("35.23");
sortEntity4.setAge(3);
sortEntity4.setPercentage("35.23%");
sortEntityList.add(sortEntity4);
SortEntity sortEntity5 = new SortEntity();
sortEntity5.setId(5);
sortEntity5.setRatio("36.23");
sortEntity5.setAge(5);
sortEntity5.setPercentage("36.23%");
sortEntityList.add(sortEntity5);
logger.info("" + "//原始//////////////////排序前");
for (SortEntity sortEntity : sortEntityList) {
logger.info("" + sortEntity);
}
//【最新方式】
//按照时间字段降序排序
//relatedRecommendationVos.sort(Comparator.comparing(RelatedRecommendationVo::getReleaseTime).reversed());
//多字段
// resList.sort(Comparator.comparing(UrbanPartyBuildColumnListPcVo::getLevel).thenComparing(UrbanPartyBuildColumnListPcVo::getParentId).thenComparing(UrbanPartyBuildColumnListPcVo::getSequence));
/**
* java里面写比较规则,多条件
*/
sortEntityList.sort((Comparator) (o1, o2) -> {
SortEntity stu1 = (SortEntity) o1;
SortEntity stu2 = (SortEntity) o2;
if (!stu2.getRatio().equals(stu1.getRatio())) {
return stu2.getRatio().compareTo(stu1.getRatio());
} else if (!stu2.getPercentage().equals(stu1.getPercentage())) {
return stu1.getPercentage().compareTo(stu2.getPercentage());
} else {
return (stu2.getId() + "").compareTo(stu1.getId() + "");
}
});
logger.info("" + "//String类型排序////////////////////排序后");
for (SortEntity sortEntity : sortEntityList) {
logger.info("" + sortEntity);
}
/**
* 用实体里面的比较规则
*/
sortEntityList.sort(SortEntity::compareTo);
logger.info("" + "//实体int类型排序////////////////////排序后");
for (SortEntity sortEntity : sortEntityList) {
logger.info("" + sortEntity);
}
/**
* java里面写比较规则
* 单条件
*/
sortEntityList.sort((Comparator) (o1, o2) -> {
SortEntity stu1 = (SortEntity) o1;
SortEntity stu2 = (SortEntity) o2;
if (!stu2.getPercentage().equals(stu1.getPercentage())) {
return stu1.getPercentage().compareTo(stu2.getPercentage());
} else {
return 1;
}
});
logger.info("" + "//实体per类型排序////////////////////排序后");
for (SortEntity sortEntity : sortEntityList) {
System.out.println(sortEntity.getPercentage());
}
/**
* 如果这个类型的升序可以直接用工具类
* List<Integer> indexes = new ArrayList<>();
* Collections.sort(indexes);
*/
}
}
###########################################################
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class ShuffListMethodSortBoolean {
public static int num = 10000;
public static void main(String[] args) {
List<String> cardList = new ArrayList<>();
for (int i = 0; i < 1000000; i++) {
cardList.add("cardId" + i);
}
long startTime = System.currentTimeMillis();
List<String> randomList = shuffleMethod(cardList);
long endTime = System.currentTimeMillis();
System.out.println("花费时间:" + (endTime - startTime));
// System.out.println(randomList);
}
/**
* 随机拿到配置num数量的数据
*/
private static List<String> shuffleMethod(List<String> resultList) {
//防止列表长度比需要的数量短,按照长度短的决定最终数量
int randomNum = Math.min(num, resultList.size());
Random random = new Random();
// 索引列表
List<Integer> indexes = new ArrayList<>();
while (indexes.size() < randomNum) {
int index = random.nextInt(resultList.size());
if (!indexes.contains(index)) {
indexes.add(index);
}
}
//对indexes排序
Collections.sort(indexes);
//取出indexes对应的list放到newList
List<String> randomList = new ArrayList<>();
for (int index : indexes) {
randomList.add(resultList.get(index));
}
return randomList;
}
}
###########################################################
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
public class ShuffListMethodBitSetListBoolean {
public static void main(String[] args) {
Long start = System.currentTimeMillis();
int num = (int) Math.pow(10, 4);
List<String> cardList = new ArrayList<>();
for (int i = 0; i < Math.pow(10, 6); i++) {
cardList.add("cardId" + i);
}
// 提供一个方法,随机从cardList中取出num数量的元素,并且新列表的元素顺序和保持和原列表一致(即:原本在前的还在前边)
List<String> randomList = shuffleMethod(cardList, num);
// System.out.println(randomList);
Long end = System.currentTimeMillis();
System.out.println((end - start) + "ms");
}
/**
* 随机拿到配置num数量的数据
*/
private static List<String> shuffleMethod(List<String> inputList, int num) {
num = num <= inputList.size() ? num : inputList.size();
//存下表
List<Integer> weiZhi = new ArrayList<>();
for (int i = 0; i < inputList.size(); i++) {
weiZhi.add(i);
}
//存获取的下标,按照先后顺序来
List minList = new ArrayList();
while (minList.size() < num) {
int xiaBiao = (int) (Math.random() * weiZhi.size());
if (!minList.contains(xiaBiao)) {
minList.add(xiaBiao);
}
}
BitSet bit = new BitSet(num);
int size = bit.length();
for (int i = 0; i < num; i++) {
int o = (int) minList.get(i);
bit.set(weiZhi.get(o));
}
List<String> resList = new ArrayList<>();
for (int index = 0; index < inputList.size(); index++) {
if (bit.get(index)) {
resList.add(inputList.get(index));
}
}
return resList;
}
}
###########################################################
import java.util.BitSet;
public class BitSetDemo {
public static void main(String[] args) {
//总长度
int count = 10;
//随机取四个
int i = 4;
BitSet bit = new BitSet(count);
while (i > 0) {
//mei有判重
bit.set((int) (Math.random() * count));
i--;
}
for (int index = 0; index < count; index++) {
if (bit.get(index)) {
System.out.print(index + ",");
}
}
}
}