黑马程序员技术交流社区

标题: 商品浏览记录的制作!一点问题 [打印本页]

作者: hejinzhong    时间: 2014-8-29 06:19
标题: 商品浏览记录的制作!一点问题
本帖最后由 hejinzhong 于 2014-8-29 07:12 编辑



如何用hashMap实现我的商品编号的1-n的排序,现在用的是TreeMap
按理说,1,2,3的哈希值不是从小到大的吗??
我看了String中hashCode方法,还是没得到自己想要的结果!
下面写代码,以便分清个模块

这个是测试的HashCode,为啥不会按这个顺序存储呢!!



作者: hejinzhong    时间: 2014-8-29 06:26
本帖最后由 hejinzhong 于 2014-8-29 06:32 编辑

  1. 这里是描述商品的JavaBean和map集合存储信息,相当简单部分

  2. 类:product
  3. /*
  4. * 建立一个javaBean形式的Product
  5. */
  6. public class Product implements Serializable{
  7.         private String id;
  8.         private String name;
  9.         private String price;
  10.         private String numb;
  11.         
  12.         
  13.         public Product() {
  14.                 super();
  15.         }
  16.         
  17.         public Product(String id, String name, String price, String numb) {
  18.                 this.id = id;
  19.                 this.name = name;
  20.                 this.price = price;
  21.                 this.numb = numb;
  22.         }
  23.         public String getId() {
  24.                 return id;
  25.         }
  26.         public void setId(String id) {
  27.                 this.id = id;
  28.         }
  29.         public String getName() {
  30.                 return name;
  31.         }
  32.         public void setName(String name) {
  33.                 this.name = name;
  34.         }
  35.         public String getPrice() {
  36.                 return price;
  37.         }
  38.         public void setPrice(String price) {
  39.                 this.price = price;
  40.         }
  41.         public String getNumb() {
  42.                 return numb;
  43.         }
  44.         public void setNumb(String numb) {
  45.                 this.numb = numb;
  46.         }
  47. }

  48. 类:DB
  49. public class DB {
  50.         
  51.         //这里将map作为成员变量
  52.         private static Map<String, Product> map = new TreeMap<String, Product>();
  53.         /*
  54.         问题就在这里,如何用hashMap让map集合中的商品按下面的编号进行存储,键为编号
  55.         */
  56.         static{
  57.                 map.put("1", new Product("1", "衬衫", "38", "5"));
  58.                 map.put("2", new Product("2", "短裤", "68", "2"));
  59.                 map.put("3", new Product("3", "板鞋", "88", "1"));
  60.                 map.put("4", new Product("4", "袜子", "18", "8"));               
  61.         }
  62.         
  63.         public static Product findById(String id){
  64.                 return map.get(id);
  65.         }
  66.         
  67.         public static Map<String, Product> findAll(){
  68.                 return map;
  69.         }
  70. }
复制代码


作者: hejinzhong    时间: 2014-8-29 06:30

  1. /*
  2. *这里是查找所有商品和浏览记录的部分。都是利用请求转发,让其他模块显示
  3. */
  4. 类:FindAll
  5. public class FindAll extends HttpServlet {

  6.         public void doGet(HttpServletRequest request, HttpServletResponse response)
  7.                         throws ServletException, IOException {
  8.                 /*
  9.                  * 由于根据显示单独一块功能,所以这里在请求转发之前,
  10.                  * 只需要获取到存取商品信息的map,缓存在request中,将其传走即可
  11.                  */
  12.                 Map<String, Product> map = DB.findAll();
  13.                 request.setAttribute("pros", map);
  14.                 request.getRequestDispatcher("/showAll").forward(request, response);
  15.         }

  16.         public void doPost(HttpServletRequest request, HttpServletResponse response)
  17.                         throws ServletException, IOException {
  18.                 doGet(request, response);
  19.         }

  20. }

  21. //查找一个商品的信息,并将请求转发

  22. 类:FindOne
  23. public class FindOne extends HttpServlet {

  24.         public void doGet(HttpServletRequest request, HttpServletResponse response)
  25.                         throws ServletException, IOException {
  26.                 String id = request.getParameter("id");
  27.                 Product pt = DB.findById(id);
  28.                 request.setAttribute("pro", pt);
  29.                 request.getRequestDispatcher("/showOne").forward(request, response);
  30.         }

  31.         public void doPost(HttpServletRequest request, HttpServletResponse response)
  32.                         throws ServletException, IOException {
  33.                 doGet(request, response);
  34.         }

  35. }

