关闭

迭代器_HashMap分拣思路与面向对象组合解题JAVA_119-121

标签: java迭代器hashmap分拣思路
146人阅读 评论(0) 收藏 举报
分类:

来源:http://www.bjsxt.com/
1、S02E119_01自定义实现迭代器深入迭代器迭代器原理_面向对象实现

package com.test.iterator;

import java.util.Iterator;
/**
 * 简化迭代器原理、加入接口、提供方法
 */
public class ArrayListForTestIterator implements Iterable<String> {

    private String[] elem = {"a","b","c"};
    private int size = elem.length;

    /**
     * 匿名内部类
     */
    public Iterator<String> iterator() {

        return new Iterator<String>() {
            private int cursor = -1;
            /**
             * 判断是否存在下一个元素
             */
            public boolean hasNext() {
                return cursor + 1 < size;
            }
            /**
             * 获取下一个元素
             */
            public String next() {
                cursor++;
                return elem[cursor];
            }
            /**
             * 删除元素
             */
            public void remove() {
                //没有实现
            }
        };
    }

    public static void main(String[] args) {
        ArrayListForTestIterator list = new ArrayListForTestIterator();
        Iterator<String> itr = list.iterator();
        while(itr.hasNext()){
            System.out.println(itr.next());
            itr.remove();//查看时可删除元素
        }
        //可重复使用
        itr = list.iterator();
        while(itr.hasNext()){
            System.out.println(itr.next());
        }

        System.out.println("增强for,必须实现java.lang.Iterable接口,重定iterator方法");
        for(String temp : list){
            System.out.println(temp);//一般不考虑下标时用增强for
            //不可用remove
        }
    }
}

2、S02E120_01HashMap_经典存储分拣思路简单_容易

package com.test.map;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
 * HashMap经典存储_分拣思路:
 * <br>this is a cat and that is a mice and where is the food?
 * <br>统计每个单词出现的次数
 * <br>存储到Map中
 * <br>key:String
 * <br>value:自定义类型
 * <br>第一种方法:为所有key创建容器,之后容器中存放对应value
 * <br>第二种方法:第一次创建容器并存放值value,第二次之后直接使用容器存入值
 */
public class Demo01 {
    String str = "this is a cat and that is a mice and where is the food";
    //分割字符串
    String[] strArray = str.split(" ");
    //存储到Map中
    Map<String,Letter> letterMap = new HashMap<String,Letter>();

    public static void main(String[] args) {
        test01();
        test02();
    }

    //第一种方法
    public static void test01(){
        System.out.println("第一种方法+++++++++++++++++++");
        Demo01 demo01 = new Demo01();
        for(String temp : demo01.strArray){
            //1、为所有key创建容器
            if(!demo01.letterMap.containsKey(temp)){
                demo01.letterMap.put(temp, new Letter());
            }
            //2、之后容器中存放对应value
            Letter letter = demo01.letterMap.get(temp);
            letter.setCount(letter.getCount() + 1);
        }

        //输出Map的值
        Set<String> keys = demo01.letterMap.keySet();
        for (String key : keys) {
            Letter letter = demo01.letterMap.get(key);
            System.out.println("字母:" + key + ",次数:" + letter.getCount());
        }
    }

    //第二种方法
    public static void test02(){
        System.out.println("第二种方法++++++++++++++++++++++++++++++++");
        Demo01 demo01 = new Demo01();
        for(String temp : demo01.strArray){
/*          //1、第一次创建容器并存放值value
            if(!demo01.letterMap.containsKey(temp)){
                Letter letter = new Letter();
                letter.setCount(1);
                demo01.letterMap.put(temp, letter);
            }else{
            //2、第二次之后直接使用容器存入值
            Letter letter = demo01.letterMap.get(temp);
            letter.setCount(letter.getCount() + 1);
            }*/

            //对以上代码优化
            Letter letter = demo01.letterMap.get(temp);
            if(null == letter){
                letter = new Letter();
                letter.setCount(1);
                demo01.letterMap.put(temp, letter);
            }else{//上面已获取了letter = demo01.letterMap.get(temp)
                letter.setCount(letter.getCount() + 1);
            }
        }

        //输出Map的值
        Set<String> keys = demo01.letterMap.keySet();
        for (String key : keys) {
            Letter letter = demo01.letterMap.get(key);
            System.out.println("字母:" + key + ",次数:" + letter.getCount());
        }
    }
}
/////////////////////////////////////////////////
package com.test.map;

