Store in Java 2D map and turn with for statement

** 1, the information "student name, subject name / score" has been made into a two-dimensional map data structure. ** ** ** 2, the total score and average score of each student were output using the for statement. ** **

Collection classes cannot store anything that is not an instance. However, it can be stored by converting int type information to Integer type (wrapper class).

Run in Eclipse Oxygen

A program that creates and turns a 2D map



import java.util.LinkedHashMap;
import java.util.Map;

public class MapScore {

	public static void main(String[] args) {
		//Create a 2D map
		//Immediately before the map name, that is, Map<String, Map<String, Integer>>Up to data type
		Map<String, Map<String, Integer>> nameMap = new LinkedHashMap<String, Map<String, Integer>>();

		//Suzuki
		Map<String, Integer> scoresMap_suzuki = new LinkedHashMap<String,  Integer>();
		scoresMap_suzuki.put("National language", 78);
		scoresMap_suzuki.put("Math", 90);
		scoresMap_suzuki.put("English", 20);

		//Mr Sato
		Map<String, Integer> scoresMap_satoh = new LinkedHashMap<String,  Integer>();
		scoresMap_satoh.put("National language", 50);
		scoresMap_satoh.put("Math", 40);
		scoresMap_satoh.put("English", 90);

		//Tanaka
		Map<String, Integer> scoresMap_tanaka = new LinkedHashMap<String,  Integer>();
		scoresMap_tanaka.put("National language", 80);
		scoresMap_tanaka.put("Math", 60);
		scoresMap_tanaka.put("English", 85);

		//Realize a 2D map by putting the above 3 maps in the value of the name map
		nameMap.put("Suzuki", scoresMap_suzuki);
		nameMap.put("Sato", scoresMap_satoh);
		nameMap.put("Tanaka", scoresMap_tanaka);

		//Turn the name map with Extended for
		//In the case of LinkedHashMap, it is fetched in the order of storage.
		for (Map.Entry<String,  Map<String, Integer>> element : nameMap.entrySet()) {

			//Get the value of nameMap and assign it to a new map
			Map<String,Integer> scores = element.getValue();

			int sum = 0;
			int avg = 0;

			//scoresMap_Turn the value of the name, that is, the score, and substitute it for sum
			for(int hogeInt : scores.values()){
				sum += hogeInt;
				avg  = sum  / scores.size();
			}

			//For the number of people((For 3 people) Output
			System.out.println(element + "The total score is" + sum + " " + "The average is" + avg);
		}
	}
}

Execution result

Execution result


Suzuki={National language=78,Math=90,English=20}The total score is 188 and the average is 62
Sato={National language=50,Math=40,English=90}Total score is 180 Average is 60
Tanaka={National language=80,Math=60,English=85}Total score is 225 Average is 75

Recommended Posts

Store in Java 2D map and turn with for statement
[Java] Make variables in extended for statement and for Each statement immutable
[Java] for Each and sorted in Lambda
Java for statement
[Java] How to turn a two-dimensional array with an extended for statement
Encrypt / decrypt with AES256 in PHP and Java
[Java] for statement / extended for statement
(Memo) Java for statement
Solving with Ruby and Java AtCoder ABC129 D 2D array
[Java basics] Let's make a triangle with a for statement
This and that for editing ini in Java. : inieditor-java
I tried using an extended for statement in Java
Prepare the environment for java11 and javaFx with Ubuntu 18.4
How to loop Java Map (for Each / extended for statement)
[For beginners] Explanation of classes, instances, and statics in Java
About for statement and if statement
[Java] Get Map key / value pairs using extended for statement
Log aggregation and analysis (working with AWS Athena in Java)
[Java] Basic statement for beginners
☾ Java / Iterative statement and iterative control statement
[Java] A technique for writing constructors, getters, and setters in one shot with IntelliJ IDEA.
StringUtils.isNotBlank is convenient for empty judgment and null judgment in java
Tips for using Salesforce SOAP and Bulk API in Java
Java while and for statements
Solving with Ruby, Perl and Java AtCoder ABC 136 D Breadth-first search
Compare Hello, world! In Spring Boot with Java, Kotlin and Groovy
[For beginners] Minimum sample to update RecyclerView with DiffUtils in Java
[Android] Convert Map to JSON using GSON in Kotlin and Java
How to encrypt and decrypt with RSA public key in Java
Morphological analysis in Java with Kuromoji
Use java with MSYS and Cygwin
Distributed tracing with OpenCensus and Java
Install Java and Tomcat with Ansible
AWS SDK for Java 1.11.x and 2.x
Rock-paper-scissors game for beginners in Java
[Java] Branch enum with switch statement
Java for beginners, expressions and operators 1
Encoding and Decoding example in Java
[For beginners] Run Selenium in Java
Use JDBC with Java and Scala.
Get Null-safe Map values in Java
Java for beginners, expressions and operators 2
Use composite keys in Java Map.
Enable OpenCV with java8. (For myself)
StringBuffer and StringBuilder Class in Java
Output PDF and TIFF with Java 8
Understanding equals and hashCode in Java
Settings for SSL debugging in Java
Play with Markdown in Java flexmark-java
Classes and instances Java for beginners
Encrypt with Java and decrypt with C #
Java8 list conversion with Stream map
Hello world in Java and Gradle
[Java] Output the result of ffprobe -show_streams in JSON and map it to an object with Jackson
For the time being, run the war file delivered in Java with Docker
Automatically transfer animations for each title and store them in a folder
I wrote a Lambda function in Java and deployed it with SAM
[Java] [For beginners] How to insert elements directly in a 2D array
(For super beginners) Getter / setter and property to think in D language
[Java] Get List / Map elements with Iterator
Difference between final and Immutable in Java