0

I have 1 List of key:value pair primaryList in Java, now i want to copy complete list except one key date in primaryListExceptDate. Can anyone help me on this? I know we can do it using for loop but i want to know is there any other efficient way of doing it?

  • i have tried for loop but that is not efficient way. –  Aug 31 '17 at 08:35
  • no, show us some code. It is a little hard to understand exactly what you are trying to achieve just by reading your unclear text there. – Jack Flamp Aug 31 '17 at 08:36
  • Copy all and then remove one – Nick Aug 31 '17 at 08:37
  • 3
    You can use `Stream` with `filter` and `map` methods – Jack Flamp Aug 31 '17 at 08:37
  • what kind of list are you using ? Have you considered to use other data structure (map for example ?). Without further assumption on the data i don't think your problem can be resolved in less than O(n) time. – gtosto Aug 31 '17 at 08:42
  • Use a `Map` and check this example out https://www.mkyong.com/java8/java-8-filter-a-map-examples/ – Jack Flamp Aug 31 '17 at 08:45
  • how come your list is key - value pair? are you sure it is not a map? could it easily become a map? Another angle: is your list sorted on those dates? `Collections.binarySearch()` for `ArrayList` is efficient, so you could find your date and remove that element. – diginoise Aug 31 '17 at 08:59

3 Answers3

1

So as I understand you, you have a list of Record objects that keep pairs of values as key, value!?

Then you can use Stream api to do what you want. Something like:

List<Record> primaryListExceptDate = primaryList.stream()
   .filter(record -> !record.getKey().equals(unwantedDateInstance))
   .collect(Collectors.toList());

That will give you a new list without the Record with that unwanted date.

UPDATE: You asked for a Vector example.

I made this test which works fine, d2 is removed. Vector implements List so it can be cast. Collectors doesn't have a toVector method since Vector is outdated:

public class Testa {

public static void main(String[] args) {
    Date d1 = new Date(100,1,2);
    Date d2 = new Date(101,2,3);
    Date d3 = new Date(102,3,4);
    Date test = new Date(101,2,3);

    Vector<Record> primaryList = new Vector<>();
    primaryList.add(new Record(d1, new Object()));
    primaryList.add(new Record(d2, new Object()));
    primaryList.add(new Record(d3, new Object()));

    List<Record> primaryListExceptDate = primaryList.stream()
               .filter(record -> !record.getKey().equals(test))
               .collect(Collectors.toList());

    primaryListExceptDate.forEach(r -> System.out.println(r.getKey().toString()));
}

static class Record {
    Date key;
    Object value;

    public Record(Date k, Object v) {
        this.key = k;
        this.value = v;
    }

    public Date getKey() {
        return key;
    }
}
}
Jack Flamp
  • 1,223
  • 1
  • 16
  • 32
0

I don't know if it's more efficient but you could first make a copy of the list and then iterate over it with an iterator and remove the entry with key 'date'.

EDIT: something like this:

List<Record> primaryList = ...;
List<Record> primaryListExceptDate = new ArrayList<>(primaryList );
Iterator<Record> it = primaryListExceptDate.iterator();
while(it.hasNext()) {
    Record record = it.next();
    if (record.getKey().equals("date")) {
        it.remove();
    }
}
Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60
0

Try a foreach construct to copy data into another List with an if to exclude the object that not interest to you. You may think it's not such an efficient way, but most of the methods the API offer have O(n) complexity. I think it's the simplest method. You also could use List proper methods to copy the List and then remove the object, but this could be a bit more onerous if you look at performance.

Anyway, I suggest you to use Map Collection: it comes to rescue of you when using a key:value pair and it's very efficient! List is useless in this case.

Giuseppe
  • 17
  • 1
  • 7
  • suppose my list is List and i want to copy in List then can you give me the exact code? –  Aug 31 '17 at 08:46
  • For example you could use, if "list" is the List and list1 is List: for(Records elem:list){ if(elem is not the elem that you want to remove){ list1.add(elem) } } – Giuseppe Aug 31 '17 at 08:47
  • i didn't understand what r u trying to say here? can you write the code for my case? –  Aug 31 '17 at 08:50
  • In the if you'll have to put your custom condition :-) – Giuseppe Aug 31 '17 at 08:52
  • How can i get key from elem?bcz i have to put condition on my key. –  Aug 31 '17 at 08:58
  • It depends on the way you put key:value pair on your list. How did you do this? – Giuseppe Aug 31 '17 at 09:08