- <P>package majun.jun;
- import java.util.Map;
- import java.util.TreeMap;
- import java.util.Map.Entry;
- public class TestMapDemo {
- /**
- * @param args
- */
- public static void main(String[] args) {
- Student[] stu=new Student[]{new Student("伍哲沂","201306266"),new Student("伍哲沂","201306266"),new Student("伍哲沂","20186266")};
- Map<Student,Integer> map=new TreeMap<Student,Integer>();
- // map.put(new Student("伍哲沂","201306266"), 1);
- // map.put(new Student("伍哲沂","201306266"), 33);
- // map.put(new Student("伍哲沂","2018886266"), 1);
- // System.out.println(stu[0].equals(stu[1]));
- for(int j=0;j<stu.length;j++){
- Integer value=map.get(stu[j]);
- if(value==null){
- map.put(stu[j], 1);
- value=1;
- stu[j].setCount(value);
- // System.out.println(value);
- }else{
- // System.out.println("1111"+value);
- value=value+1;
- map.put(stu[j], value);
- stu[j].setCount(value);
- }
- }
- /*for(int j=0;j<stu.length;j++){
- Integer value=map.get(stu[j]);
- if(value==null){
- map.put(stu[j], 1);
- value=1;
- stu[j].setCount(value);
- // System.out.println(value);
- }else{
- // System.out.println("1111"+value);
- value=value+1;
- map.put(stu[j], value);
- stu[j].setCount(value);
- }
- }*/
- for(Entry<Student,Integer> entry:map.entrySet()){
- Student s=entry.getKey();
- Integer value=entry.getValue();
- System.out.println(s);
- }
- }</P>
- <P><FONT color=lime>怎么让结果运行为 </FONT></P>
- <P><FONT color=lime>姓名:伍哲沂 学号:20186266 次数1--value: 1
- 姓名:伍哲沂 学号:201306266 次数1--value: 2</FONT></P>
- <P><FONT color=lime>实际结果是:</FONT></P>
- <P>姓名:伍哲沂 学号:20186266 次数1--value: 1
- 姓名:伍哲沂 学号:201306266 次数1--value: 1
- 姓名:伍哲沂 学号:201306266 次数1--value: 1</P>
- <P>=====================================================================</P>
- <P>hash equals 都复写过了 comparable接口也实现了 对象竟然重复 搞不明白 求大神!</P>
- <P>如果这样添加对象</P>
- <P> map.put(new Student("伍哲沂","201306266"), 1);
- map.put(new Student("伍哲沂","201306266"), 33);
- map.put(new Student("伍哲沂","2018886266"), 1);</P>
- <P>结果是这样:</P>
- <P><FONT color=#00ff00>姓名:伍哲沂 学号:20186266 次数1--value: 1
- 姓名:伍哲沂 学号:201306266 次数1--value: 33</FONT></P>
- <P>=====================================================================
- </P>
复制代码 |
|