Filter objects from a list that have the same member

filter array of objects javascript
javascript filter array multiple values
javascript filter array of objects by another array of objects
javascript search filter array of objects
typescript filter array of objects by property value
js filter object by value
angular 4 filter array of objects
javascript array.filter multiple arguments

I have a list of objects. The object looks like this:

public class Slots {
  String slotType;
  Visits visit;
}


public class Visits {
  private long visitCode;
  private String agendaCode;
  private String scheduledTime;
  private String resourceType;
  private String resourceDescription;
  private String visitTypeCode;
  ...
}

I need to find the elements that have the same agendaCode, visitTypeCode and scheduledTime and for the life of me I can't get it done.

I tried this:

Set<String> agendas = slotsResponse.getContent().stream()
    .map(Slots::getVisit)
    .map(Visits::getAgendaCode)
    .collect(Collectors.toUnmodifiableSet());

Set<String> visitTypeCode = slotsResponse.getContent().stream()
    .map(Slots::getVisit)
    .map(Visits::getVisitTypeCode)
    .collect(Collectors.toUnmodifiableSet());

Set<String> scheduledTime = slotsResponse.getContent().stream()
    .map(Slots::getVisit)
    .map(Visits::getScheduledTime)
    .collect(Collectors.toUnmodifiableSet());

List<Slots> collect = slotsResponse.getContent().stream()
    .filter(c -> agendas.contains(c.getVisit().getAgendaCode()))
    .filter(c -> visitTypeCode.contains(c.getVisit().getVisitTypeCode()))
    .filter(c -> scheduledTime.contains(c.getVisit().getScheduledTime()))
    .collect(Collectors.toList());

But it's not doing what I thought it would. Ideally I would have a list of lists, where each sublist is a list of Slots objects that share the same agendaCode, visitTypeCode and scheduledTime. I struggle with functional programming so any help or pointers would be great!

This is Java 11 and I'm also using vavr.


Since you mentioned you're using vavr, here is the vavr way to solve this question.

Supposed you have your io.vavr.collection.List (or Array or Vector or Stream or similar vavr collection) of visits:

List<Visits> visits = ...;

final Map<Tuple3<String, String, String>, List<Visits>> grouped =
    visits.groupBy(visit ->
        Tuple.of(
            visit.getAgendaCode(),
            visit.getVisitTypeCode(),
            visit.getScheduledTime()
        )
    );

Or with a java.util.List of visits:

List<Visits> visits = ...;

Map<Tuple3<String, String, String>, List<Visits>> grouped = visits.stream().collect(
    Collectors.groupingBy(
        visit ->
            Tuple.of(
                visit.getAgendaCode(),
                visit.getVisitTypeCode(),
                visit.getScheduledTime()
            )
    )
);

JavaScript Array Filter: Filtering Array Elements Based on a Test , This tutorial shows you how to use the JavaScript array filter method to filter elements in an Suppose you have an array of city objects where each object contains two The following example returns the same result as the example above:. Just FYI - this is no more "modern" - my version and this one both became available in the same compiler release ;) – Reed Copsey Mar 6 '13 at 17:13 I really thought it was added in a newer version of C#.


The easiest way is to define a new class with necessaries fields (agendaCode, visitTypeCode and scheduledTime). Don't forget about equals/hashcode.

public class Visits {
    private long visitCode;
    private String resourceType;
    private String resourceDescription;
    private Code code;
    ...
}
class Code {
    private String agendaCode;
    private String scheduledTime;
    private String visitTypeCode;
    ...
    @Override
    public boolean equals(Object o) {...}
    @Override
    public int hashCode() {...}
}

Then you can use groupingBy like:

Map<Code, List<Slots>> map = slotsResponse.getContent().stream()
            .collect(Collectors.groupingBy(s -> s.getVisit().getCode()));

Also you can just implement equals method inside Visits only for agendaCode, visitTypeCode and scheduledTime. In this case use groupingBy by s.getVisit()

