Java 8 : Map Lambda expression - java

I have a Map<String, List<Object>> multiFieldMap and I need to itereate over its value set and add the value to multiFieldsList as below
public List<Object> fetchMultiFieldsList() {
List<Object> multiFieldsList = new ArrayList<Object>();
for (Entry<String, List<Object>> entry : multiFieldMap.entrySet()) {
String entityName = entry.getKey();
List<Object> ids = entry.getValue();
for (Object id : ids) {
Object entity = queryService.query(entityName, queryService.property("id").eq(id));
multiFieldsList.add(entity);
}
}
return multiFieldsList;
}
Am wondering can this method be simplified further?

You can use the Streams API :
List<Object> multiFieldsList =
multiFieldMap.entrySet()
.stream()
.flatMap(e -> e.getValue()
.stream()
.map(o -> queryService.query(e.getKey(), queryService.property("id").eq(o))))
.collect(Collectors.toList());

You can indeed use a stream to simplify you inner loop.
You can replace:
List<Object> ids = entry.getValue();
for (Object id : ids) {
Object entity = queryService.query(entityName, queryService.property("id").eq(id));
multiFieldsList.add(entity);
}
with:
entry.getValue().map(
id -> queryService.query(entityName, queryService.property("id").eq(id))
).forEach(multiFieldsList::add);
But you don't really gain much from that. Your choice...
See #Eran's answer for a "full stream" solution.

You can simplify it like this:
public List<Object> fetchMultiFieldsList() {
List<Object> multiFieldsList = new ArrayList<>();
multiFieldMap.forEach( (entityName, ids ) ->
ids.forEach( id -> multiFieldsList.add(
queryService.query(entityName, queryService.property("id").eq(id)))
)
);
return multiFieldsList;
}
Unless you want to use the Stream API, the method Map.forEach might be the biggest win regarding code simplification as you don’t need to deal with Map.Entry and its generic signature anymore…

Related

creating Map from List is not giving expected result

