Use HashMap to count the number of words in a line of text and the class score - sorting

 

 

0 Sorting Chart:

 


 

 

1 HashMap(key, MyBean(key, number of occurrences))

 

class Letter {
	private String name;
	private int count;
	public Letter() {
		// TODO Auto-generated constructor stub
	}
	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;
	}
	
	
	
}


/**
 * this is a cat and that is a mice and where is the food?
 * Count the number of times each word appears
 *
 * Store in Map
 * key :String
 * value: custom type
 *
 * "sorting" idea
 * 1. Create containers for all keys
 * After that, the corresponding value is stored in the container
 * 2. Create the container for the first time and store the value value
 * After the second time, use the container directly to store the value
 * @author Administrator
 *
 */
public class Demo01 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String str ="this is a cat and that is a mice and where is the food";
		// split the string
		String[] strArray=str.split(" ");
		// store in Map
		Map<String,Letter>  letters = new HashMap<String,Letter>();
		for(String temp:strArray){			
			/*
			 //1. Create a container for all keys		 
			if(!letters.containsKey(temp)){
				Letter col = new Letter();
				col.setCount(1); //The first value is stored in the container
				letters.put(temp, col);
			}else{
				//2. After the second time, directly use the container to store the value
				Letter col =letters.get(temp); //Use the container directly
				col.setCount(col.getCount()+1);
			}*/
			Letter col = null;
			if(null==(col=letters.get(temp))){ //This extraction is quite interesting
				col = new Letter();
				col.setCount(1); //The first value is stored in the container
				letters.put(temp, col);
			}else{
				//2. After the second time, directly use the container to store the value				
				col.setCount(col.getCount()+1);
			}
		}
		// output the value of the map
		Set<String> keys = letters.keySet();
		for(String key:keys){
			Letter col =letters.get(key);
			System.out.println("Letter:"+key+",Number of times"+col.getCount());
		}
		
		
	}
	public static void test1(){
		String str ="this is a cat and that is a mice and where is the food";
		// split the string
		String[] strArray=str.split(" ");
		// store in Map
		Map<String,Letter>  letters = new HashMap<String,Letter>();
		/*
		for(String temp:strArray){
			
			 //1. Create a container for all keys
			 	 Then the corresponding value is stored in the container
			 
			if(!letters.containsKey(temp)){
				letters.put(temp, new Letter());
			}
		}
		for(String temp:strArray){
			
			// Store the corresponding value in the container
			
			Letter col =letters.get(temp); //Use the container directly
			col.setCount(col.getCount()+1);
		}
		
		*/
		for(String temp:strArray){
			
			 //1. Create a container for all keys		 
			if(!letters.containsKey(temp)){
				letters.put(temp, new Letter());
			}
			//2, then store the corresponding value in the container
			Letter col =letters.get(temp); //Use the container directly
			col.setCount(col.getCount()+1);
		}
		
		// output the value of the map
		Set<String> keys = letters.keySet();
		for(String key:keys){
			Letter col =letters.get(key);
			System.out.println("Letter:"+key+",Number of times"+col.getCount());
		}
	}
}

 

 

2 Count the average score of the class, pay attention to the encapsulation of the classroom in the value (object-oriented encapsulation) HashMap(classno, ClassRoom(classno,studentLists,totalScore))

public class Student {
	private String name;
	private String no;
	private double score;
	public Student() {
	}
	public Student(String name, String no, double score) {
		super();
		this.name = name;
		this.no = no;
		this.score = score;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNo() {
		return no;
	}

	public void setNo(String no) {
		this.no = no;
	}

	public double getScore() {
		return score;
	}

	public void setScore(double score) {
		this.score = score;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + ", no=" + no + ", score=" + score
				+ "]";
	}

}


/**
 * class
 * @author Administrator
 *
 */
public class ClassRoom {
	private String no;
	private List<Student> stus; //student list
	private double total; //Total score
	public ClassRoom() {
		stus = new ArrayList<Student>();	
	}
	
	public ClassRoom(String no) {
		this();
		this.no = no;	
	}
	public String getNo() {
		return no;
	}
	public void setNo(String no) {
		this.no = no;
	}
	public List<Student> getStus() {
		return stus;
	}
	public void setStus(List<Student> stus) {
		this.stus = stus;
	}
	public double getTotal() {
		return total;
	}
	public void setTotal(double total) {
		this.total = total;
	}
	
	
	
}



/**
 * Define a Student class, attributes: name name, classNumber class number, score
   Now put several Student objects into the List, please count the total score and average score of each class, and print them out respectively
 object-oriented thinking
 * @author Administrator
 *
 */
public class MapDemo03 {
	public static void main(String[] args) {
		List<Student> list = new ArrayList<Student>();
		exam(list);		
		
		//statistics		
		Map<String,ClassRoom> rooms = new HashMap<String,ClassRoom>();
		count(rooms,list);
		//Print
		printScore(rooms);
	}
	
	/**
	 * Print the total and average scores
	 */
	public static void printScore(Map<String,ClassRoom> rooms){
		Set<Map.Entry<String,ClassRoom>> entrySet =rooms.entrySet();
		Iterator<Map.Entry<String,ClassRoom>> it =entrySet.iterator();
		while(it.hasNext()){
			Map.Entry<String,ClassRoom> entry =it.next();
			ClassRoom room = entry.getValue();
			double avg = room.getTotal()/room.getStus().size();
			System.out.println("The class number is: "+room.getNo()+", the total score is "+room.getTotal()+", the average score is "+avg);
		}		
	}
	
	
	/**
	 * Statistics score
	 */
	public static void count(Map<String,ClassRoom> rooms,List<Student> list){
		for(Student stu:list){
			String no = stu.getNo();
			double score = stu.getScore();
			//Check if there is a class sorting idea in the Map according to the class number
			ClassRoom room = rooms.get(no);
			if(null==room){ //First time
				room = new ClassRoom(no);
				rooms.put(no, room);
			}			
			// store the total score
			room.setTotal(room.getTotal()+score);
			room.getStus().add(stu); //Add students			
		}
	}
	
	
	/**
	 * Now put several Student objects into List
	 * @param 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("a","002",80));
		list.add(new Student("c","003",80));
		list.add(new Student("d","003",80));
	}
}

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326635664&siteId=291194637