How to filter a list in Java, We are going to filter a list having these beans. The toString() method This is going to be helpful when we print the filtered list of elements. I have two lists of objects Person and PersonResult. Both are linked through the property PersonId. I need to create a filter for the list of PersonResult that meet certain criteria for the Person (e.g. Person.Gender == "female"). Im currently using the following LINQ query to achieve this:


I love Ruslan's idea of using Collectors::groupingBy. Nevertheless, I don't like creating a new class or defining a new equals method. Both of them coerces you to a single Collectors::groupingBy version. What if you want to group by other fields in other methods?

Here is a piece of code that should let you overcome this problem:

slotsResponse.getContent()
    .stream()
    .collect(Collectors.groupingBy(s -> Arrays.asList(s.getVisit().getAgendaCode(), s.getVisit().getVisitTypeCode(), s.getVisit().getScheduledTime())))
    .values();

My idea was to create a new container for every needed field (agendaCode, visitTypeCode, scheludedTime) and compare slots on these newly created containers. I would have liked doing so with a simple Object array, but it doesn't work - arrays should be compared with Arrays.equals which is not the comparison method used by Collectors::groupingBy.

Please note that you should store somewhere or use a method to define which fields you want to group by.

TypeScript - Array filter(), TypeScript - Array filter() - filter() method creates a new array with all elements that pass the test implemented by the thisObject − Object to use as this when executing callback. On compiling, it will generate the same code in JavaScript. A new copy of the list is created. CollectionUtils.filter(result, o -> ((Person) o).getAge() < 30); The CollectionUtils.filter() filters the collection by applying a predicate to each element. If the predicate returns false, the element is removed. Filtering a list with Google Guava. In the following example, we filter a list using Google Guava.


The fields you want to group by are all strings. You can define a function which concatenate those fields values and use that as key for your groups. Example

Function<Slots,String> myFunc = s -> s.getVisit().agendaCode + s.getVisit().visitTypeCode + s.getVisit().scheduledTime;
                               // or s.getVisit().agendaCode +"-"+ s..getVisit().visitTypeCode +"-"+ s.getVisit().scheduledTime;

And then group as below:

Map<String,List<Slots>> result = slotsResponse.getContent().stream()
                               .collect(Collectors.groupingBy(myFunc));

Filtering Kotlin Collections, A quick and practical reference for filtering Kotlin collections. To read more about lambdas, have a look at our Kotlin Lambda article here. If we try to drop more elements than the collection contains, we'll just be left with an empty List. will just return a List that is the same size as the original collection. Using List<T>.FindAll: List<Car> matchingCars = carList.FindAll(car => car.id == 1); I would prefer the LINQ approach personally - note that that is lazy, whereas FindAll immediately looks through the whole list and builds a new list with the results.


array_filter - Manual, array_filter — Filters elements of an array using a callback function json_encode() will convert your result array into an object instead of an array. If you like me have some trouble understanding example #1 due to the You can use array_filter from within a class to access a protected method from that same class: Also attribute members have multiple presentations, including keys and texts, therefore you have the same fields available. But and attribute member can’t reference another attribute member. Filter Behavior. Let’s now see how our member list reacts on active filters. If I modify my script like this, what happens? Will getMembers return only


Array .filter() Method ― Scotch.io, The filter() method returns an array containing elements of the parent array that match the set test Filtering a list of objects using their similar properties. The . filter() method can be used to filter an array of objects which share a similar property� Python Filter() Function. The filter() function accepts only two parameters. The first argument is the name of a user-defined function, and second is iterable like a list, string, set, tuple, etc.


Array, Arrays are list-like objects whose prototype has methods to perform Array elements are object properties in the same way that toString is a elements of the calling array for which the provided filtering function returns true . I have a Crate object, which has a List of KeyValuePairs. Currently, I'm iterating through each pair to see if the kvp.Value.PixelsWide are the same for all items in the List. If they are, return true, else false. The existing method that I have is shown below: