SpringBoot ,Controller层 通用的返回类,自定义集合类

来自上海的这位朋友 2022-08-06 08:51:23 阅读数:729

SpringBootcontroller自定义通用返回

SpringBoot ,Controller层 通用的返回类,自定义集合类

 public static void main(String[] args) {

EnhanceList<String> aspect = new EnhanceList<>(true);
for (int i = 0; i < 11; i++) {

aspect.setValue(i,"test"+i);
}
System.out.println("第五个值:"+aspect.getMapValue(5));
System.out.println("第六个值:"+aspect.getMapValue(6));
System.out.println("大小:"+aspect.size());
System.out.println("第一个值:"+aspect.first());
System.out.println("最后一个值:"+aspect.last());
Map<String, String> all = aspect.getAllMap();
System.out.println("全部数据 = " + all);
aspect.addValue("list1");
aspect.addValue("list2");
aspect.addValue("list3");
System.out.println("第一个值:"+aspect.getListValue(0));
System.out.println("第二个值:"+aspect.getListValue(1));
System.out.println("aspect.getAllList() = " + aspect.getAllList());
}
输出:
第五个值:test5
第六个值:test6
大小:11
第一个值:test0
最后一个值:test10
全部数据 = {
0=test0, 1=test1, 2=test2, 3=test3, 4=test4, 5=test5, 6=test6, 7=test7, 8=test8, 9=test9, 10=test10}
第一个值:list1
第二个值:list2
aspect.getAllList() = [list1, list2, list3]
源码:
public class EnhanceList<V> {

private transient V first;
private transient V last;
private transient boolean repetition;
private final Map<String,V> grid;
private List<V> list;
private final List<Integer> duplicateGrid = new Vector<>();
public EnhanceList() {

grid = new HashMap<>();
list = new ArrayList<>();
}
public EnhanceList(boolean repetition) {

grid = new HashMap<>();
list = new ArrayList<>();
this.repetition = repetition;
}
public boolean setValue(Integer num, V v){

theSizeOfTheDecision(num);
add(num,v);
return true;
}
public boolean addValue(V v){

list.add(v);
return true;
}
public V getListValue(int index){

return list.get(index);
}
public Map<String,V> getAllMap(){

return grid;
}
public List<V> getAllList(){

return list;
}
public V getMapValue(Integer num){

return grid.get(hash(num));
}
public int size(){

return grid.size();
}
public V first(){

return getMapValue(duplicateGrid.get(0));
}
public V last(){

return last;
}
private void add(Integer num, V v){

if (num < 0){

throw new IllegalArgumentException("Illegal num: "+
num);
}
if (first == null){

first = v;
}
grid.put(hash(num),v);
last = v;
}
static String hash(Object key) {

int h;
return String.valueOf((key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16));
}
/** * 检测是否有重复的值 构造方法开启true * @param num */
private void theSizeOfTheDecision(Integer num){

if (repetition){

if (duplicateGrid.contains(num)) {

throw new IllegalArgumentException("repetition num: "+
num);
}
}
duplicateGrid.add(num);
}
}
版权声明:本文为[来自上海的这位朋友]所创,转载请带上原文链接,感谢。 https://tanyongpeng.blog.csdn.net/article/details/123217845