Here's some imperative code that I'm trying to translate into functional programming code:
public class Person {
String name;
Token token;
public Person(String name, Token token) {
this.name = name;
this.token = token;
}
}
public class Token {
String id;
boolean isValid;
public Token(String id, boolean isValid) {
this.id = id;
this.isValid = isValid;
}
public String getId() { return id; }
public boolean isValid() {return isValid;}
}
public static List<Token> getTokensForPerson(String name) {...}
public static List<Person> getPeople1 (String[] names) {
List<Person> people = new ArrayList<Person> ();
for (String name: names) {
List<Token> tokens = getTokensForPerson(name);
for (Token token: tokens) {
if (token.isValid()) {
people.add(new Person(name, token));
}
}
}
return people;
}
Here's my attempt to do it the functional way.
public static List<Person> getPeople2 (String[] names) {
return Arrays.stream(names).map(name -> getTokensForPerson(name))
.flatMap(tokens -> tokens.stream().filter(token -> token.isValid))
.map(token -> new Person(name, token)) // <== compiler error here. "Cannot resolve symbol 'name'"
.collect(Collectors.toList());
}
However it doesn't compile since in the last map operation I need to refer to name to create the Person object and name is not available at that time. Any ideas?
You can move the map steps inside the flatMap:
return Arrays.stream(names)
.<Person>flatMap(
name -> getTokensForPerson(name).stream()
.filter(Token::isValid)
.map(token -> new Person(name, token)))
.collect(Collectors.toList());
This way you can access name variable as well.
StreamEx-based solution is shorter, though it requires third-party library:
return StreamEx.of(names)
.cross(name -> getTokensForPerson(name).stream())
// Here we have the stream of entries
// where keys are names and values are tokens
.filterValues(Token::isValid)
.mapKeyValue(Person::new)
.toList();
Would it be possible to create TokenExtended class extending Token, and adding the name, and return a List<TokenExtended> from getTokensForPerson instead of the List<Token>?
public class TokenExtended extends Token {
private String name;
public TokenExtended(String name, String id, boolean isValid) {
super(id, isValid);
this.name = name;
}
}
This way your code would work
Arrays.stream(names).map(name -> getTokensForPerson(name)).flatMap(tokens -> tokens.stream().filter(token -> token.isValid))
.map(token -> new Person(token.name, token)).collect(Collectors.toList());
Related
Trying to get my head round the Java 8 streams syntax with a simple example. Had a look at the other similar questions on this topic, but could not find any solutions that would match my example and would work for me.
I have a class as follow
import java.util.List;
public class Car {
private String model;
private String make;
private String carName;
private List<Specification> specification;
public Car(String model, String make, String carName, List<Specification> specification) {
this.model = model;
this.make = make;
this.carName = carName;
this.specification = specification;
}
public String getModel() {
return model;
}
public String getMake() {
return make;
}
public String getCarName() {
return carName;
}
public List<Specification> getSpecification() {
return specification;
}
}
public class Specification {
private String name;
private String value;
public String getName() {
return name;
}
public String getValue() {
return value;
}
public Specification(String name, String value) {
this.name = name;
this.value = value;
}
}
And I have the main method
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class RegisterCar {
public static void main(String[] args) throws IOException {
List<String> carNames = new ArrayList<>();
carNames.add("Audi");
carNames.add("BMW");
carNames.add("Toyota");
List<String> colour = new ArrayList<>();
colour.add("red");
colour.add("white");
}
}
I want to create a list of car object with each item of carNames. But if the car name is 'Audi' or 'BMW' it should create only one object of the class as follow
List<Car> carList = new ArrayList<>();
Specification musicSystem = new Specification("MusicSysten" , "present");
List<Specification> specList= new ArrayList<>();
specList.add(musicSystem);
carList.add(new Car("Q5", "2020", "Audi", specList));
carList.add(new Car("X2", "2020", "BMW", specList));
But if the item is 'Toyota' then it should create two object each for each color.
List<Specification> specListRed= new ArrayList<>();
specListRed.add(musicSystem);
specListRed.add(redColor);
List<Specification> specListWhite= new ArrayList<>();
specListWhite.add(musicSystem);
specListWhite.add(whiteColor);
carList.add(new Car("Camry", "2020", "Toyota", specListRed));
carList.add(new Car("Camry", "2020", "Toyota", specListWhite));
I am trying to write a common method for creating object and adding to a list. I have tried something like this, but I won't create correct two object for Toyota.
carList = carNames.stream()
.map(carName -> new Car(model, make, carName, specList))
.collect(Collectors.toList());
please suggest how can I achieve this with streams.
If you want the name "Toyota" to be used twice it should be contained in the list of names twice, since the lambda in map() is executed for every name.
But inside your map-Call: Where do you get and model & make?
First thing you need to understand is that map operation always preserves same amount (i.e. cardinality) as your input.
If you will pass 3 items - no matter which function you have, result will always be 3.
Next thing worth looking into: flatMap function. This function can transform any amount of inputs to any amount of results! This looks like what we need.
var carList = carNames.stream()
.flatMap(carName -> {
if (carName.equals("Toyota") {
return Stream.of(new Car(model, make, carName, specList1),
new Car(model, make, carName, specList2));
} else {
return Stream.of(new Car(model, make, carName, specList));
})
.collect(Collectors.toList());
Since it's all about creation and holding of object. Foucus on equals() and and hashcode() methods.
#Override
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Car)) {
return false;
}
Car car = (Car) o;
if (carName.equals("BMW")) {
return true;
}
if (carName.equals("Toyota")) {
return false;
}
return Objects.equals(carName , car.carName);
}
#Override
public int hashCode() {
return 1;
}
Driver methood
List<String> carNames = new ArrayList<>();
carNames.add("Audi");
carNames.add("BMW");
carNames.add("Toyota");
carNames.add("Toyota");
List<String> colours = new ArrayList<>();
colours.add("red");
colours.add("white");
Set carList = carNames.stream()
.map(carName -> {
Set ss = new HashSet();
if (carName.equals("Toyota")) {
ss.addAll(colours.stream().map(colr -> new Car("model" , "make" , carName , null)).collect(Collectors.toSet()));
} else {
ss.add(new Car("model" , "make" , carName , null));
}
return ss;
})
.collect(HashSet::new, Set::addAll, Set::addAll);
I'm trying to learn aggregate functions and lambdas in Java. I have a class:
public class Person {
public enum Privilege{
PRIV1, PRIV2, PRIV3, PRIV4, PRIV4
}
private String name;
private Set<Privilege> privileges;
...
}
and a list of objects of this class.
I want to convert it to EnumMap<Privilege, List<String>>
where List contains names of all people having certain privilege. I created a method to do this:
public static Map<Privilege,List<String>> personsByPrivilege(List<Person> personList){
Map<Privilege, List<String>> resultMap = new EnumMap(Privilege.class);
Arrays.asList(Privilege.values())
.stream()
.forEach(p->resultMap.put(p,new ArrayList<String>()));
for(Person p :personList){
Set<Privilege> personsPrivileges = p.getPrivileges();
for(Privilege pr : personsPrivileges){
resultMap.get(pr).add(p.getName());
}
}
return resultMap;
}
How do I do it using aggregate functions?
I mean e.g. personlist.stream().collect style
You could flatten the list of person -> list of privileges into pairs, then groupby by the privilege, and map with the name
public static Map<Person.Privilege, List<String>> personsByPrivilegeB(List<Person> personList) {
return personList.stream()
.flatMap(pers -> pers.getPrivileges().stream().map(priv -> new Object[]{priv, pers.getName()}))
.collect(groupingBy(o -> (Person.Privilege) o[0], mapping(e -> (String) e[0], toList())));
}
You can add a Pair class and use the below code for achieving your goal
return personList.stream().flatMap(p -> {
String name = p.getName();
return p.getPrivileges().stream()
.flatMap(priv -> Stream.of(new NamePriviledge(priv, name)));
}).collect(Collectors.groupingBy(NamePriviledge::getPrivilege, Collectors.mapping(NamePriviledge::getName, Collectors.toList())));
}
class NamePriviledge {
private final Person.Privilege privilege;
private final String name;
public NamePriviledge(Person.Privilege privilege, String name) {
this.privilege = privilege;
this.name = name;
}
public String getName() {
return name;
}
public Person.Privilege getPrivilege() {
return privilege;
}
#Override
public String toString() {
return "NamePriviledge{" +
"privilege=" + privilege +
", name='" + name + '\'' +
'}';
}
}
I've defined the following set of data
Response response = new Response();
List<ObjectTest> objList = new ArrayList<ObjectTest>();
objList.add(new ObjectTest(new Attributes(new FirstName("ab","1"),new LastName("hernandez","2"))));
objList.add(new ObjectTest(new Attributes(new FirstName("jose","1"),new LastName("perez","2"))));
objList.add(new ObjectTest(new Attributes(new FirstName("paco","2"),new LastName("jackson","2"))));
objList.add(new ObjectTest(new Attributes(new FirstName("pedro","1"),new LastName("herrera","2"))));
objList.add(new ObjectTest(new Attributes(new FirstName("juan","2"),new LastName("flores","2"))));
response.setObjectList(objList);
So based on what the user selects I need to be able to get the specific class and the attribute, for example:
if the user selects [Attributes - FirstName - value] the output would be :
ab
jose
paco
pedro
juan
if the user selects [Attributes - LastName- status] the output would be:
2
2
2
2
2
The problem here is that I dont know how to get the specific class in runtime. Also the main object could have any number of classes inside of it like MainClass.ClassA.ClasstB.ClassX.classAttributeValue. The only thing that I know is that the last value is going to be the one that I have to take in that case I have to print classAttributeValue . Any ideas how to solve this using java 8 ?
Assuming your class structure looks something like this:
public static abstract class Attribute {
public final String value;
public final String status;
public Attribute(String value, String status) {
this.value = value;
this.status = status;
}
}
public static class FirstName extends Attribute {
public FirstName(String value, String status) {
super(value, status);
}
}
public static class LastName extends Attribute {
public LastName(String value, String status) {
super(value, status);
}
}
public static class Attributes {
public final FirstName firstName;
public final LastName lastName;
public Attributes(FirstName firstName, LastName lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}
public static class ObjectTest {
public final Attributes attributes;
public ObjectTest(Attributes attributes) {
this.attributes = attributes;
}
}
You can define java.util.function.Function accessors for each stage:
Function<ObjectTest, Attributes> attributes = t -> t.attributes;
Function<Attributes, FirstName> firstName = t -> t.firstName;
Function<Attributes, LastName> lastName = t -> t.lastName;
Function<Attribute, String> value = t -> t.value;
Function<Attribute, String> status = t -> t.status;
And combine them like so:
Function<ObjectTest, String> attributeFirstNameValue =
attributes.andThen(firstName).andThen(value);
Function<ObjectTest, String> attributeLastNameStatus =
attributes.andThen(lastName).andThen(status);
Then apply the combined accessor to the list:
objList.stream().map(attributeFirstNameValue).forEach(System.out::println);
objList.stream().map(attributeLastNameStatus).forEach(System.out::println);
Is it critical to use this class structure?
In your example using a associative container is more suitable.
For example you can create class with structure like this:
Firstly you shoud something for itterate by Tree:
class DynamicObjectNode {
private HashMap<String, DynamicObjectNode> childs = new HashMap<>();
public HashMap<String, DynamicObjectNode> getChilds() {
return childs;
}
}
All values should be in leafs:
class DynamicObjectNodeValue<T> extends DynamicObjectNode {
public DynamicObjectNodeValue(T value) {
this.value = value;
}
private T value;
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
#Override
public HashMap<String, DynamicObjectNode> getChilds() {
return null; //Tree leafs should not has childs
}
}
If you need to work with this as objects. You can use wrapped class like this:
class FirstNameAttribute extends DynamicObjectNode{
private static final String NameValueProperty = "NameValue";
private static final String StatusProperty = "Status";
private DynamicObjectNodeValue<String> nameValue = new DynamicObjectNodeValue<String>("Default name");
private DynamicObjectNodeValue<Integer> status = new DynamicObjectNodeValue<Integer>(1);
public FirstNameAttribute() {
getChilds().put(NameValueProperty, nameValue);
getChilds().put(StatusProperty, status);
}
public String getName() {
return nameValue.getValue();
}
public Integer getStatus() {
return status.getValue();
}
public void setName(String val) {
nameValue.setValue(val);
}
public void setStatus(Integer val) {
status.setValue(val);
}
}
So, with this code you can iterate it as a Tree and get values Dynamic.
And you can use this as objects to call some methods.
Thank you for your responses, what I finally did was to use JsonNode and based on the attribute I wanted to get I was iterating the same object and assign the result to se same object for example:
Json Response:
Object.Person1.firstName.value
I created an array of that and split it by "." then I created a for and I used this
jsonNode = jsonNode.get(inputArray[x]);
at the end the last element of the array is the one that I need so I added some logic to get it.
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
I want to sort a list within an entity with Java 8 Comparators.
Can someone help me to replace this here --> thenComparing(???)
#Test
public void testSort() {
// given
List<Person> values = Lists.newArrayList(
new Person("you", Lists.newArrayList(new Todo("A"))),
new Person("me", Lists.newArrayList(new Todo("B"), new Todo("A"), new Todo("C"))),
new Person("me", Lists.newArrayList(new Todo("A"))),
new Person("me", Lists.newArrayList(new Todo("D")))
);
// when
List<Person> result = values
.stream()
.sorted(Comparator
.comparing(Person::getName))
.thenComparing(???) <-- TODO
.collect(Collectors.toList());
// then
assertEquals(4, result.size());
Person person_1 = result.get(0);
assertEquals("me", person_1.getName());
assertEquals(1, person_1.getTodos().size());
assertEquals("A", person_1.getTodos().get(0).getName());
Person person_2 = result.get(1);
assertEquals("me", person_2.getName());
assertEquals(3, person_2.getTodos().size());
assertEquals("A", person_2.getTodos().get(0).getName());
assertEquals("B", person_2.getTodos().get(1).getName());
assertEquals("C", person_2.getTodos().get(2).getName());
Person person_3 = result.get(2);
assertEquals("me", person_3.getName());
assertEquals(1, person_3.getTodos().size());
assertEquals("D", person_3.getTodos().get(0).getName());
Person person_4 = result.get(3);
assertEquals("you", person_4.getName());
assertEquals(1, person_4.getTodos().size());
assertEquals("A", person_4.getTodos().get(0).getName());
}
#Data
#AllArgsConstructor
class Todo {
private String name;
}
#Data
#AllArgsConstructor
class Person {
private String name;
private List<Todo> todos;
}
Expected sort result:
Person(name=me, todos=[Todo(name=A)])
Person(name=me, todos=[Todo(name=A), Todo(name=B), Todo(name=C)])
Person(name=me, todos=[Todo(name=D)])
Person(name=you, todos=[Todo(name=A)])
By editing the classes Person and Todo, in combination with the fact String sorting is built into java this becomes easier.
so lets first think about this, we can use thenComparing after the first comparing by casting the Todo List to a string, then sorting by that string. we edit the Person class first by adding a function that returns the Todo list as a string. It will end up looking like:
public class Person {
private String name;
private List<Todo> todos;
public Person(String name, List<Todo> todos) {
this.name = name;
this.todos = todos;
}
public String getName() {
return name;
}
public List<Todo> getTodos() {
return todos;
}
public String getTodoListAsString() {
return this.todos.stream()
.map(Todo::getName)
.sorted()
.collect(Collectors.joining());
}
}
We also want to change the Todo class to implement Comparable so that we can map the Todo list in them into a sorted list. We can accomplish this by editing the Todo class as such:
public class Todo implements Comparable<Todo> {
private String name;
public Todo(String name) {
this.name = name;
}
public String getName() {
return name;
}
#Override
public int compareTo(Todo o) {
return this.getName().compareTo(o.getName());
}
}
Now we can use that in the stream as such:
List<Person> result = values
.stream()
.sorted(Comparator.comparing(Person::getName)
.thenComparing(Person::getTodoListAsString))
.map(person -> new Person(person.getName(),
person.getTodos()
.stream()
.sorted()
.collect(Collectors.toList())))
.collect(Collectors.toList())