简体   繁体   中英

How to create a list of matching objects from multiple lists and delete objects in the matching list such that it reflects in the original list

I'm sorry if this has been asked but I couldn't find something similar from google search so here goes. Say I have two objects

Notebook

public class NoteBook {
    private String name;
    private String description;

    public NoteBook(String name, String description) {
        this.name = name;
        this.description = description;
    }
}

and notes

public class Note {
    private String sourceNoteBook
    private String name;
    private String category;
    private String details;

    public Note(String sourceNoteBook,String name, String category, String details) {
        this.sourceNoteBook = sourceNoteBook;
        this.name = name;
        this.category = category;
        this.details = details;
    }
}

In the program the user can create a number of NoteBook objects and each NoteBook stores a variable number of Notes. Ultimately I would like to add a search function that can search for notes by category or name and return a list of the found notes.

Normally I would use 2 For loops to iterate through the list of notebooks and then iterate through the list of notes for each notebook and compare strings. something like this:

    For (NoteBook noteBook: noteBooks) {
        For(Note note :noteBooks.getNoteList){
            if (note.getCategory().contains(someString)) {
                matchingNotes.add(notes);
            }
        }
    }

However I now want to be able to delete notes from the matchingNotes list such that the note in the original notebook is also deleted.

Whats the best way of storing and searching these two class such that I can implement such a function.

EDIT:

Just for clarification, the end result is that I would like the user to be able to search for a category of notes across all the notebooks,the program will then return a list of notes matching that category. He/she can then delete a note from that list such that it is also deleted in the original notebook. Eg completely removed from the program.

Iterator:
Probably the simplest solution. And since java is using Iterators under the hood in foreach loops, the performance would be the same.

For (NoteBook noteBook: noteBooks) {
  Iterator<Note> it = noteBooks.getNoteList().iterator();
  while (it.hasNext()) {
    Note note = it.next();
    if (note.getCategory().equals(someString)) {
      it.remove();
    }
  }
}

SQL:
This would be optimal. However even using something lightweight, such as H2, or SQLite would require refactoring. And also is not an option in very lightweight applications.

Efficient:
If you only search by category, or name, you could use 2 maps:

Map<String, Note> notesByCategory;
Map<String, Note> notesBytName

This would require O(n) memory to store the maps, but would have very efficient lookups in O(1) time (compared to the current O(n) ). I would avoid this solution , because it is very easy to achieve incosistent state between content of notes and the maps.

EDIT:

var newNoteNames = newList.stream().map(Note::getName).collect(Collectors.toSet());
var oldNoteNames = noteBooks.stream().flatMap(Notebook::getNodeList).map(Note::getName).collect(Collectors.toSet());

var removedNames = oldNoteNames.removeAll(newNoteNames);

for (var removedName : removedNames) {
  for (NoteBook noteBook: noteBooks) {
    Iterator<Note> it = noteBooks.getNoteList().iterator();
    while (it.hasNext()) {
      Note note = it.next();
      if (note.getName().contains(removedName)) {
        it.remove();
      }
    }
  }
}

Why don't you just store the notebook information in note class?

public class Note {
    private NoteBook sourceNoteBook;
    private String name;
    private String category;
    private String details;

    public Note(NoteBook sourceNoteBook,String name, String category, String details) {
        this.sourceNoteBook = sourceNoteBook;
        this.name = name;
        this.category = category;
        this.details = details;
    }
}

Every data manipulation to note will always affect the notebook which store it

edit my answer

you can create a map when the key is the note and the value is the notebook keySet will returned to the user and once he selected the key you have the value to know from which notebook to delete and you have the key that you can delete the note. is that what you meant?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM