List of objects - get object fields distinct count - java

For a list of objects I have to check for (some) fields:
all objects having same value for that field
all objects having a different value for that field
class Person {
final String name;
final int age;
final int group;
public Person( final String name, final int age, final int group ) {
this.name = name;
this.age = age;
this.group = group;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public int getGroup() {
return this.group;
}
}
public static <T> long distinctByField( final List<Person> personList, final Function<Person, T> field ) {
return personList.stream()
.map( field )
.distinct().count();
}
public static void main( final String[] args ) {
final List<Person> personList = Arrays.asList(
new Person( "Fred", 25, 1 ),
new Person( "Bill", 22, 1 ),
new Person( "Fred", 27, 1 ),
new Person( "Lisa", 25, 1 )
);
System.out.println( distinctByField( personList, Person::getName ) );
System.out.println( distinctByField( personList, Person::getAge ) );
System.out.println( distinctByField( personList, Person::getGroup ) );
}
With result of stream/distinct/count I can compare with current list size:
if count == 1 : all objects having same value for that field
if count == list.size : all objects having different value for that field
Drawback is, i have to stream for every interested field.
Is it possible to do this with one query (for a list of interested fields) ?

It's possible using reflection:
public class ReflectionTest {
class Person {
final String name;
final int age;
final int group;
public Person(final String name, final int age, final int group) {
this.name = name;
this.age = age;
this.group = group;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public int getGroup() {
return this.group;
}
}
#DisplayName("should determine distinct fields")
#Test
public void distinct() {
final List<Person> personList = Arrays.asList(new Person("Fred", 25, 1),
new Person("Bill", 22, 1),
new Person("Fred", 27, 1),
new Person("Lisa", 25, 1));
Map<String,Long> fieldCountMap = Stream.of("name", "age", "group")
.map(fieldName -> ReflectionUtils.findField(Person.class, fieldName))
.filter(Objects::nonNull)
.collect(Collectors.toMap(Field::getName, field -> personList.stream().map(person -> getField(field, person)).distinct().count()));
assertEquals(3,fieldCountMap.get("name"));
assertEquals(1,fieldCountMap.get("group"));
assertEquals(3,fieldCountMap.get("age"));
}
//extracted into a method because field.get throws a checked exception
Object getField(Field field, Person object) {
try {
return field.get(object);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}

Let me first mention: This is a more or less huge downside in code quality (searching manually through all fields, using an extra class to store the results). And I doubt, this would be more efficient in terms of computation time or memory. By logic you will have to touch every field of every person and store values that already occured in order to find the distinct count for every field. Which is exactly what your solution with 3 streams does. I'd advice you to stay with it.
But here is a solution. I built a collector, that collects in one run all the different values for each field into a custom class.
static class PersonStatistic {
Set<String> names = new HashSet<>();
Set<Integer> ages = new HashSet<>();
Set<Integer> groups = new HashSet<>();
}
public static void main(final String[] args) {
final List<Person> personList = Arrays.asList(
new Person("Fred", 25, 1),
new Person("Bill", 22, 1),
new Person("Fred", 27, 1),
new Person("Lisa", 25, 1));
PersonStatistic personStatistic = personList.stream().collect(
// Create new Statistic
PersonStatistic::new,
// Merge A Person into statistic
(statistic, person) -> {
statistic.names.add(person.name);
statistic.ages.add(person.age);
statistic.groups.add(person.group);
},
// Merge second statistic into first
(stat1, stat2)-> {
stat1.names.addAll(stat2.names);
stat1.ages.addAll(stat2.ages);
stat1.groups.addAll(stat2.groups);
});
System.out.println(personStatistic.names.size());
System.out.println(personStatistic.ages.size());
System.out.println(personStatistic.groups.size());
}

Related

Do multi level grouping and summing using Java Stream API

I have a class
public class Person {
private String name;
private String country;
private String city;
private String pet;
private int totalCountryToCityCount;
private int petCount;
public Person(String name, String country, String city, String pet, int total, int petCount) {
this.name = name;
this.country = country;
this.city = city;
this.pet = pet;
this.totalCountryToCityCount = total;
this.petCount = petCount;
}
public String getName() {
return name;
}
public String getCountry() {
return country;
}
public String getCity() {
return city;
}
public String getPet() {
return pet;
}
public int getPetCount() {
return petCount;
}
public int getTotalCountryToCityCount() {
return totalCountryToCityCount;
}
}
and Given a list of Person class, I have do aggregations based upon the different properties of the class.
For eg -
Person person1 = new Person("John", "USA", "NYC", "Max", 1, 2);
Person person2 = new Person("Steve", "UK", "London", "Lucy", 2, 8);
Person person3 = new Person("Anna", "USA", "NYC", "Max", 4, 32);
Person person4 = new Person("Mike", "USA", "Chicago", "Duke", 5, 1);
Person person5 = new Person("Test", "INDIA", "HYD", "Tommy", 4, 32);
Person person6 = new Person("Test1", "INDIA", "HYD", "Tommy", 4, 65);
Person person7 = new Person("Tim", "USA", "Chicago", "Duke", 5, 111);
Person person8 = new Person("Tim", "USA", "Chicago", "Puke", 5, 111);
Person person9 = new Person("Test1", "INDIA", "DELHI", "Tommy", 4, 65);
List<Person> persons = Arrays
.asList(person1, person2, person3, person4, person5, person6, person7, person8,
person9);
Now I need to get a result such that I should get the total "totalCountryToCityCount" based upon the combinations of country and city and I should get total "petCount" based upon combinations of country,city and pet. I am able to get them separately using groupingBy and summingint
private Map<String, Map<String, Integer>> getTotalCountForCountry(List<Person> persons) {
return persons.stream().collect(groupingBy(Person::getCountry, getCityCount()));
}
public Collector<Person, ?, Map<String, Integer>> getCityCount() {
return groupingBy(Person::getCity, summingInt(Person::getTotal));
}
public Map<String, Map<String, Map<String, Integer>>> threeLevelGrouping(List<Person> persons) {
return persons
.stream().collect(
groupingBy(Person::getCountry, groupByCityAndPetName()
)
);
}
private Collector<Person, ?, Map<String, Map<String, Integer>>> groupByCityAndPetName() {
return groupingBy(Person::getCity, groupByPetName());
}
private Collector<Person, ?, Map<String, Integer>> groupByPetName() {
return groupingBy(Person::getPet, summingInt(Person::getPetCount));
}
which gives the result
{USA={Chicago={Puke=111, Duke=112}, NYC={Max=34}}, UK={London={Lucy=8}}, INDIA={DELHI={Tommy=65}, HYD={Tommy=97}}}
{USA={Chicago=15, NYC=5}, UK={London=2}, INDIA={DELHI=4, HYD=8}}
but the actual result which I want is :-
{USA={Chicago={15,{Puke=111, Duke=112}}, NYC={5,{Max=34} }, UK={London={2, {Lucy=8}}, INDIA={DELHI={4, {Tommy=65}}, , HYD={8,{Tommy=97}}}}
is there a way to achieve the same using Java stream API
I also tried using the code -
personList.stream().collect(groupingBy(person -> person.getCountry(), collectingAndThen(reducing(
(a, b) -> new Person(a.getName(), a.getCountry(), a.getCity(), a.getPet(),
a.getTotal() + b.getTotal(), a.getPetCount() + b.getPetCount())),
Optional::get)))
.forEach((country, person) -> System.out.println(country + person));
But was getting the result -
USAPerson{name='John', country='USA', city='NYC'}
UKPerson{name='Steve', country='UK', city='London'}
INDIAPerson{name='Test', country='INDIA', city='HYD'}
with the counts surprisingly removed
What you are looking for really is Collectors::teeing, but only available in java-12:
System.out.println(
persons.stream()
.collect(Collectors.groupingBy(
Person::getCountry,
Collectors.groupingBy(
Person::getCity,
Collectors.teeing(
Collectors.summingInt(Person::getTotalCountryToCityCount),
Collectors.groupingBy(
Person::getPet,
Collectors.summingInt(Person::getPetCount)
),
SimpleEntry::new
)
))));
A back-port for java-8 it is available here.

What is the best way to get the result through Java8 function?

I need to filter elements and then sort based on certain column. Post that I would need to find the unique entries based on combination of columns. Since it is file processing, pipe(|) is used as delimiter to denote the column value.
String s1= "12|Thor|Asgaurd|1000000|Avenger|Active"
String s2= "234|Iron man|New York|9999999|Avenger|Active"
String s3= "420|Loki|Asgaurd|||Inactive"
String s4= "12|Thor|Asgaurd Bank|1000000|Avenger HQ|Active"
Data first needs to be filtered based on the Active/Inactive status. Then it needs to be sorted based on 4th column. Lastly, the uniqueness needs to be maintained by combining column 1,2,3.
Expected Output =
"234|Iron man|New York|9999999|Avenger|Active"
"12|Thor|Asgaurd|1000000|Avenger|Active"
Creating a model class and parsing the string is the way to go, but if for some reaseon you don't want to do that you can do it this way:
import java.util.Comparator;
import java.util.stream.Collectors;
import java.util.stream.Stream;
List<String> result = Stream.of(s1, s2, s3, s4)
.filter(s -> s.split("\\|")[5].equals("Active"))
.sorted(Comparator.comparing(e -> e.split("\\|")[4]))
.collect(Collectors.toList());
First of all you should create an Object which represents your String data. Something like this:
public class MyObject {
private int id;
private String name;
private String location;
private Integer value;
private String category;
private String state;
public MyObject(String entry) {
String[] parts = entry.split("\\|");
if (parts.length != 6) {
throw new IllegalArgumentException("entry has not 6 parts");
}
id = Integer.parseInt(parts[0]);
name = parts[1];
location = parts[2];
try {
value = Integer.parseInt(parts[3]);
} catch (NumberFormatException ignored) {
}
category = parts[4];
state = parts[5];
}
// getters
#Override
public String toString() {
return String.join("|", String.valueOf(id), name, location, String.valueOf(value), category, state);
}
}
With this you can create a Stream of objects from your Strings and to the filter, sort and distinct operations afterwards:
Collection<MyObject> result = Stream.of(s1, s2, s3, s4)
.map(MyObject::new)
.filter(o -> "Active".equals(o.getState()))
.sorted(Comparator.comparing(MyObject::getValue).reversed())
.collect(Collectors.toMap(o -> Arrays.asList(o.getId(), o.getName()),
Function.identity(), (o1, o2) -> o1, LinkedHashMap::new))
.values();
result.forEach(System.out::println);
After the map operation you filter the values by state and sort them by column 4 (value in my case). At the end you collect all the values in a map for the distinct operation. Add all values you need distinction for to the Arrays.asList(). As values the map takes all the original values (Function.identity()). For duplicates we keep the first value ((o1, o2) -> o1) and we are using a LinkedHashMap to keep the order of the items. At the end wee use only the values of the map.
If you need a List instead of a Collection use new ArrayList(result).
The result will be this:
234|Iron man|New York|9999999|Avenger|Active
12|Thor|Asgaurd|1000000|Avenger|Active
It seems like you're unable to filter while everything is string only.
Try this,
create a new model class which can hold your columns.
Ex:
class MyData{
private String name;
private String city;
private String distance;
private String organization;
private String status;
//And create Getter Setter method for all above fields.
}
Now came to your main class where you can play with your code stuff.
Map<MyData> map = new HashMap<MyData>();
MyData myData = new MyData();
myData.setName("Thor");
myData.setCity("Asgaurd");
myData.setDistance("1000000");
myData.setOrganization("Avenger");
myData.setStatus("Active");
map.put(12, myData);
//Same thing for all other data (note: use the loop for data insertion in map)
Map<String, MyData> sorted = map.entrySet().stream().sorted(comparingByValue()).collect(toMap(e -> e.getKey(), e -> e.getValue().getName(), (e1, e2) -> e2,LinkedHashMap::new));
System.out.println("map after sorting by values: " + sorted);
You can solve your task this way:
Firstly, just create POJO(Plain Old Java Object) and override the toString() method.
class MarvelPerson {
private Integer id;
private String name;
private String origin;
private Integer point = null;
private String faction;
private String status;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOrigin() {
return origin;
}
public void setOrigin(String origin) {
this.origin = origin;
}
public Integer getPoint() {
return point;
}
public void setPoint(Integer point) {
this.point = point;
}
public String getFaction() {
return faction;
}
public void setFaction(String faction) {
this.faction = faction;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
#Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(id);
builder.append("|");
builder.append(name);
builder.append("|");
builder.append(origin);
builder.append("|");
if(point != null) {
builder.append(point);
}
builder.append("|");
if(faction != null) {
builder.append(faction);
}
builder.append("|");
builder.append(status);
return builder.toString();
}
}
Then, you should write the parser from string to MarvelPerson. Side note: Carefully, my implementation is pretty basic, and I suppose it should be modified because I may not have foreseen some corner cases.
class PersonParser {
static MarvelPerson parse(String data) {
MarvelPerson person = new MarvelPerson();
String[] array = data.split("\\|", -1);
person.setId(Integer.parseInt(array[0]));
person.setName(array[1]);
person.setOrigin(array[2]);
if(!array[3].isEmpty()) {
person.setPoint(Integer.parseInt(array[3]));
}
if(!array[4].isEmpty()) {
person.setFaction(array[4]);
}
person.setStatus(array[5]);
return person;
}
}
And then your solution:
public class Test {
public static void main(String[] args) {
List<MarvelPerson> list = new ArrayList<>();
list.add(PersonParser.parse("12|Thor|Asgaurd|1000000|Avenger|Active"));
list.add(PersonParser.parse("234|Iron man|New York|9999999|Avenger|Active"));
list.add(PersonParser.parse("420|Loki|Asgaurd|||Inactive"));
list.add(PersonParser.parse("12|Thor|Asgaurd Bank|1000000|Avenger HQ|Actie"));
list.stream()
.filter(marvelPerson -> marvelPerson.getStatus().equals("Active"))
.sorted((o1, o2) -> o1.getPoint() <= o2.getPoint() ? 1 : -1)
.forEach(marvelPerson -> {
System.out.println(marvelPerson.toString());
});
}
}
The output to be printed:
234|Iron man|New York|9999999|Avenger|Active
12|Thor|Asgaurd|1000000|Avenger|Active

How to collect properties of List<Map> by unique property using MultiMap?

I have List of stories. Using unique property(id) I want to collect keyword and targeting as list of values. Can I do this with MultiMap? Or is there other library for this?
[{
id = 1,
title = Onboarding,
keyword = new joinee,
targeting = finance
}, {
id = 1,
title = Onboarding,
keyword = training,
targeting = HR
}]
The Desired output must like this :
{
id = 1,
title = Onboarding,
keyword = [new joinee,training], //may be keywords - plural
targeting = [HR,finance]
}
Sample my tried Code as follows:
package prac;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JavaPrac {
public static void main(String[] args) {
Multimap<Integer, Map> multiMap = ArrayListMultimap.create();
List<Map> stories=new ArrayList();
Map story1=new HashMap();
story1.put("id", 1);
story1.put("title", "Onboarding");
story1.put("keyword","new joinee");
story1.put("targeting","finance");
Map story2=new HashMap();
story2.put("id", 1);
story2.put("title", "Onboarding");
story2.put("keyword","training");
story2.put("targeting","HR");
stories.add(story1);
stories.add(story2);
System.out.println(stories);
stories.forEach((story) -> {
multiMap.put((Integer) story.get("id"), story);
});
}
}
A multimap can only store multiple values per key but what you want is to combine those multiple values so that you get one element that has the same id and title as well as a collection of keywords and targeting information. Thus it would probably be best to either have something like MultiStory or already have Story contain those collections.
I'd suggest using proper objects instead of just maps but with maps and Java 8 lambdas you could use compute() etc. to build maps that contain collections and combine maps that don't.
Here's an example of how you'd do it with maps. Note that this is very bad style and an example using proper pojos will follow:
Disclaimer: example based on the OP's code, not recommended (read text above)
//Problem 1: we don't know the type of the values, i.e. we could put anything for "id" etc.
Map<String, Object> story1=new HashMap<>();
story1.put("id", 1);
story1.put("title", "Onboarding");
story1.put("keyword","new joinee");
story1.put("targeting","finance");
Map<String, Object> story2=new HashMap<>();
story2.put("id", 1);
story2.put("title", "Onboarding");
story2.put("keyword","training");
story2.put("targeting","HR");
List<Map<String, Object>> stories=new ArrayList<>();
stories.add(story1);
stories.add(story2);
Map<Integer, Map<String, Object>> combined = new HashMap<>();
stories.forEach((story) -> {
//Problem 2: because we don't know the type of the values we need a lot of nasty casts
Map<String, Object> combinedStory = combined.computeIfAbsent( (Integer)story.get( "id" ), k -> new HashMap<String, Object>() );
combinedStory.put("id", story.get( "id" ) );
combinedStory.put("title", story.get( "title" ) );
//Problem 3: the combined map would look a lot like your "story" maps but would contain different types
((List<String>)combinedStory.computeIfAbsent( "keyword", v -> new List<String>() )).add( (String)story.get("keyword") );
((List<String>)combinedStory.computeIfAbsent( "targeting", v -> new List<String>() )).add( (String)story.get("targeting") );
});
Using POJOs
Here's a greatly simplified example of how you'd do it with proper Java objects (POJOs). Note that those are meant to resemble your code as much as possible and there are a lot of other issues but addressing those would be way too much here and better designed code would be a lot larger and probably harder to understand - after all it's just meant to show you a difference.
First let's define our classes (for simplicity I made the fields public, you'd normally not do that):
class Story {
public final int id;
public String title;
public String keyword;
public String targeting;
public Story(int storyId) {
id = storyId ;
}
}
class MultiStory {
public final int id;
public String title;
public Set<String> keywords = new HashSet<>();
public Set<String> targetingInfo = new HashSet<>();
public MultiStory( int storyId ) {
id = storyId ;
}
}
Then let's reiterate the code above:
Story story1=new Story( 1 );
story1.title = "Onboarding";
story1.keyword = "new joinee";
story1.targeting = "finance";
Story story2=new Story( 1 );
story2.title = "Onboarding";
story2.keyword = "training";
story2.targeting = "HR";
List<Story> stories=new ArrayList<>();
stories.add(story1);
stories.add(story2);
Map<Integer, MultiStory> combined = new HashMap<>();
stories.forEach((story) -> {
MultiStory multiStory = combined.computeIfAbsent( story.id, v -> new MultiStory( story.id ) );
multiStory.title = story.title;
multiStory.keywords.add( story.keyword );
multiStory.targetingInfo.add( story.targeting );
});
As you can see, there are no casts needed and it's clear what fields are available (though not necessarily filled) which makes it easier to reason about the code and spot errors (the compiler can help a lot here which it couldn't to in the example that uses maps).
Here is a solution using classes to represent the story and tags:
public static void main(String[] args) {
TagsCollector app = new TagsCollector();
app.go();
}
private void go() {
List<Story> stories = createStories();
System.out.println(stories);
Map<Long, Tags> tagsById = collectTags(stories);
tagsById.forEach((aLong, tags) -> System.out.println(tags));
}
private List<Story> createStories() {
return Arrays.asList(
new Story(1, "Onboarding", "new joinee", "finance"),
new Story(1, "Onboarding", "training", "HR")
);
}
private Map<Long, Tags> collectTags(List<Story> stories) {
Map<Long, Tags> tagsById = new HashMap<>();
stories.forEach(s -> {
Tags tags = tagsById.computeIfAbsent(s.id, v -> new Tags(s));
tags.getKeywords().add(s.getKeyword());
tags.getTargetings().add(s.getTargeting());
});
return tagsById;
}
Class used to represent the Story:
public class Story {
private final long id;
private final String title;
private final String keyword;
private final String targeting;
public Story(long id, String title, String keyword, String targeting) {
this.id = id;
this.title = title;
this.keyword = keyword;
this.targeting = targeting;
}
public long getId() {
return id;
}
public String getTitle() {
return title;
}
public String getKeyword() {
return keyword;
}
public String getTargeting() {
return targeting;
}
#Override
public String toString() {
return String.format("Story %s, title=%s, keyword=%s, targeting=%s", id, title, keyword, targeting);
}
}
Class used to represent the Tags:
public class Tags {
private final long id;
private final String title;
private final List<String> keywords = new ArrayList<>();
private final List<String> targetings = new ArrayList<>();
Tags(Story story) {
this.id = story.id;
this.title = story.title;
}
public List<String> getKeywords() {
return keywords;
}
public List<String> getTargetings() {
return targetings;
}
#Override
public String toString() {
return String.format("Tags for id %s, title:%s: keywords=%s, targetings=%s", id, title, keywords, targetings);
}
}
Output
[Story 1, title=Onboarding, keyword=new joinee, targeting=finance, Story 1, title=Onboarding, keyword=training, targeting=HR]
Tags for id 1, title:Onboarding: keywords=[new joinee, training], targetings=[finance, HR]
Yes, you can do that with a Multimap. First I would define a pojo for Story in order to make things clearer:
public class Story {
private int id;
private String title;
private String keyword;
private String targeting;
//getters setters
}
Second you need to define a key with hashcode and equals.
public static class StoryKey {
private final int id;
private final String title;
public StoryKey(int id, String title) {
this.id = id;
this.title = title;
}
//getters
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
StoryKey storyKey = (StoryKey) o;
if (id != storyKey.id) return false;
return title != null ? title.equals(storyKey.title) : storyKey.title == null;
}
#Override
public int hashCode() {
int result = id;
result = 31 * result + (title != null ? title.hashCode() : 0);
return result;
}
The code will look like:
ArrayListMultimap<StoryKey, Story> multiMap = ArrayListMultimap.create();
List<Story> stories = new ArrayList();
Story story1 = new Story();
story1.setId(1);
story1.setTitle("Onboarding");
story1.setKeyword("training");
story1.setTargeting("HR");
Story story2 = new Story();
story2.setId(1);
story2.setTitle("Onboarding");
story2.setKeyword("new joinee,");
story2.setTargeting("finance");
stories.add(story1);
stories.add(story2);
System.out.println(stories);
stories.
forEach((story) -> {
multiMap.put(new StoryKey(story.getId(), story.getTitle()), story);
});
multiMap.keys().forEach(key ->
System.out.println(
"id =" + key.getId() +
" title =" + key.getTitle()+
"keyword =" + multiMap.get(key).stream().map(story->story.getKeyword()).collect(Collectors.toList()).toString()+
"targeting ="+ multiMap.get(key).stream().map(story->story.getTargeting()).collect(Collectors.toList()).toString())
);

Adding Multiple Set<String> in Java

I have two sets as: set1 and set2 that I want to combine.
set1 contains personID and place as: [1-NY, 2-CA, 3-MD, 1-TX, 3-VA]
set2 contains personName and place as: [John-NY, Bill-CA, Ron-CA, Rick-MD, John-TX, Rick-VA]
I want to combine both the set such that I will get the output of personID, personName and place as: [1-John-NY, 2-Bill-CA, 2-Ron-CA, 3-Rick-MD, 1-John-TX, 3-Rick-VA].
Basically the thing is: I want to use "place" as the anchor to combine.
Set<String> set1 = new LinkedHashSet<String>();
Set<String> set2 = new LinkedHashSet<String>();
Set<String> combination = new LinkedHashSet<String>();
combination.addAll(set1);
combination.addAll(set2);
But, I am not able to get the output in my expected way. Any suggestion please.
Thanks!
You should rethink your approach a bit. In order to combine these two sets you should create some kind of look-up table. I would use simple HashMap for this.
The code is really self-explanatory, but fell free to ask questions)
Using Java 8:
Set<String> personIds = new LinkedHashSet<>(Arrays.asList("1-NY", "2-CA", "3-MD", "1-TX", "3-VA"));
Set<String> personNames = new LinkedHashSet<>(Arrays.asList("John-NY", "Bill-CA", "Ron-CA", "Rick-MD", "John-TX", "Rick-VA"));
Map<String, String> personIdMap = personIds.stream().map(v -> v.split("-"))
.collect(Collectors.toMap(v -> v[1], v -> v[0]));
Set<String> combination = new LinkedHashSet<>();
personNames.forEach(name -> {
final String[] split = name.split("-");
final String personId = personIdMap.get(split[1]);
combination.add(personId + '-' + name);
});
Using Java 7:
Set<String> personIds = new LinkedHashSet<>(Arrays.asList("1-NY", "2-CA", "3-MD", "1-TX", "3-VA"));
Set<String> personNames = new LinkedHashSet<>(Arrays.asList("John-NY", "Bill-CA", "Ron-CA", "Rick-MD", "John-TX", "Rick-VA"));
Map<String, String> personIdMap = new HashMap<>();
for (String id : personIds) {
final String[] split = id.split("-");
personIdMap.put(split[1], split[0]);
}
Set<String> combination = new LinkedHashSet<>();
for (String name : personNames) {
final String[] split = name.split("-");
final String personId = personIdMap.get(split[1]);
combination.add(personId + '-' + name);
}
As user chrylis suggests, you could use class for this propose. First, create a class Person.class to store the required values: person ID / person name / place name. For simplifying the process, a constructor with 3 parameters is used here to construct the object, but it's not the only choice. By the way, I strongly suggest you to use a unique value for each person.
public Person(String id, String name, String place) {
this.id = id;
this.name = name;
this.place = place;
}
Then create a method to combine the different information stored in the person class.
public String getCombination() {
return String.format("%s-%s-%s", id, name, place);
}
Now you can put the data into the set combinations:
Set<Person> people = new LinkedHashSet<>();
people.add(new Person("1", "John", "NY"));
people.add(new Person("2", "Bill", "CA"));
people.add(new Person("2", "Ron", "CA"));
people.add(new Person("3", "Rick", "MD"));
people.add(new Person("1", "John", "TX"));
people.add(new Person("3", "Rick", "VA"));
Set<String> combinations = new LinkedHashSet<>();
for (Person p : people) {
combinations.add(p.getCombination());
}
Here's the full implementation of class Person.
public class Person {
private String id; // maybe place id ?
private String name;
private String place;
public Person(String id, String name, String place) {
this.id = id;
this.name = name;
this.place = place;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPlace(String place) {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public String getCombination() {
return String.format("%s-%s-%s", id, name, place);
}
}

Reduce returns unpredictable results for parallel stream

I have wrote the following code sample with java stream reduce:
Person reducedPerson = Person.getPersons().stream()
.parallel() //will return surprising result
.reduce(new Person(), (intermediateResult, p2) -> {
intermediateResult.setAge(intermediateResult.getAge() + p2.getAge());
return intermediateResult;
},
(ir1, ir2) -> {
ir1.setAge(ir1.getAge() + ir2.getAge());
return ir1;
});
System.out.println(reducedPerson);
model:
public class Person {
String name;
Integer age;
public Person() {
age = 0;
name = "default";
}
//...
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public static Collection<Person> getPersons() {
List<Person> persons = new ArrayList<>();
persons.add(new Person("Vasya", 12));
persons.add(new Person("Petya", 32));
persons.add(new Person("Serj", 10));
persons.add(new Person("Onotole", 18));
return persons;
}
}
Each code sample execution returns different result:
example:
Person{name='default', age=256}
or
Person{name='default', age=248}
I have loclized that problem inside combiner because in sequental stream code executes correctly.
Please help to correct combiner.
P.S.
expected result: person with name 'default' and age 72(sum ges of all pepsons in list)
P.S.
same code for Integer as reduce result works properly:
Integer age = Person.getPersons().stream()
.parallel()
.reduce(0, (intermediateResult, p2) -> {
intermediateResult = intermediateResult + p2.getAge();
return intermediateResult;
}, (ir1, ir2) -> {
System.out.println("combiner");
ir1 = ir1 + ir2;
return ir1;
});
System.out.println(age);
To perform mutable reduction, use collect:
reducedPerson = Person.getPersons().parallelStream()
.collect(
Person::new,
(p, q) -> p.setAge(p.getAge() + q.getAge()),
(p, q) -> p.setAge(p.getAge() + q.getAge())
);
collect is specifically designed to accumulate into mutable containers safely even in parallel.
As Boris noted, the problem is mutation within a stream.
Most stream operations accept parameters that describe user-specified
behavior, such as the lambda expression w -> w.getWeight() passed to
mapToInt in the example above. To preserve correct behavior, these
behavioral parameters:
must be non-interfering (they do not modify the stream source); and in
most cases must be stateless (their result should not depend on any
state that might change during execution of the stream pipeline).
https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
Here is version using reduce, and the more straightforward version using maptoint and sum.
class gstackoverflow{
public static void main(String... args) {
Person reducedPerson = Person.getPersons().stream()
.parallel() //will NOT return surprising result
.reduce(new Person("default",0),
(ir1, ir2) -> //no longer mutates
new Person(String.join(",", ir1.getName(), ir2.getName()), ir1.getAge() + ir2.getAge())
);
System.out.println(reducedPerson);
//here is a clean(er) way to do it:
int totalAge = Person.getPersons().stream()
.parallel() //will NOT return surprising result
.mapToInt(Person::getAge)
.sum();
System.out.println(totalAge);
}
}
class Person {//no longer mutable
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
final String name;
final Integer age;
//no args constructor removed
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public static Collection<Person> getPersons() {
List<Person> persons = new ArrayList<>();
persons.add(new Person("Vasya", 12));
persons.add(new Person("Petya", 32));
persons.add(new Person("Serj", 10));
persons.add(new Person("Onotole", 18));
return persons;
}
#Override
public String toString() {
final StringBuilder sb = new StringBuilder("Person{");
sb.append("name='").append(name).append('\'');
sb.append(", age=").append(age);
sb.append('}');
return sb.toString();
}
}

Categories