-
-
Save vinoct6/775cc01711e6f965433f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vinoth.java8.interfaces; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.stream.Collectors; | |
public class Test1 { | |
public static void main(String[] args){ | |
Activity a1 = new Activity(null,"Vinoth"); | |
Activity a2 = new Activity("1","Kumar"); | |
Activity a3 = new Activity("2","Varsh"); | |
Activity a4 = new Activity("3","Vinoth"); | |
Activity a5 = new Activity("4","AAA"); | |
Activity a6 = new Activity(null,"Varsh"); | |
List<Activity> activities = Arrays.asList(a1,a2,a3,a4,a5,a6); | |
List<Activity> filteredActivity = squashDTO(activities); | |
List<Activity> expandedActivity = expandDTO(filteredActivity); | |
System.out.println(expandedActivity); | |
} | |
private static List<Activity> expandDTO(List<Activity> dtoList){ | |
List<Activity> newList = new ArrayList<Activity>(); | |
for(Activity a : dtoList){ | |
for(String activityId : a.getActivityIds()){ | |
Activity n = new Activity(activityId,a.getName()); | |
newList.add(n); | |
} | |
} | |
return newList; | |
} | |
private static List<Activity> squashDTO(List<Activity> dtoList){ | |
List<Activity> filteredActivity = new ArrayList<>(); | |
Map<String, List<Activity>> activityMap = dtoList.stream().collect(Collectors.groupingBy(Activity::getName)); | |
activityMap.forEach(( key,value) ->{ | |
Activity a = new Activity(value.get(0).getName()); | |
List<String> activityIds = new ArrayList<>(); | |
for(Activity aa : value){ | |
activityIds.add(aa.getActivityId()); | |
} | |
a.setActivityIds(activityIds); | |
filteredActivity.add(a); | |
}); | |
return filteredActivity; | |
} | |
} | |
class Activity{ | |
String name; | |
String activityId; | |
List<String> activityIds = new ArrayList<>(); | |
public Activity(String name){ | |
this.name = name; | |
} | |
public Activity(String id, String name){ | |
this.name = name; | |
this.activityId = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public String getActivityId() { | |
return activityId; | |
} | |
public List<String> getActivityIds() { | |
return activityIds; | |
} | |
public void setActivityIds(List<String> activityIds) { | |
this.activityIds = activityIds; | |
} | |
@Override | |
public String toString() { | |
return "Activity [name=" + name + ", activityId=" + activityId | |
+ ", activityIds=" + activityIds + "]\n"; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment