-1

I am maintaining a sorted ArrayList of objects (by overwriting the add method as shown here) where each object has 2 attributes: a and b. How can I retrieve an object for which a equals 5?

I cannot use a map, because the value which I want to sort the list on must be able to accept duplicates (which is why this answer is not applicable here).

Code:

 class TimeMap {
     List<MyType> list = new ArrayList<KVT>() {
        public boolean add(KVT mt) {
            int index = Collections.binarySearch(this, mt, new SortByTime());
            if (index < 0) index = ~index;
            super.add(index, mt);
            return true;
        }
    };
}
class KVT{//value-timestamp object
    String value;
    int timestamp;
    public VT(String v, int t){
        value=v;
        timestamp=t;
    }
}
class SortByTimestamp implements Comparator<KVT>{
    public int compare(KVT a, KVT b){
        return a.timestamp.compareTo(b.timestamp);
    }
}
rahs
  • 1,759
  • 2
  • 15
  • 31

2 Answers2

0

I have written a small example using java8 streams where you can get the object from the ArrayList by a property of the object.

import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Test> list = Arrays.asList(new Test(1, 2), new Test(5, 6), new Test(3, 4));

        Test test = list.stream().filter(obj -> obj.a == 5).findFirst().orElse(null);
        System.out.println(test.a);
    }
}

class Test {
    int a;
    int b;

    Test(int a, int b) {
        this.a = a;
        this.b = b;
    }
}

Hope this will give you an idea

Sandeepa
  • 3,457
  • 5
  • 25
  • 41
0

Here is an mcve demonstrating retrieval by timestamp as well as some other enhancement:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class TimeMap {

    private List<KVT> list;

    TimeMap() {
        list = new ArrayList<>() {
            @Override
            public boolean add(KVT mt) {
                super.add(mt); //add
                Collections.sort(this, new SortByTimestamp()); //resort after add
                return true;
            }
        };
    }

    boolean add(KVT mt){return list.add(mt);}

    KVT getByTimeStamp(int timestamp){
        for(KVT mt : list){
            if(timestamp == mt.timestamp)
                return mt;
        }
        return null;
    }

    //returns a copy of list
    List<KVT> getListCopy() { return new ArrayList<>(list) ;};

    //test 
    public static void main(String[] args) {
        TimeMap tm = new TimeMap();
        tm.add(new KVT("A", 2));
        tm.add(new KVT("B", -3));
        tm.add(new KVT("C", 1));
        System.out.println(tm.getListCopy());
        System.out.println(tm.getByTimeStamp(1));
    }
}

class KVT{
    String value;
    int timestamp;
    public KVT(String v, int t){
        value=v;
        timestamp=t;
    }

    @Override
    public String toString(){   return value+" ("+timestamp+")";}

    //todo add getters 
}

class SortByTimestamp implements Comparator<KVT>{

    @Override
    public int compare(KVT a, KVT b){
        //compareTo can not be applied to primitives
        return Integer.valueOf(a.timestamp).compareTo(b.timestamp);
    }
}
c0der
  • 18,467
  • 6
  • 33
  • 65