复制代码


作者: hejinzhong    时间: 2014-8-29 06:35

  1. /*
  2. *这里将请求转发过来的信息处理之后,给浏览器,有显示所有和单一商品
  3. */
  4. 类:ShowAll

  5. public class ShowAll extends HttpServlet {

  6.         public void doGet(HttpServletRequest request, HttpServletResponse response)
  7.                         throws ServletException, IOException {
  8.                
  9.                 response.setContentType("text/html;charset=utf-8");
  10.                 PrintWriter pw = response.getWriter();
  11.                 /*
  12.                  * 构建一个表格
  13.                  */
  14.                 pw.print("<div align=center>商品信息汇总表</div><hr width=300 align=center/>");
  15.                 pw.print("<table border=1 align=center>");
  16.                 pw.print("<tr>");
  17.                 pw.print("<td>编号</td>");
  18.                 pw.print("<td>名称</td>");
  19.                 pw.print("<td>价格</td>");
  20.                 pw.print("<td>数量</td>");
  21.                 pw.print("<td>查看</td>");
  22.                 pw.print("</tr>");        
  23.                
  24.                 Map<String, Product> pros = (Map<String, Product>) request.getAttribute("pros");
  25.                 if(pros!=null){
  26.                         for ( Entry<String, Product> entry : pros.entrySet()) {                        
  27.                                 Product pt = entry.getValue();
  28.                                 pw.print("<tr>");
  29.                                 pw.write("<td>"+pt.getId()+"</td>");
  30.                                 pw.write("<td>"+pt.getName()+"</td>");
  31.                                 pw.write("<td>"+pt.getPrice()+"</td>");
  32.                                 pw.write("<td>"+pt.getNumb()+"</td>");
  33.                                 pw.write("<td><a href='/HE02/findOne?id="+pt.getId()+"'>详情</a></td>");
  34.                                 pw.print("</tr>");
  35.                         }
  36.                 }
  37.                 pw.print("</table><br/><br/><br/>");
  38.                
  39.                 pw.write("<div align=center>商品浏览记录<hr width=300 align=center />");
  40.                 Cookie[] cookies = request.getCookies();
  41.                 if(cookies !=null){
  42.                         for (Cookie cookie : cookies) {
  43.                                 if("ids".equals(cookie.getName())){
  44.                                         String[] ids  = cookie.getValue().split(",");
  45.                                         for (int i=0;i<ids.length;i++) {
  46.                                                 Product pt = DB.findById(ids[i]);
  47.                                                 pw.write((i+1)+":<a href=/HE02/findOne?id="+pt.getId()+">"+pt.getName()+"</a><br/>");
  48.                                         }
  49.                                 }
  50.                         }
  51.                 }
  52.                 pw.write("</div>");
  53.         }

  54.         public void doPost(HttpServletRequest request, HttpServletResponse response)
  55.                         throws ServletException, IOException {
  56.                 doGet(request, response);
  57.         }

  58. }

  59. 类:ShowOne

  60. public class ShowOne extends HttpServlet {

  61.         public void doGet(HttpServletRequest request, HttpServletResponse response)
  62.                         throws ServletException, IOException {
  63.                
  64.                 response.setContentType("text/html;charset=utf-8");
  65.                 //得到需要显示的商品
  66.                 Product pt = (Product) request.getAttribute("pro");
  67.                 //输出给浏览器
  68.                 show(response, pt);

  69.                 StringBuilder sb = new StringBuilder();
  70.                 sb.append(pt.getId());
  71.                
  72.                 //更新cookies信息
  73.                 Cookie cookie = updateCookie(request, pt, sb);
  74.                 response.addCookie(cookie);
  75.         }


  76.         public void doPost(HttpServletRequest request, HttpServletResponse response)
  77.                         throws ServletException, IOException {
  78.                 doGet(request, response);
  79.         }

  80.         public void show(HttpServletResponse response, Product pt)
  81.                         throws IOException {
  82.                 PrintWriter pw = response.getWriter();
  83.                 pw.write("<div align=center>");
  84.                 pw.write("商品详情 <hr width=300 align=center/>");
  85.                 pw.write("商品:"+pt.getName()+"  甩卖:"+pt.getPrice());
  86.                 pw.write("<a href='/HE02/findAll'><br/>返回商品列表</a>");
  87.                 pw.write("</div>");
  88.         }
  89.         
  90.         public Cookie updateCookie(HttpServletRequest request, Product pt,
  91.                         StringBuilder sb) {
  92.                 Cookie[] cookies = request.getCookies();                        
  93.                 if(cookies!=null){
  94.                         for (Cookie ck : cookies) {
  95.                                 if(ck.getName().equals("ids")){
  96.                                         String[] cookie = ck.getValue().split(",");
  97.                                         for (String id : cookie) {
  98.                                                 if(!id.equals(pt.getId())){
  99.                                                         sb.append(","+id);
  100.                                                 }
  101.                                         }
  102.                                 }
  103.                         }
  104.                 }
  105.                 Cookie cookie = new Cookie("ids", sb.toString());
  106.                 cookie.setMaxAge(60);
  107.                 cookie.setPath("/");
  108.                 return cookie;
  109.         }
  110. }
