-3

I need to find objects from ArrayList<Object> with similar object.trackingId.

For example:

ArrayList<Ilist> ilistArray = new ArrayList<>();
ilistArray.addAll(...); // array is filled with Ilist objects
class Ilist {
    String name;   
    String trackingId;
    String place;
    // Et cetera
}

I need to find the those object from ilistArray which has similar trackingId.

Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
Ashwinee
  • 15
  • 6
  • You mean you want to find a list of duplicates or just get some object from the list based on the value? – Amongalen Jul 07 '20 at 07:22
  • yes I want to get the objects in list where tracking id is same. – Ashwinee Jul 07 '20 at 07:25
  • Does this answer your question? [Identify duplicates in a List](https://stackoverflow.com/questions/7414667/identify-duplicates-in-a-list) – Amongalen Jul 07 '20 at 07:26
  • Use a `set` to find the duplicate objects. – Amit kumar Jul 07 '20 at 07:30
  • no I don't want to find duplicate objects.. I want to find the object which has same tracking id i.e one of the object parameter and other parameter value may differ. – Ashwinee Jul 07 '20 at 07:34
  • It's currently not clear what you are asking. You are searching for objects where `trackingId` is the same – the same as what? Do you want to *filter out* duplicates? Or do you want to find objects with tracking id *matching a certain value*? – MC Emperor Jul 07 '20 at 07:34
  • Explain your problem clearly – soorapadman Jul 07 '20 at 07:34
  • @MCEmperor look like you are correct – soorapadman Jul 07 '20 at 07:35
  • @Ashwinee do you want to get list of Ilist which has the object.trackingId is the given value. is that? – janith1024 Jul 07 '20 at 07:41
  • Guys... explaining more : I have a arraylist holding Ilist object. I need to find those objects which has similar trackingId... Hope you got me. – Ashwinee Jul 07 '20 at 07:43

5 Answers5

0

ilistArray.stream().filter(ilist-ilist.tracking_id.equals(the-given-tracking-id)).collect(Collectors.toList()) - does this answer your question?

Demo:

package com.stackoverflow;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Prob10 {

    public static void main(String[] args) {
        ArrayList<Ilist> ilistArray = new ArrayList<Ilist>();
        ilistArray.add(new Ilist("Amit", "tid1", "Kolkata"));
        ilistArray.add(new Ilist("Ajay", "tid1", "Durgapur"));
        ilistArray.add(new Ilist("Bishnu", "tid2", "Bangalore"));
        ilistArray.add(new Ilist("Vineet", "tid3", "Noida"));

        List<Ilist> resultListArray = new ArrayList<Ilist>();

        resultListArray = ilistArray.stream().filter(ilist -> ilist.tracking_id.equals("tid1"))
                .collect(Collectors.toList());
        System.out.println(resultListArray);
    }
}

class Ilist {
    String name;
    String tracking_id;
    String place;

    Ilist(String n, String id, String pl) {
        this.name = n;
        this.tracking_id = id;
        this.place = pl;
    }
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Guys... explaining more : I have a arraylist holding Ilist object. I need to find those objects which has similar trackingId... Hope you got me. – Ashwinee Jul 07 '20 at 07:40
  • Ok, did you try as I explained above....You may pass your required tracking id replacing "tid_2001" as in the snippet...and it should give you the result. – Aniruddha Sardar Jul 07 '20 at 07:45
  • Aniruddha Sardar :I only have ilistArray and need to collect those object in another ilistArray where trackingId is similar. – Ashwinee Jul 07 '20 at 07:48
  • Probably I'm not able to understand the issue - please check the updated answer if that gives you more clarification. – Aniruddha Sardar Jul 07 '20 at 07:57
0

Given ilistArray this method returns a map of all the Ilist objects with the same trakingId:

public Map<String,List<Ilist>> mapTrakingId(List<Ilist> ilistArray){
    Map<String,List<Ilist>> result = new HashMap<>();
    for(Ilist i : ilistArray) {
        if(result.containsKey(i.trakingId)) {
            result.get(i.trakingId).add(i);
        }else {
            List<Ilist> iList = new ArrayList<>();
            iList.add(i);
            result.put(i.trakingId, iList);
        }
    }
    return result;
}

Or using Java Streams:

Map<String,List<Ilist>> trakingIdMap = ilistArray.stream()
                .collect(Collectors.groupingBy(i -> i.trakingId));
Marc
  • 2,738
  • 1
  • 17
  • 21
0

If you want to return the first element with the matching value, you need to write a method with a list as first parameter and a value-to-find as second parameter, that:

  • traverses the list
  • for each element, checks (with an if statement) whether the value is equal to your method parameter
  • if it matches, return it immediately.
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
0

you can map to do this. trackingId as key.

public static Map<String,List<Ilist>> groupById(List<Ilist> iListArray){
        //same trackingId store to same List
        Map<String, List<Ilist>> trackIdMap=new HashMap<>();
        for(Ilist ilist:iListArray){
            trackIdMap.computeIfAbsent(ilist.trackingId, trackId -> new ArrayList<>()).add(ilist);
        }
        return trackIdMap;
}
0
for (Ilist il : ilistArray) {
        List<Ilist> sameTrackingIdList = ilistArray.stream()
                .filter(d -> d.getTrackingID().equals(il.getTrackingID())).collect(Collectors.toList());

        ilistMap.put(il.getTrackingID(), sameTrackingIdList);

    }
This code snippet resolved my issue. Thank you all. :)
Ashwinee
  • 15
  • 6