[java learning path] (practice) collection exercise: classic examples

Posted by Brunda on Wed, 26 Jan 2022 21:39:50 +0100

Question 1: World Cup

Using Map, complete the following functions:
Read a string from the command line, indicating a year, and output which team is the world cup champion in that year. If no world cup is held in that year, output: no world cup is held.

Previous World Cup Champions
Number of sessions held year champion
The first 1930 Uruguay
The second session of 1934 Italy
The third 1938 Italy
Fourth 1950 Uruguay
The fifth 1954 West Germany
Sixth 1958 Brazil
The seventh 1962 Brazil
The 8th 1966 England
9th 1970 Brazil
The 10th West German Congress in 1974
The 11th Argentina in 1978
12th 1982 Italy
The 13th National Congress of Argentina, 1986
14th 1990 West Germany
15th Brazil, 1994
16th session 1998 France
17th Brazil 2002
18th 2006 Italy
19th 2010 Spain
20th 2014 Germany
21st 2018 France

On the basis of the original world cup Map, the following functions are added:


thinking

  1. According to the requirements of the topic, we need to input the data first
  2. Use containsKey and containValue to determine whether there is data we need in the collection
  3. Just print

code implementation

package Map;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class Hw {

    public static void main(String[] args) {


        Map<String, String> map=new TreeMap<>();

        map.put("1930", "Uruguay");
        map.put("1934", "Italy");
        map.put("1938", "Italy");
        map.put("1950", "Uruguay");
        map.put("1954", "West Germany");
        map.put("1958", "Brazil");
        map.put("1962", "Brazil");
        map.put("1966", "England");
        map.put("1970", "Brazil");
        map.put("1974", "West Germany");
        map.put("1978", "Argentina");
        map.put("1982", "Italy");
        map.put("1986", "Argentina");
        map.put("1990", "West Germany");
        map.put("1994", "Brazil");
        map.put("1998", "France");
        map.put("2002", "Brazil");
        map.put("2006", "Italy");
        map.put("2012", "Spain");
        map.put("2016", "Germany");


        Scanner sc=new Scanner(System.in);
        System.out.println("Please enter a year:");
        String str=sc.nextLine();

        if(map.containsKey(str)==false)
        {
            System.out.println("There was no world cup that year!");
        }
        else
        {
            System.out.println("The World Cup winners of the year were:"+map.get(str));
        }

        Scanner sc1=new Scanner(System.in);

        System.out.println("Please enter the name of the team:");

        String str1=sc1.nextLine();

        if(map.containsValue(str1)==true)
        {
            System.out.println(str1+"The winning years are:");
            for(String ss:map.keySet())
            {
                if(map.get(ss).contains(str1))
                {
                    System.out.print(" ,"+ss);
                }
            }
        }
    }

}

Question 2: how many times do letters appear

There is such a string "babbabcacdeabcdeaa", which counts the number of occurrences of each letter in the string
Required result: {a=5,b=4,c=3,d=2,e=2} consider that the content of the string is variable

code implementation

package Map;

import java.util.*;

public class Hw {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        char[] ch = str.toCharArray();

        Map<Character,Integer> treeMap = new TreeMap<>();

        for(Character i : ch){
            Integer value = treeMap.get(i);
            if(value==null){
                treeMap.put(i, 1);
            }else {
                value++;
                treeMap.put(i,value);
            }
        }

        System.out.println(treeMap);
        //The use of iterators below is purely a new thing. If you don't want to see it, you can skip it directly
        //Equivalent system out. println(treeMap);
        
//        Set<Map.Entry<Character, Integer>> entries = treeMap.entrySet();
//        Iterator<Map.Entry<Character, Integer>> iterator = entries.iterator();
//        while (iterator.hasNext()){
//            Map.Entry<Character, Integer> next = iterator.next();
//            System.out.println(next.getKey()+" = "+next.getValue());
//        }
    }

}

Running screenshot

Topic 3

There are 2 arrays
The content of the first array is: [Heilongjiang Province, Zhejiang Province, Jiangxi Province, Guangdong Province, Fujian Province]
The second array is: [Harbin, Hangzhou, Nanchang, Guangzhou, Fuzhou]
Store the first array element as a key and the second array element as a value in the Map collection.
For example, {Heilongjiang Province = Harbin, Zhejiang Province = Hangzhou,...}.

code implementation

package Map;

import java.util.*;

public class Hw {

    public static void main(String[] args) {
        Map<String,String> map = new HashMap<>();
        String[] a={"Henan Province","Zhejiang Province","Jiangxi Province","Guangdong Province","Fujian Province"};
        String[] b={"Zhengzhou","Hangzhou","Nanchang","Guangzhou","Fuzhou"};

        for(int i=0;i<a.length;i++){
            map.put(a[i],b[i]);
        }

        System.out.println(map);
    }

}

Running screenshot

Topics: Java Back-end