I have the two list objects as shown below, from which i'm creating the map object.
List<Class1> list1;
List<Class2> list2;
HashMap<String,String> map = new HashMap<>();
for(Class1 one : list1){
if(one.isStatus()){
map.put(one.getID(),one.getName());
}
}
//iterating second list
for(Class2 two : list2){
if(two.isPerformed()){
map.put(two.getID(),two.getName());
}
}
The above code works fine , want the above to be written using streams.
Below is the sample code using streams().
map = list1.stream().filter(one.isStatus()).collect(toMap(lst1 -> lst1.getID(), lst1.getName());
map = list2.stream().filter(...);
But the "map" is not giving the expected result when written using stream() API.
Stream concatenation Stream.concat may be applied here to avoid map.putAll
Map<String, String> map = Stream.concat(
list1.stream()
.filter(Class1::isStatus)
.map(obj -> Arrays.asList(obj.getID(), obj.getName())),
list2.stream()
.filter(Class2::isPerformed)
.map(obj -> Arrays.asList(obj.getID(), obj.getName()))
) // Stream<List<String>>
.collect(Collectors.toMap(
arr -> arr.get(0), // key - ID
arr -> arr.get(1),
(v1, v2) -> v1 // keep the first value in case of possible conflicts
));
The code above uses a merge function (v1, v2) -> v1 to handle possible conflicts when the same ID occurs several times in list1 and/or list2 to keep the first occurrence.
However, the following merge function allows joining all the occurrences into one string value (v1, v2) -> String.join(", ", v1, v2).
I'm not sure what expected result you're not seeing but I created a minimal working example that you should be able to adapt for your own use case.
public class Main {
public static void main(String[] args) {
List<Person> personList = new ArrayList<>();
Map<Integer, String> personMap = personList.stream()
.filter(Person::isStatus)
.collect(Collectors.toMap(person -> person.id, person -> person.name));
}
private static class Person {
public String name;
public int id;
public boolean isStatus() {
return true;
}
}
}
Try this,
List<Class1> list1;
List<Class2> list2;
Map<String, String> map1 = list1.stream().filter(Class1::isStatus).collect(Collectors.toMap(Class1::getId, Class1::getName));
Map<String, String> map2 = list2.stream().filter(Class2::isPerformed).collect(Collectors.toMap(Class2::getId, Class2::getName));
map1.putAll(map2);

How to return the entire list if filter is false

I have this list
List<String> lstStr = new ArrayList<>();
lstStr.add("1");
lstStr.add("2");
lstStr.add("3");
lstStr.add("4");
lstStr.add("5");
When I search for the string "1" its should return a List<String> = ["1"] and if search string is not in the list for example "0" it should return the entire List <String> =["1","2","3","4","5"]. Can this be achieved using java stream? Please show an example.
I have tried this using the code below but I could get the entire list when I search for say "0"
List<String> filteredLst = lstStr.stream()
.filter(data-> "1".equalsIgnoreCase(data))
.collect(Collectors.toList());
filteredLst.forEach(data2 -> System.out.println(data2));
Thanks in advance.
You can partition on a predicate and return the non-empty list:
Map<Boolean, List<String>> split = lstStr.stream()
.collect(Collectors.partitioningBy("1"::equalsIgnoreCase));
List<String> filteredLst = split.get(Boolean.TRUE).isEmpty() ?
split.get(Boolean.FALSE) : //can also use lstStr instead
split.get(Boolean.TRUE);
Collectors.partitioningBy("1"::equals) will return a 2-entry map, where true will be the key of entries that meet your filter, and false the key of the rest.
filteredLst should contain the value mapped to true if that is not empty, or the value of false otherwise (which would surely be the same as he original list)
If you don't need to handle duplicates then you can do the following :
static List<String> getOneOrAll(List<String> list, String element) {
return list.stream()
.filter(element::equalsIgnoreCase)
.findFirst()
.map(Collections::singletonList)
.orElse(list);
}
...
List<String> result = getOneOrAll(lstStr, "1");
Otherwise you can pass in a predicate and filter the duplicates:
static <T> List<T> getOneOrAll(List<T> list, Predicate<T> predicate) {
List<T> filteredList = list.stream()
.filter(predicate)
.collect(toList());
return filteredList.isEmpty() ? list : filteredList;
}
...
List<String> result = getOneOrAll(lstStr, "1"::equals);
// or
List<String> resultIgnoringCase = getOneOrAll(lstStr, "1"::equalsIgnoreCase);
A possible simple util for this would be using contains :
List<String> findAndReturnValue(List<String> lstStr, String value) {
return lstStr.contains(value) ? Arrays.asList(value) : lstStr;
}
and for possible duplicates in the list:
List<String> findAndReturnValue(List<String> lstStr, String value) {
return lstStr.contains(value) ?
lstStr.stream()
.filter(a -> a.equalsIgnoreCase(value)) // condition as in question
.collect(Collectors.toList()) : lstStr;
}
To reduce the complexity for the cases where the element would be present in the list, the rather simpler solution would be collecting to a list and then checking for the size :
List<String> findAndReturn(List<String> lstStr, String value) {
List<String> filteredLst = lstStr.stream()
.filter(data -> data.equalsIgnoreCase(value))
.collect(Collectors.toList());
return filteredLst.isEmpty() ? lstStr : filteredLst;
}
You can use custom Collector for this purpose.
li.stream().filter(d->"0".equalsIgnoreCase(d)).collect(
Collector.of(
ArrayList::new,
ArrayList::add,
(a, b) -> {
a.addAll(b);
return a;
},
a -> a.isEmpty() ? li : a
)
);
Look for Collector docs: https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collector.html
First Check if your list contains that value .
If yes then go for filtering the list
or else just print the Previous list
if(!lstStr.contains("0")) {
lstStr.forEach(data2 -> System.out.println(data2));
}else {
List<String> filteredLst = lstStr.stream()
.filter(data-> "1".equalsIgnoreCase(data))
.collect(Collectors.toList());
filteredLst.forEach(data2 -> System.out.println(data2));
}

How can I convert this source code to lambda?

It consists of a map in the list object. I try to match lists with the same id by comparing them through loop statements. How can I convert to lambda?
List<Map<String, String>> combineList = new ArrayList<>(); // Temp List
for(Map titleMap : titleList) { // Name List
for(Map codeMap : codeList) { // Age List
if(titleMap.get("ID").equals(codeMap.get("ID"))) { // compare Id
Map<String,String> tempMap = new HashMap<>();
tempMap.put("ID", titleMap.get("ID"));
tempMap.put("NAME", titleMap.get("NAME"));
tempMap.put("AGE", codeMap.get("AGE"));
combineList.add(tempMap);
}
}
}
You are already doing it in efficient manner. So if you want you could change same code to just use stream().forEach or if want to use streams more do it as below:
titleList.stream()
.forEach(titleMap ->
combineList.addAll(
codeList.stream()
.filter(codeMap -> titleMap.get("ID").equals(codeMap.get("ID")))
.map(codeMap -> {
Map<String, Object> tempMap = new HashMap<>();
tempMap.put("ID", titleMap.get("ID"));
tempMap.put("NAME", titleMap.get("NAME"));
tempMap.put("ID", codeMap.get("ID"));
tempMap.put("AGE", codeMap.get("AGE"));
return tempMap;
})
.collect(Collectors.toList())
)
);
Notice that you have to filter from the codeList each time because your condition is that way. Try using a class in place of Map to be more efficient, cleaner and effective.

Set created from complex List

So I have a list containing duplicated entities from database with the same "Id" (it's not the real Id but kind of) but a different CreatedDate.
So I would like to have the latest entity from duplicates with the latest CreatedDate.
Example I have a list of created users :
RealId|CreatedDate|Id|Name
1|20170101|1|User1
2|20170102|1|User1Modified
3|20170103|2|User2
4|20170104|2|User2Modified
From that list what is the best way to obtain :
RealId|CreatedDate|Id|Name
2|20170102|1|User1Modified
4|20170104|2|User2Modified
This is my first idea
List<T> r = query.getResultList();
Set<T> distinct = r.stream().filter(x -> {
List<T> clones = r.stream()
.filter(y -> y.getId() == x.getId())
.collect(Collectors.toList());
T max = clones.stream()
.max(Comparator.comparing(AbstractEntityHistory::getEntryDate))
.get();
return max.getNumber() == x.getNumber();
}).collect(Collectors.toSet());
An other idea I have is to make it order descending by the date then do distinct().collect() like :
Set<T> distinct2 = r.stream().sorted((x,y) -> {
if(x.getEntryDate().isBefore(y.getEntryDate())) {
return 1;
} else if(x.getEntryDate().isAfter(y.getEntryDate())) {
return -1;
} else {
return 0;
}
}).distinct().collect(Collectors.toSet());
Here, T overrides equals which watch for the RealId if they are equal else use reflection to watch every other field.
Try this:
List<YourObject> collect = activities
.stream()
.collect(Collectors.groupingBy(
YourObject::getId,
Collectors.maxBy(Comparator.comparing(YourObject::getCreatedDate))))
.entrySet()
.stream()
.map(e -> e.getValue().get())
.collect(Collectors.toList());
Here is used Collectors.groupingBy to create a Map<Integer, Optional<YourObject>>, grouped by id and most recent createDate. The you get the entrySet for this map and collect it to a List.
Without java8 functional stuff:
Map<Long, Item> map = new HashMap<>();
for (Item item: items) {
Item old = map.get(item.getId());
if (old == null || old.getDate().before(item.getDate())) {
map.put(item.getId(), item);
}
}
List<Item> result = new ArrayList<Item>(map.values());

Searching a Hashmap for values of type list, retrieving an element of list and adding it to a new Collection to return

i'm pretty new when it comes to Java but i'll hopefully clear this up.
I currently have one class and within that class i have a TreeMap called "departments" which takes an argument of >:
TreeMap <String, List<String>> department;
Within each department such as HR, Builders etc there are a list of names of people who work there. Such as:
HR: Janet, Jones, Bob
What i'd like to do is search through department to find all departments (keys) that contain someone who's called "bob" for instance and add them to a collection to make a return.
Can anyone help with this, i've been pulling my hair out for a few days! So far i'm this far with the method although clearly nowhere near complete!
public List<String> selectValues( String... aValue)
{
for(String eachDept : department.keySet()){
Collection<String> peopleInTheDept = department.get(eachDept);
for(String person : aValue){
if(peopleInTheDept.contains(person)){
result.add(person);
}
}
}
System.out.println(result);
}
Just like OH GOD SPIDERS predicted, there is a Java 8 stream solution:
TreeMap <String, List<String>> department = new TreeMap<>();
department.put("AB", Arrays.asList("Bob", "Truus", "Miep"));
department.put("CD", Arrays.asList("Jan", "Kees", "Huub"));
department.put("EF", Arrays.asList("Jan", "Piet", "Bert"));
String aValue = "Jan";
Map<String,List<String>> result = department.entrySet().stream()
// filter out those departments that don't contain aValue
.filter(entry -> entry.getValue().contains(aValue))
// collect the matching departments back into a map
.collect(Collectors.toMap(k -> k.getKey(), k -> k.getValue()));
// print the result
result.forEach((k,v)-> System.out.println(k + " " + v.toString()));
Which prints:
EF [Jan, Piet, Bert]
CD [Jan, Kees, Huub]
Pre Java 8 solution:
Map<String, List<String>> result2 = new TreeMap<>();
for(String eachDept : department.keySet()){
List<String> peopleInTheDept = department.get(eachDept);
if(peopleInTheDept.contains(aValue)){
result2.put(eachDept, department.get(eachDept));
}
}
for (String s : result2.keySet()){
System.out.println(s + " " + result2.get(s));
}
This prints exactly the same as my Java 8 code.
Here is some java 8 Streams fancy solution to get the information and fill your list.
public void selectValues(String... values)
{
for(String value : values) {
results.addAll(department.entrySet()
.stream()
.filter(entry -> entry.getValue().contains(value))
.map(entry -> entry.getKey())
.collect(Collectors.toList()));
}
}
What I understand that you need a list of department who's Value contain certain name. Here is the solution of Java 8 version.
public static void main(String[] args) {
// Initialization of map
Map<String, List<String>> department = new TreeMap<>();
department.put("HR", new ArrayList<>());
department.put("ACC", new ArrayList<>());
department.put("MK", new ArrayList<>());
department.get("HR").add("bob");
department.get("HR").add("John");
department.get("ACC").add("Kim");
department.get("ACC").add("bob");
department.get("MK").add("XXX");
// Here is the solution
// depts is a list of string which contains the name of
// department who's value contains 'bob'
List<String > depts = department.entrySet()
.stream()
.filter(i -> i.getValue().contains("bob"))
.map(Map.Entry::getKey)
.collect(Collectors.toList());
// Printing out the result
depts.stream().forEach(System.out::println);
}
In traditional way
List<String> depts = new ArrayList<>();
for (Map.Entry<String , List<String >> it :
department.entrySet()) {
if (it.getValue().contains("bob"))
depts.add(it.getKey());
}
Java8's streams is certainly an elegant solution...but if you are restricted with the use of java 8 then how about your departments be of type TreeMap<String, Set<String>>. If you insist in using a list its very similiar. Just change the Set to List. There is a contains method in List interface too.
TreeMap<String, List<String>> departments = new TreeMap<String, List<String>>();
ArrayList<String> myList = new ArrayList<String>();
myList.add("person1");
departments.put("dep1", myList);
String[] aValue = {"person1","person2"};
public void selectValues( String... aValue)
{
for(String eachDept : departments.keySet()){
List<String> peopleInTheDept = departments.get(eachDept);
for(String person : aValue){
if(peopleInTheDept.contains(person)){
result.add(person)
}
}
}
System.out.println(result);
}
Something similar to this maybe?

Categories

Resources