public class Letter {
    private String name;
    private int count;

    public Letter() {
    }

    public Letter(String name, int count) {
        super();
        this.name = name;
        this.count = count;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getCount() {
        return count;
    }
    public void setCount(int count) {
        this.count = count;
    }
}

3、S02E121_01121HashMap_经典存储经典分拣思路与面向对象组合解题

package com.test.map;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * HashMap经典分拣思路与面向对象组合解题:
 * <br>定义一个Student类,属性:name姓名,classNumber班号,score成绩
 * <br>将若干Student对象放入List,请统计出每个班级的总分和平均分,分别打印出来
 */
public class Demo02 {

    public static void main(String[] args) {
        List<Student> list = new ArrayList<Student>();
        exam(list);

        //统计
        Map<String, ClassRoom> classRoomMap = new HashMap<String, ClassRoom>();
        ClassRoom.count(classRoomMap,list);
        //打印
        ClassRoom.printScore(classRoomMap);
    }
    /**
     * 将若干Student对象放入List
     */
    public static void exam(List<Student> list){
        list.add(new Student("a", "001", 80));
        list.add(new Student("b", "001", 80));
        list.add(new Student("c", "002", 80));
        list.add(new Student("d", "003", 80));
        list.add(new Student("e", "003", 80));
    }
}
/////////////////////////////////////////////////////
package com.test.map;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

public class ClassRoom {
    private String classNum;
    private List<Student> stuList;
    private double total;

    public ClassRoom() {
        stuList = new ArrayList<Student>();
    }
    public ClassRoom(String classNum) {
        this();
        this.classNum = classNum;
    }
    /**
     * 统计分数
     */
    public static void count(Map<String, ClassRoom> classRoomMap,List<Student> stuList){
        for(Student stu : stuList){
            String classNum = stu.getClassNum();
            double score = stu.getScore();
            //根据班级编号查看Map是否存在该班级——分拣思路
            ClassRoom classRoom = classRoomMap.get(classNum);
            if(null == classRoom){//把班级存储到Map中
                classRoom = new ClassRoom(classNum);
                classRoomMap.put(classNum, classRoom);
            }

            //存储总分
            classRoom.setTotal(classRoom.getTotal() + score);
            classRoom.getStuList().add(stu);//班级加入学生
        }
    }
    /**
     * 打印总分与平均分
     */
    public static void printScore(Map<String, ClassRoom> classRoomMap){
        Set<Map.Entry<String, ClassRoom>> entrySet = classRoomMap.entrySet();
        Iterator<Entry<String, ClassRoom>> itr = entrySet.iterator();
        while(itr.hasNext()){
            Entry<String, ClassRoom> entry = itr.next();
            ClassRoom classRoom = entry.getValue();
            double avg = classRoom.getTotal()/classRoom.getStuList().size();
            System.out.println("班级号为:" + classRoom.getClassNum() + ",总分为:" + classRoom.getTotal() + ",平均分为:" + avg);
        }
    }
    public String getClassNum() {
        return classNum;
    }
    public void setClassNum(String classNum) {
        this.classNum = classNum;
    }
    public List<Student> getStuList() {
        return stuList;
    }
    public void setStuList(List<Student> stuList) {
        this.stuList = stuList;
    }
    public double getTotal() {
        return total;
    }
    public void setTotal(double total) {
        this.total = total;
    }
}
//////////////////////////////////////////////////////
package com.test.map;

public class Student {
    private String name;
    private String classNum;
    private int score;

    public Student(){

    }
    public Student(String name, String classNum, int score) {
        super();
        this.name = name;
        this.classNum = classNum;
        this.score = score;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getClassNum() {
        return classNum;
    }
    public void setClassNum(String classNum) {
        this.classNum = classNum;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:37408次
    • 积分:667
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:105篇
    • 译文:0篇
    • 评论:1条
    文章分类