发表于: 2019-05-28 18:04:43

2 521


今天完成的事情:

    @Override
   public List<Map<String,Object>> getParents(){
List<Product> product1=productMapper.select1();

       List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
       for(int j=0;j<product1.size();j++) {
Map<String, Object> map = new HashMap<String, Object>();
           product=product1.get(j);
           product = productMapper.selectSelective(product);
           map.put("id", product.getId());
           map.put("name", product.getName());
           int Id = 0;
           Id = product.getParentId();
           int i = 0;
//            if(Id != 0){
           while (Id != 0) {
//            for(int i=0;i<2;i++){
               products.setId(Id);
//                System.out.println(products);
               Products products2=productsMapper.selectIdProducts(products);
//                System.out.println(products2);
               map.put("parent" + i, products2.getName());
               Id=products2.getParentId();
               System.out.println(Id);
               i++;
//            }
           }
map.put("status", product.getStatus());
           map.put("createAt", product.getCreateAt());
           map.put("createBy", product.getCreateBy());
           map.put("updateAt", product.getUpdateAt());
           map.put("updateBy", product.getUpdateBy());
           list.add(map);
       }
return list;
   }

完成了拼接数据并且装入List的操作,如下:

这样在controller层拿到数据后就可以直接返还给jsp页面,在jsp页面循环就行,这里parent0表示二级标题.parent1表示一级标题,但是这样写的话速度比较慢,一个方法调用了4次查询接口.在IDEA中从开始运行到出结果需要两秒的时间.

        做这个方法的时候中间碰到了一个bug,

map.put("id"product.getId());

这一句getId没有值,单独测试

productMapper.selectSelective(product)

打印出来的product为空集,

是因为自己昨天碰到bug时修改了实体类里面的属性为

private int id;

private int parentId;

这就涉及到在Java语言中我们都是new一个对象放在堆中,通过堆栈的引用来使用这些对象,而基本数据类型不具备对象的性质,为了解决基本数据类型无法参与面向对象的开发问题,java5.0以后就提供了包装类,其方法是通过自动装箱(autoboxing),使基本数据类型有了对象的性质,并且有了属性和方法,尤其是ArrayList,HashMap中放东西时,容器都是装Object的,就需要用到基本类型的包装类.

首先我们通过Ctrl+左键进入Integer

可以发现Integer拥有Number类继承自Comparable,然后Alt+7展示他里面的方法

里面包含了他的构造方法,强制类型转换方法,比较的方法,valueOf方法,将-128~127返回的数值缓存为Integer对象.

通过一段代码来实验

@Test
public void testEquals() {
int int1 = 12;
   int int2 = 12;

   Integer integer1 = new Integer(12);
   Integer integer2 = new Integer(12);
   Integer integer3 = new Integer(127);

   Integer a1 = 127; //或者写成Integer a1 = Integer.valueOf(127);
   Integer a2 = 127;//或者写成Integer a2 = Integer.valueOf(127);

   Integer a = 128;
   Integer b = 128;

   System.out.println("int1 == int2 -> " + (int1 == int2));
   System.out.println("int1 == integer1 -> " + (int1 == integer1));
   System.out.println("integer1 == integer2 -> " + (integer1 == integer2));
   System.out.println("integer3 == a1 -> " + (integer3 == a1));
   System.out.println("a1 == a2 -> " + (a1 == a2));
   System.out.println("a == b -> " + (a == b));
}

可以得出结论:

1.由于Integer变量实际上是对一个Integer对象的引用,所以两个通过new生成的Integer变量永远是不相等的(因为new生成的是两个对象,其内存地址不同)。

2.Integer变量和int变量比较时,只要两个变量的值是向等的,则结果为true(因为包装类Integer和基本数据类型int比较时,java会自动拆包装为int,然后进行比较,实际上就变为两个int变量的比较)

3.非new生成的Integer变量和new Integer()生成的变量比较时,结果为false。(因为非new生成的Integer变量指向的是java常量池中的对象,而new Integer()生成的变量指向堆中新建的对象,两者在内存中的地址不同)

4.对于两个非new生成的Integer对象,进行比较时,如果两个变量的值在区间-128到127之间,则比较结果为true,如果两个变量的值不在此区间,则比较结果为false

参考资料:https://www.cnblogs.com/guodongdidi/p/6953217.html

对于第四条原因

public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
   return new Integer(i);
}

public Integer(int value) {
this.value = value;
}

对于-128~127之间的数字会直接调用Integer.valueOf()方法.

Number类中有

String和Character有其自己的方法,

还在Integer中学到了一个i/10的算法优化

static void getChars(int i, int index, char[] buf) {
int q, r;
   int charPos = index;
   char sign = 0;

   if (i < 0) {
sign = '-';
       i = -i;
   }

// Generate two digits per iteration
   while (i >= 65536) {
q = i / 100;
   // really: r = i - (q * 100);
       r = i - ((q << 6) + (q << 5) + (q << 2));
       i = q;
       buf [--charPos] = DigitOnes[r];
       buf [--charPos] = DigitTens[r];
   }

// Fall thru to fast mode for smaller numbers
   // assert(i <= 65536, i);
   for (;;) {
q = (i * 52429) >>> (16+3);
       r = i - ((q << 3) + (q << 1));  // r = i-(q*10) ...
       buf [--charPos] = digits [r];
       i = q;
       if (i == 0) break;
   }
if (sign != 0) {
buf [--charPos] = sign;
   }
}

如图在getChars中有一个q=(i*52429)>>>(16+3)

同样测试一下这个方法

@Test
public void testsin() {
for (int i = 0; i < 100000; i++) {
int a1 = i * 52429 >>> 19;
       int a2 = i / 10;
       if (a1 != a2) {
System.out.println(i);

       }
}
}

发现这个方法精度可以到81919都是相当于i/10并且算法速度快

参考资料:https://www.zhihu.com/question/34948884/answer/60497785

明天计划的事情:继续写工作室接口,想一想作品集下架接口怎么写.
遇到的问题:暂无
收获:学会了/10的算法优化,了解了什么是包装类,做出来了ListMap的拼装


返回列表 返回列表
评论

    分享到