diff --git a/homework/bill_gc.log b/homework/bill_gc.log new file mode 100644 index 0000000..e5e453e --- /dev/null +++ b/homework/bill_gc.log @@ -0,0 +1,16 @@ +Java HotSpot(TM) 64-Bit Server VM (25.20-b23) for windows-amd64 JRE (1.8.0_20-b26), built on Jul 30 2014 13:51:23 by "java_re" with MS VC++ 10.0 (VS2010) +Memory: 4k page, physical 8271460k(3665084k free), swap 10237540k(4273076k free) +CommandLine flags: -XX:InitialHeapSize=132343360 -XX:MaxHeapSize=2117493760 -XX:+PrintGC -XX:+PrintGCTimeStamps -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC +3.961: [GC (Allocation Failure) 23808K->21424K(125952K), 0.0080784 secs] +4.551: [GC (Allocation Failure) 53438K->52064K(125952K), 0.0110631 secs] +5.875: [GC (Allocation Failure) 83678K->82752K(125952K), 0.0129618 secs] +5.888: [Full GC (Ergonomics) 82752K->82716K(201216K), 0.0267797 secs] +6.612: [GC (Allocation Failure) 114034K->123740K(221184K), 0.0133295 secs] +6.625: [Full GC (Ergonomics) 123740K->113436K(273920K), 0.0038620 secs] +8.864: [GC (Allocation Failure) 165577K->205660K(282624K), 0.0266288 secs] +8.890: [Full GC (Ergonomics) 205660K->164636K(384512K), 0.0206266 secs] +10.402: [GC (Allocation Failure) 216917K->215868K(418304K), 0.0119434 secs] +14.423: [GC (Allocation Failure) 309699K->308060K(418304K), 0.0319572 secs] +14.455: [Full GC (Ergonomics) 308060K->307997K(593920K), 0.0288107 secs] +16.737: [GC (Allocation Failure) 401938K->410461K(655872K), 0.0324336 secs] +16.770: [Full GC (Ergonomics) 410461K->400157K(776192K), 0.0112225 secs] diff --git a/homework/bill_jvm32_gc.log b/homework/bill_jvm32_gc.log new file mode 100644 index 0000000..5590840 --- /dev/null +++ b/homework/bill_jvm32_gc.log @@ -0,0 +1,53 @@ +Java HotSpot(TM) Client VM (25.51-b03) for windows-x86 JRE (1.8.0_51-b16), built on Jun 8 2015 18:00:23 by "java_re" with MS VC++ 10.0 (VS2010) +Memory: 4k page, physical 8271460k(4177156k free), swap 10237540k(4761452k free) +CommandLine flags: -XX:ErrorFile=C:\Users\Bill\java_error_in_IDEA_%p.log -XX:InitialHeapSize=16777216 -XX:MaxHeapSize=1073741824 -XX:+PrintGC -XX:+PrintGCTimeStamps -XX:-UseLargePagesIndividualAllocation +0.322: [GC (Allocation Failure) 4416K->1040K(15872K), 0.0043299 secs] +0.398: [GC (Allocation Failure) 5456K->3128K(15872K), 0.0035010 secs] +0.480: [GC (Allocation Failure) 7544K->4146K(15872K), 0.0033180 secs] +0.508: [GC (Allocation Failure) 8562K->4319K(15872K), 0.0028429 secs] +0.515: [GC (Allocation Failure) 8726K->4362K(15872K), 0.0014477 secs] +0.546: [GC (Allocation Failure) 8778K->5109K(15872K), 0.0023394 secs] +0.571: [GC (Allocation Failure) 9525K->5040K(15872K), 0.0018788 secs] +0.619: [GC (Allocation Failure) 9456K->6239K(15872K), 0.0045714 secs] +0.639: [GC (Allocation Failure) 10655K->9123K(15872K), 0.0026407 secs] +0.653: [GC (Allocation Failure) 13539K->12218K(16640K), 0.0025906 secs] +0.656: [Full GC (Allocation Failure) 12218K->12218K(16640K), 0.0091288 secs] +0.694: [GC (Allocation Failure) 19579K->16967K(28348K), 0.0058126 secs] +0.722: [GC (Allocation Failure) 24834K->22086K(30052K), 0.0046649 secs] +0.727: [Full GC (Allocation Failure) 22086K->22086K(30052K), 0.0112068 secs] +0.778: [GC (Allocation Failure) 35490K->30043K(51368K), 0.0098116 secs] +0.830: [GC (Allocation Failure) 44315K->34207K(51368K), 0.0127499 secs] +0.882: [GC (Allocation Failure) 48479K->38497K(52776K), 0.0119572 secs] +0.894: [Full GC (Allocation Failure) 38497K->38497K(52776K), 0.0277656 secs] +1.057: [GC (Allocation Failure) 61409K->43770K(88932K), 0.0232897 secs] +1.567: [GC (Allocation Failure) 68410K->49161K(88932K), 0.0228224 secs] +1.774: [GC (Allocation Failure) 73801K->54209K(88932K), 0.0144856 secs] +2.660: [Full GC (Metadata GC Threshold) 69102K->42564K(88932K), 0.0449064 secs] +3.355: [GC (Allocation Failure) 71044K->47328K(102944K), 0.0108748 secs] +3.904: [GC (Allocation Failure) 75808K->52938K(102944K), 0.0210788 secs] +4.027: [GC (Allocation Failure) 81418K->56317K(102944K), 0.0125392 secs] +4.355: [GC (Allocation Failure) 84797K->57277K(102944K), 0.0092810 secs] +4.707: [GC (Allocation Failure) 85757K->60982K(102944K), 0.0120037 secs] +5.090: [Full GC (Metadata GC Threshold) 75873K->58456K(102944K), 0.0899523 secs] +6.323: [GC (Allocation Failure) 97496K->67513K(141332K), 0.0300465 secs] +7.357: [GC (Allocation Failure) 102999K->78400K(141332K), 0.0350410 secs] +7.672: [Full GC (Metadata GC Threshold) 87567K->65335K(141332K), 0.1162865 secs] +9.394: [GC (Allocation Failure) 108983K->70979K(157984K), 0.0274511 secs] +10.300: [GC (Allocation Failure) 114627K->74486K(157984K), 0.0302167 secs] +10.983: [GC (Allocation Failure) 118134K->76286K(157984K), 0.0271287 secs] +11.780: [GC (Allocation Failure) 119934K->79470K(157984K), 0.0233156 secs] +13.111: [GC (Allocation Failure) 123118K->81341K(157984K), 0.0249988 secs] +13.268: [Full GC (Metadata GC Threshold) 85218K->66155K(157984K), 0.2353434 secs] +14.551: [GC (Allocation Failure) 110379K->69711K(159988K), 0.0204552 secs] +15.486: [GC (Allocation Failure) 113935K->71958K(159988K), 0.0192550 secs] +16.629: [GC (Allocation Failure) 116182K->75935K(159988K), 0.0278797 secs] +17.686: [GC (Allocation Failure) 120159K->83147K(159988K), 0.0294754 secs] +18.268: [GC (Allocation Failure) 127371K->94111K(159988K), 0.0380868 secs] +18.725: [GC (Allocation Failure) 138335K->105327K(159988K), 0.0443552 secs] +23.391: [GC (Allocation Failure) 149551K->113632K(159988K), 0.0503248 secs] +57.356: [GC (Allocation Failure) 157856K->118215K(163324K), 0.0372537 secs] +57.394: [Full GC (Allocation Failure) 118215K->78406K(163324K), 0.2369982 secs] +92.345: [GC (Allocation Failure) 130821K->81545K(189624K), 0.0168981 secs] +117.588: [GC (Allocation Failure) 133961K->82092K(189624K), 0.0214815 secs] +139.777: [GC (Allocation Failure) 134508K->82945K(189624K), 0.0225465 secs] +219.740: [GC (Allocation Failure) 135361K->82582K(189624K), 0.0140721 secs] diff --git a/homework/heap_dump_compare.png b/homework/heap_dump_compare.png new file mode 100644 index 0000000..5d43d19 Binary files /dev/null and b/homework/heap_dump_compare.png differ diff --git a/homework/jvmgc_2.png b/homework/jvmgc_2.png new file mode 100644 index 0000000..aa759c4 Binary files /dev/null and b/homework/jvmgc_2.png differ diff --git a/homework/jvmgc_3.png b/homework/jvmgc_3.png new file mode 100644 index 0000000..38820b9 Binary files /dev/null and b/homework/jvmgc_3.png differ diff --git a/homework/jvmgc_4.png b/homework/jvmgc_4.png new file mode 100644 index 0000000..06017d8 Binary files /dev/null and b/homework/jvmgc_4.png differ diff --git a/src/main/java/com/epam/processor/DataProcessor.java b/src/main/java/com/epam/processor/DataProcessor.java index d2f37d7..e52aca5 100644 --- a/src/main/java/com/epam/processor/DataProcessor.java +++ b/src/main/java/com/epam/processor/DataProcessor.java @@ -1,11 +1,12 @@ package com.epam.processor; import com.epam.data.RoadAccident; -import com.google.common.collect.Multimap; +import com.google.common.base.*; +import com.google.common.collect.*; -import java.util.Collection; -import java.util.List; -import java.util.Map; +import java.util.*; +import java.util.Optional; +import java.util.stream.Collectors; /** * This is to be completed by mentees @@ -18,7 +19,12 @@ public DataProcessor(List roadAccidentList){ this.roadAccidentList = roadAccidentList; } - + public static boolean inRect(RoadAccident roadAccident, float minLongitude, float maxLongitude, float minLatitude, float maxLatitude) { + float longitude = roadAccident.getLongitude(); + float latitude = roadAccident.getLatitude(); + return ( (longitude>=minLongitude && longitude<=maxLongitude) + && (latitude>=minLatitude && latitude<=maxLatitude) ); + } // First try to solve task using java 7 style for processing collections /** @@ -27,6 +33,12 @@ public DataProcessor(List roadAccidentList){ * @return */ public RoadAccident getAccidentByIndex7(String index){ + Iterator iter = roadAccidentList.iterator(); + while(iter.hasNext()) { + RoadAccident rec = iter.next(); + if (rec.getAccidentId().equals(index)) + return rec; + } return null; } @@ -40,7 +52,13 @@ public RoadAccident getAccidentByIndex7(String index){ * @return */ public Collection getAccidentsByLocation7(float minLongitude, float maxLongitude, float minLatitude, float maxLatitude){ - return null; + List result = new ArrayList<>(); + for (RoadAccident rec : roadAccidentList ) { + if ( inRect(rec, minLongitude, maxLongitude, minLatitude, maxLatitude) ) { + result.add(rec); + } + } + return result; } /** @@ -51,7 +69,16 @@ public Collection getAccidentsByLocation7(float minLongitude, floa * @return */ public Map getCountByRoadSurfaceCondition7(){ - return null; + Map result = new HashMap<>(); + Multiset roadSurfaceCondition = HashMultiset.create(); + for (RoadAccident rec : roadAccidentList ) { + roadSurfaceCondition.add(rec.getRoadSurfaceConditions()); + } + for (String key : roadSurfaceCondition.elementSet() ) { +// System.out.println(key+" count:"+roadSurfaceCondition.count(key)); + result.put(key, Long.valueOf(roadSurfaceCondition.count(key))); + } + return result; } /** @@ -59,8 +86,43 @@ public Map getCountByRoadSurfaceCondition7(){ * as example if there were 10 accidence in rain, 5 in snow, 6 in sunny and 1 in foggy, then your result list should contain {rain, sunny, snow} - top three in decreasing order * @return */ + public List getTopThreeWeatherCondition6(){ + + Multiset weatherConditions = HashMultiset.create(); + for (RoadAccident rec : roadAccidentList ) { + weatherConditions.add(rec.getWeatherConditions()); + } + ImmutableSortedMap.Builder builder = new ImmutableSortedMap + .Builder(Ordering.natural().reverse()); + for (String key : weatherConditions.elementSet() ) { + String countStr = String.format("%08d %s", weatherConditions.count(key), key); + System.out.println(countStr); + builder.put(countStr, key); + } + ImmutableSortedMap sortedConditions = builder.build(); + List result = sortedConditions.values().asList(); +// System.out.println(result); +/* + // If no duplicate count value for WeatherCondition, we can use BiMap + // Another Solution + Multimap sortedConditions = TreeMultimap.create(Ordering.natural().reverse(), Ordering.natural()); + for (String key : weatherConditions.elementSet() ) { + sortedConditions.put(weatherConditions.count(key), key); + } + List result = ImmutableList.copyOf(sortedConditions.values()); +*/ + return result.subList(0,3); + } + public List getTopThreeWeatherCondition7(){ - return null; + Multiset weatherConditions = HashMultiset.create(); + for (RoadAccident rec : roadAccidentList ) { + weatherConditions.add(rec.getWeatherConditions()); + } + List> topKList = Ordering + .from(Comparator.>comparingInt(e -> e.getCount())) + .greatestOf(weatherConditions.entrySet(),3); + return Lists.transform(topKList, e -> e.getElement()); } /** @@ -71,7 +133,11 @@ public List getTopThreeWeatherCondition7(){ * @return */ public Multimap getAccidentIdsGroupedByAuthority7(){ - return null; + ImmutableMultimap.Builder builder = new ImmutableMultimap.Builder(); + for (RoadAccident rec : roadAccidentList ) { + builder.put(rec.getDistrictAuthority(), rec.getAccidentId()); + } + return builder.build(); } @@ -80,7 +146,10 @@ public Multimap getAccidentIdsGroupedByAuthority7(){ public RoadAccident getAccidentByIndex(String index){ - return null; + return roadAccidentList.stream() + .filter(r -> r.getAccidentId().equals(index)) + .findFirst().orElse(null); + } @@ -93,7 +162,9 @@ public RoadAccident getAccidentByIndex(String index){ * @return */ public Collection getAccidentsByLocation(float minLongitude, float maxLongitude, float minLatitude, float maxLatitude){ - return null; + return roadAccidentList.stream() + .filter(r -> inRect(r, minLongitude, maxLongitude, minLatitude, maxLatitude) ) + .collect(Collectors.toList()); } /** @@ -101,7 +172,14 @@ public Collection getAccidentsByLocation(float minLongitude, float * @return */ public List getTopThreeWeatherCondition(){ - return null; + Map weatherCondition = roadAccidentList.stream() + .collect(Collectors.groupingBy(RoadAccident::getWeatherConditions, + Collectors.counting())); + return weatherCondition.entrySet().stream() + .sorted(Map.Entry.comparingByValue().reversed()) + .limit(3) + .map(Map.Entry::getKey) + .collect(Collectors.toList()); } /** @@ -109,7 +187,11 @@ public List getTopThreeWeatherCondition(){ * @return */ public Map getCountByRoadSurfaceCondition(){ - return null; + Map roadSurfaceCondition = roadAccidentList.stream() + .collect(Collectors.groupingBy(RoadAccident::getRoadSurfaceConditions, + Collectors.counting())); + + return roadSurfaceCondition; } /** @@ -117,7 +199,11 @@ public Map getCountByRoadSurfaceCondition(){ * @return */ public Map> getAccidentIdsGroupedByAuthority(){ - return null; + Map> authority = roadAccidentList.stream() + .collect(Collectors.groupingBy(RoadAccident::getDistrictAuthority, + Collectors.mapping(RoadAccident::getAccidentId, Collectors.toList()))); + + return authority; } } diff --git a/src/test/java/com/epam/processor/DataProcessorTest.java b/src/test/java/com/epam/processor/DataProcessorTest.java index de1b3e8..9199257 100644 --- a/src/test/java/com/epam/processor/DataProcessorTest.java +++ b/src/test/java/com/epam/processor/DataProcessorTest.java @@ -57,13 +57,13 @@ public void should_return_null_if_no_such_index(){ @Test public void should_count_by_road_conditions7(){ Map countByRoadConditions = dataProcessor.getCountByRoadSurfaceCondition7(); - assertThat(countByRoadConditions.get("Dry"), is(110277)); + assertThat(countByRoadConditions.get("Dry"), is(110277L)); } @Test public void should_count_by_road_conditions(){ Map countByRoadConditions = dataProcessor.getCountByRoadSurfaceCondition(); - assertThat(countByRoadConditions.get("Dry"), is(110277)); + assertThat(countByRoadConditions.get("Dry"), is(110277L)); } @Test