【问题标题】:How to visualize java complex collection objects with multiple levels down in diagrams如何在图表中可视化具有多个级别的java复杂集合对象
【发布时间】:2021-10-16 20:27:09
【问题描述】:

如何在图表中可视化 java 集合对象?比起文字和书籍,我更擅长处理图片和视频。

如果地图的键为字符串,列表为值。
上面的列表还有另一个自定义对象列表。
自定义对象有另一个列表。

这些类型的场景经常出现在实际项目中。
如何描绘和想象这些类型的场景以推进编码?

【问题讨论】:

    标签: java


    【解决方案1】:

    想象一下这样的例子。您的每个朋友都有他或她访问过的国家/地区的列表。

    键是String名称,值是List<Country>

    Map countriesVisited = new HashMap<String, List<Country>>();
    

    每个国家/地区都有许多您的朋友去过的城市。例如,李在乌克兰访问了基辅、敖德萨和利沃夫。在基辅,他参观了 3 个名胜古迹:

    代码如下。试试看它是如何工作的:

    public class Travellers {
        
        public static void main(String[] args) {
            Country countryUkraine = new Country("Ukraine");
            
            Map countriesVisited = new HashMap<String, List<Country>>();
            countriesVisited.put("George", Arrays.asList(
                    new Country("Japan"), new Country("Brazil"), new Country("Poland"), 
                    new Country("Croatia"), new Country("Nigeria"), new Country("Laos"), 
                    new Country("Germany")
            ));
            countriesVisited.put("Li", Arrays.asList(
                    new Country("Argentina"), countryUkraine, new Country("Italy"), 
                    new Country("Great Britain"), new Country("Nepal")
            ));
            countriesVisited.put("Keith", Arrays.asList(
                    new Country("Egypt"), new Country("Greece"), new Country("Lebanon"), 
                    new Country("Marocco"), new Country("Saudi Arabia"), new Country("Kuwait")
            ));
            countriesVisited.put("Jenny", Arrays.asList(
                    new Country("Norway"), new Country("Canada"), new Country("Russia"), 
                    new Country("Antarctica"), new Country("Finland")
            ));
            
            City cityKyiv = new City("Kyiv");
            countryUkraine.setCities(Arrays.asList(
                    cityKyiv, new City("Odesa"), new City("Lviv")));
            Place placeKyiv1 = new Place("Lavra");
            Place placeKyiv2 = new Place("St Andrew’s Church");
            Place placeKyiv3 = new Place("Museum of Folk Architecture");
            cityKyiv.setPlaces(Arrays.asList(placeKyiv1, placeKyiv2, placeKyiv3));
            
            System.out.println("Countries visited by George: " + countriesVisited.get("George"));
            System.out.println("Countries visited by Li: " + countriesVisited.get("Li"));
            System.out.println("Countries visited by Keith: " + countriesVisited.get("Keith"));
            System.out.println("Countries visited by Jenny: " + countriesVisited.get("Jenny"));
            System.out.println();
            System.out.println("Cities visited by Li in Ukraine: " + countryUkraine.getCities());
            System.out.println();
            System.out.println("Places visited by Li in Kyiv: " + cityKyiv.getPlaces());
        }
    }
    
    class Country {
        String name;
        List<City> cities;
    
        Country(String name) {
            this.name = name;
        }
        
        void setCities(List<City> cities) {
            this.cities = cities;
        }
        
        List<City> getCities() {
            return cities;
        }
        
        @Override public String toString() {
            return name;
        }
    }
    
    class City {
        String name;
        List<Place> places;
        
        City(String name) {
            this.name = name;
        }
        
        void setPlaces(List<Place> places) {
            this.places = places;
        }
        
        List<Place> getPlaces() {
            return places;
        }
        
        @Override public String toString() {
            return name;
        }
    }
    
    class Place {
        String name;
        int rating;
        float latitude;
        float longitude;
        Image img;
        
        Place(String name) {
            this.name = name;
        }
        
        @Override public String toString() {
            return name;
        }
    }
    

    【讨论】:

    • 这是非常有用的实际生活示例和思考复杂对象的方式。谢谢
    • 如何将这个例子与现实世界的场景关联起来,当我们看待它们时没有真正意义的 java 对象,比如 InvoiceData 是键,具有 InvoiceDataList 作为值,而 InvoiceData 列表有 3 个元素。每个元素都有一个名为 items 的字段,当在 items 上调用 getter 时返回 invoiceItemData 自定义对象,我需要根据数据库执行调用设置的 InvoiceNumber
    • 您是否已经尝试将示例与您的场景相关联?
    • 这是我很难将其关联起来的地方,因为我的大脑处理图片比处理一些原始数据更好
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-12
    • 2021-07-14
    • 2016-03-27
    • 2020-07-18
    • 2013-12-02
    • 2016-02-09
    相关资源
    最近更新 更多