复制代码


作者: ximi    时间: 2014-8-29 08:25
HashMap存储是无序的,可以使用Set下面的集合类
作者: 优乐    时间: 2014-8-29 10:20
HashMap底层是链表数组的结构(LinkedList[ ]),他的put方法存放键值对到数组的顺序是根据key.hashCode()%数组长度,而不是根据key.hashCode()存放的。
跟据key的hashcode%链表数组的大小,确定放在第几个数组上
如果该数组元素为空,那么为该数组创建一个链表(LinkedList  link=new LinkedList),并把键值对加到链表上(link.add())
如果该数组不为空,那么直接在该链表上加键值对(数组不为空,也就是创建了LinkedList,直接在后面加就行了)
作者: hejinzhong    时间: 2014-8-29 10:48
优乐 发表于 2014-8-29 10:20
HashMap底层是链表数组的结构(LinkedList[ ]),他的put方法存放键值对到数组的顺序是根据key.hashCode()% ...

大哥!你去看看字符串的HachCode方法怎么覆写的!
作者: 优乐    时间: 2014-8-29 10:51
hejinzhong 发表于 2014-8-29 10:48
大哥!你去看看字符串的HachCode方法怎么覆写的!

为什么要看字符串的HachCode方法怎么覆写的
作者: hejinzhong    时间: 2014-8-29 10:59
优乐 发表于 2014-8-29 10:51
为什么要看字符串的HachCode方法怎么覆写的

我存储的是字符串类型的编号,难道存的时候不是调用他自己的HashCode方法?

作者: 优乐    时间: 2014-8-29 11:01
hejinzhong 发表于 2014-8-29 10:48
大哥!你去看看字符串的HachCode方法怎么覆写的!

存放的顺序是根据key.hashCode()%数组长度,而不是根据key.hashCode()存放的
不是说hashCode()就能确定存储的位置,就像你说的key的hash值为49,50,51,52,假设数组长度为5,那么key.hashCode()%数组长度就分别是4,0,1,2
存放的顺序就是array[0]=50代表的key,array[1]=51代表的key....
这跟key.hashCode()是不一样的把
不知道我的意思表述清楚了没
作者: 南国    时间: 2014-8-29 11:05
不会的飘过
作者: 优乐    时间: 2014-8-29 12:07
hejinzhong 发表于 2014-8-29 10:59
我存储的是字符串类型的编号,难道存的时候不是调用他自己的HashCode方法?
...

1  key.hashCode()%数组的长度
2  key.hashCode()
这2种方法计算的结果是不一样的,这个应该没问题吧

HashMap就是根据第一种方法来决定存的顺序啊,所以跟 key.hashCode()计算出来的顺序是不一样的啊

表述能力有限,如果还是不明白。我也无能为力了!!!!





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2