Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bill Bai homework #3

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions homework/bill_gc.log
Original file line number Diff line number Diff line change
@@ -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]
53 changes: 53 additions & 0 deletions homework/bill_jvm32_gc.log
Original file line number Diff line number Diff line change
@@ -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]
Binary file added homework/heap_dump_compare.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added homework/jvmgc_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added homework/jvmgc_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added homework/jvmgc_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
114 changes: 100 additions & 14 deletions src/main/java/com/epam/processor/DataProcessor.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,7 +19,12 @@ public DataProcessor(List<RoadAccident> 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

/**
Expand All @@ -27,6 +33,12 @@ public DataProcessor(List<RoadAccident> roadAccidentList){
* @return
*/
public RoadAccident getAccidentByIndex7(String index){
Iterator<RoadAccident> iter = roadAccidentList.iterator();
while(iter.hasNext()) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recomend to use for(accident: list) kind of syntax - no need to create iterator by yourself.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's a homework, I'd like to try more way to implement it.
In other function, I use the for(:) style

RoadAccident rec = iter.next();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is rec? :)

There's a general recommendation to use some meaningful names for variables. with moder IDE it does not take too long time to press Ctrl+Space and get name like roadAccident. And it will save time to new-comers on a project when they read your code.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rec short for record :)

if (rec.getAccidentId().equals(index))
return rec;
}
return null;
}

Expand All @@ -40,7 +52,13 @@ public RoadAccident getAccidentByIndex7(String index){
* @return
*/
public Collection<RoadAccident> getAccidentsByLocation7(float minLongitude, float maxLongitude, float minLatitude, float maxLatitude){
return null;
List<RoadAccident> result = new ArrayList<>();
for (RoadAccident rec : roadAccidentList ) {
if ( inRect(rec, minLongitude, maxLongitude, minLatitude, maxLatitude) ) {
result.add(rec);
}
}
return result;
}

/**
Expand All @@ -51,16 +69,60 @@ public Collection<RoadAccident> getAccidentsByLocation7(float minLongitude, floa
* @return
*/
public Map<String, Long> getCountByRoadSurfaceCondition7(){
return null;
Map<String, Long> result = new HashMap<>();
Multiset<String> roadSurfaceCondition = HashMultiset.create();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool - nice usage of multi collections. I was thinking more about counter as a map value, but this approach is much better than mine :)

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;
}

/**
* find the weather conditions which caused the top 3 number of incidents
* 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<String> getTopThreeWeatherCondition6(){

Multiset<String> weatherConditions = HashMultiset.create();
for (RoadAccident rec : roadAccidentList ) {
weatherConditions.add(rec.getWeatherConditions());
}
ImmutableSortedMap.Builder<String, String> builder = new ImmutableSortedMap
.Builder<String, String>(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<String, String> sortedConditions = builder.build();
List<String> result = sortedConditions.values().asList();
// System.out.println(result);
/*
// If no duplicate count value for WeatherCondition, we can use BiMap
// Another Solution
Multimap<Integer, String> sortedConditions = TreeMultimap.create(Ordering.natural().reverse(), Ordering.natural());
for (String key : weatherConditions.elementSet() ) {
sortedConditions.put(weatherConditions.count(key), key);
}
List<String> result = ImmutableList.copyOf(sortedConditions.values());
*/
return result.subList(0,3);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a pretty cool solution as well!
One minor point - subList will throw exception if list has less then 3 elements.

}

public List<String> getTopThreeWeatherCondition7(){
return null;
Multiset<String> weatherConditions = HashMultiset.create();
for (RoadAccident rec : roadAccidentList ) {
weatherConditions.add(rec.getWeatherConditions());
}
List<Multiset.Entry<String>> topKList = Ordering
.from(Comparator.<Multiset.Entry<String>>comparingInt(e -> e.getCount()))
.greatestOf(weatherConditions.entrySet(),3);
return Lists.transform(topKList, e -> e.getElement());
}

/**
Expand All @@ -71,7 +133,11 @@ public List<String> getTopThreeWeatherCondition7(){
* @return
*/
public Multimap<String, String> getAccidentIdsGroupedByAuthority7(){
return null;
ImmutableMultimap.Builder<String, String> builder = new ImmutableMultimap.Builder<String, String>();
for (RoadAccident rec : roadAccidentList ) {
builder.put(rec.getDistrictAuthority(), rec.getAccidentId());
}
return builder.build();
}


Expand All @@ -80,7 +146,10 @@ public Multimap<String, String> getAccidentIdsGroupedByAuthority7(){


public RoadAccident getAccidentByIndex(String index){
return null;
return roadAccidentList.stream()
.filter(r -> r.getAccidentId().equals(index))
.findFirst().orElse(null);

}


Expand All @@ -93,31 +162,48 @@ public RoadAccident getAccidentByIndex(String index){
* @return
*/
public Collection<RoadAccident> 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());
}

/**
* find the weather conditions which caused max number of incidents
* @return
*/
public List<String> getTopThreeWeatherCondition(){
return null;
Map<String, Long> weatherCondition = roadAccidentList.stream()
.collect(Collectors.groupingBy(RoadAccident::getWeatherConditions,
Collectors.counting()));
return weatherCondition.entrySet().stream()
.sorted(Map.Entry.<String, Long>comparingByValue().reversed())
.limit(3)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
}

/**
* count incidents by road surface conditions
* @return
*/
public Map<String, Long> getCountByRoadSurfaceCondition(){
return null;
Map<String, Long> roadSurfaceCondition = roadAccidentList.stream()
.collect(Collectors.groupingBy(RoadAccident::getRoadSurfaceConditions,
Collectors.counting()));

return roadSurfaceCondition;
}

/**
* To match streaming operations result, return type is a java collection instead of multimap
* @return
*/
public Map<String, List<String>> getAccidentIdsGroupedByAuthority(){
return null;
Map<String, List<String>> authority = roadAccidentList.stream()
.collect(Collectors.groupingBy(RoadAccident::getDistrictAuthority,
Collectors.mapping(RoadAccident::getAccidentId, Collectors.toList())));

return authority;
}

}
4 changes: 2 additions & 2 deletions src/test/java/com/epam/processor/DataProcessorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ public void should_return_null_if_no_such_index(){
@Test
public void should_count_by_road_conditions7(){
Map<String, Long> countByRoadConditions = dataProcessor.getCountByRoadSurfaceCondition7();
assertThat(countByRoadConditions.get("Dry"), is(110277));
assertThat(countByRoadConditions.get("Dry"), is(110277L));
}

@Test
public void should_count_by_road_conditions(){
Map<String, Long> countByRoadConditions = dataProcessor.getCountByRoadSurfaceCondition();
assertThat(countByRoadConditions.get("Dry"), is(110277));
assertThat(countByRoadConditions.get("Dry"), is(110277L));
}

@Test
Expand Down