提问者:小点点

根据代码中的某些条件将对象优雅地添加到HashMap中


我目前正在Java构建一个日历应用程序。 我决定采取的路线是保留一个日历月视图(在JavaFX中构建)并更改日期。 由于每个月通常都有不同的第一天,因此每次月份更改时,这些日期都必须重新添加到日历中。 我决定使用一个Hashmap,我将一个月中的几天添加到其中,这取决于这个月的第一天是什么。 然后我可以在我的代码中使用这个Hashmap来做一些事情,改变应用程序端的文本字段等等。

我提交的代码是我如何将日期添加到hashmap,然后打印它们(为了测试目的显示它们是什么日期)的基本版本。

我的方法是有效的,但是看起来很冗长,如果我有一个程序,比如说有10000个Hashmap条目,这个方法就不实用了。

我的问题如下:在Hashmap中添加条目依赖于代码中的某些条件的情况下,是否有一种更优雅的方法来添加条目?

import java.util.HashMap;
import java.util.Set;

public class MyMonth {

    public static void main(String[] args) {

        addDaysFromFirstDayOfWeek(list);

        int localcount = 1;

        Set<Integer> hashset = list.keySet();

        for (Integer key : hashset) {

            String day = list.get(key);
            System.out.println( day + " is day " + localcount + " in the month " );
            localcount ++;
        }
    }


    static int count = 1;
    static int Daysinmonth = 30;

   


    public static HashMap<Integer, String> list = new HashMap<>(Daysinmonth);



    //Week 1
    static String Monday1 = "Monday1";
    static String Tuesday1 = "Tuesday1";
    static String Wednesday1 = "Wednesday1";
    static String Thursday1 = "Thursday1";
    static String Friday1 = "Friday1";
    static String Saturday1 = "Saturday1";
    static String Sunday1 = "Sunday1";

    // Week 2
    static String Monday2 = "Monday2";
    static String Tuesday2 = "Tuesday2";
    static String Wednesday2 = "Wednesday2";
    static String Thursday2 = "Thursday2";
    static String Friday2 = "Friday2";
    static String Saturday2 = "Saturday2";
    static String Sunday2 = "Sunday2";






    static String FirstDayOfWeek = Tuesday1;





    public static void addDaysFromFirstDayOfWeek(HashMap<Integer,String> hashMap) {


        if (FirstDayOfWeek.equals(Monday1)) {
            hashMap.put(count, Monday1);
            count++;
            hashMap.put(count, Tuesday1);
            count++;
            hashMap.put(count, Wednesday1);
            count++;
            hashMap.put(count, Thursday1);
            count++;
            hashMap.put(count, Friday1);
            count++;
            hashMap.put(count, Saturday1);
            count++;
            hashMap.put(count, Sunday1);
            count++;
            addDaysToArrayList(hashMap);



        } else if (FirstDayOfWeek.equals(Tuesday1)) {
            hashMap.put(count, Tuesday1);
            count++;
            hashMap.put(count, Wednesday1);
            count++;
            hashMap.put(count, Thursday1);
            count++;
            hashMap.put(count, Friday1);
            count++;
            hashMap.put(count, Saturday1);
            count++;
            hashMap.put(count, Sunday1);
            count++;
            addDaysToArrayList(hashMap);

        } else if (FirstDayOfWeek.equals(Wednesday1)) {
            hashMap.put(count, Wednesday1);
            count++;
            hashMap.put(count, Thursday1);
            count++;
            hashMap.put(count, Friday1);
            count++;
            hashMap.put(count, Saturday1);
            count++;
            hashMap.put(count, Sunday1);
            count++;
            addDaysToArrayList(hashMap);
        } else if(FirstDayOfWeek.equals(Thursday1)){
            hashMap.put(count, Thursday1);
            count++;
            hashMap.put(count, Friday1);
            count++;
            hashMap.put(count, Saturday1);
            count++;
            hashMap.put(count, Sunday1);
            count++;
            addDaysToArrayList(hashMap);
        } else if(FirstDayOfWeek.equals(Friday1)){
            hashMap.put(count, Friday1);
            count++;
            hashMap.put(count, Saturday1);
            count++;
            hashMap.put(count, Sunday1);
            count++;
            addDaysToArrayList(hashMap);
        } else if(FirstDayOfWeek.equals(Saturday1)){
            hashMap.put(count, Saturday1);
            count++;
            hashMap.put(count, Sunday1);
            count++;
            addDaysToArrayList(hashMap);
        } else if(FirstDayOfWeek.equals(Sunday1)){
            hashMap.put(count, Sunday1);
            count++;
            addDaysToArrayList(hashMap);
        }


    }

    public static void addDaysToArrayList(HashMap<Integer,String> hashMap){

        hashMap.put(count,Monday2);
        count++;
        hashMap.put(count,Tuesday2);
        count++;
        hashMap.put(count,Wednesday2);
        count++;
        hashMap.put(count,Thursday2);
        count++;
        hashMap.put(count,Friday2);
        count++;
        hashMap.put(count,Saturday2);
        count++;
        hashMap.put(count,Sunday2);
        count++;

    }


}

共1个答案

匿名用户

你只需要计算和翻译一周中的几天。

import java.time.*;
import java.util.*;

public class Main
{
    public static void main(String[] args) {
        Map<DayOfWeek, String> frenchDayOfWeek = new HashMap<>();
        frenchDayOfWeek.put(DayOfWeek.MONDAY, "lundi");
        frenchDayOfWeek.put(DayOfWeek.TUESDAY, "mardi");
        frenchDayOfWeek.put(DayOfWeek.WEDNESDAY, "mercredi");
        frenchDayOfWeek.put(DayOfWeek.THURSDAY, "jeudi");
        frenchDayOfWeek.put(DayOfWeek.FRIDAY, "vendredi");
        frenchDayOfWeek.put(DayOfWeek.SATURDAY, "samedi");
        frenchDayOfWeek.put(DayOfWeek.SUNDAY, "dimanche");
        
        LocalDate d = LocalDate.of(2020, 6, 1);
        // print days to the end of month
        do {
            String s = d.toString() + " is "
                + d.getDayOfWeek().toString()
                + "(" + frenchDayOfWeek.get(d.getDayOfWeek()) + " in french)"
                + (d.getDayOfMonth() / 7 + 1);
            System.out.println(s);
            d = d.plusDays(1);
        } while (d.getDayOfMonth() != 1);
    }
}

输出:

2020-06-01 is MONDAY(lundi in french)1
2020-06-02 is TUESDAY(mardi in french)1
2020-06-03 is WEDNESDAY(mercredi in french)1
2020-06-04 is THURSDAY(jeudi in french)1
2020-06-05 is FRIDAY(vendredi in french)1
2020-06-06 is SATURDAY(samedi in french)1
2020-06-07 is SUNDAY(dimanche in french)2
2020-06-08 is MONDAY(lundi in french)2
2020-06-09 is TUESDAY(mardi in french)2
...