0

I have an arraylist that is unsorted order and another arraylist that is the sorted order. I need to add a remove button to remove words from both the Original Order and Sorted Order but to remove with binarySearch I need to sort the original order. But I need to keep it unsorted...

    int songIndex = Collections.binarySearch(song, titleArtistInput.getText());
    int sortedSongIndex = Collections.binarySearch(sortedSong, titleArtistInput.getText());

    //To test the values.
    System.out.println(songIndex + " " + sortedSongIndex);



    if (sortedSongIndex < 0)
    {
        titleArtistOutput.setText("That CD does not exist in the collection, please try again");
    }
    else if (sortedSongIndex >= 0)
    {
        sortedSong.remove(sortedSongIndex);
        Collections.sort(song);
        song.remove(Collections.binarySearch(song, titleArtistInput.getText()));
    }

Is there a method that reverts the Collections.sort? or any way to do this without sorting the song ArrayList?

EDIT: I got it to work myself! Finally.

int sortedSongIndex = Collections.binarySearch(sortedSong, titleArtistInput.getText());

    //if the Collections.binarySearch is negative (song not found), it will output 
    //"That CD does not exist in the collection, please try again", if the sortedSongIndex is positive
    //(the song had been found!) and will remove the indexOf titleArtistInput.getText() from the ArrayLists
    if (sortedSongIndex < 0)
    {
        titleArtistOutput.setText("That CD does not exist in the collection, please try again");
    }
    else if (sortedSongIndex >= 0)
    {
        sortedSong.remove(sortedSong.indexOf(titleArtistInput.getText()));
        song.remove(song.indexOf(titleArtistInput.getText()));

    }

3 Answers3

4

Use a Map<String, Integer> songToIndexMap to store the index of each song.

Then just do:

Integer index = songToIndexMap.remove(titleArtistInput.getText());
if(index != null) {    // the song has been found!
    song.remove(index);
}

Binary search is O(log n) while remove/getin a HashMap is O(1).

Fildor
  • 14,510
  • 4
  • 35
  • 67
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
0

I think, creating list of pairs is good idea. One pair keeps position and other one keeps value. look this question. You still can sort list according to your value.

Community
  • 1
  • 1
Erkan Akın
  • 106
  • 5
0

If you want to preserve the original order and have a fast time of search you can combine a Map and an ArrayList.

For each item add it to the ArrayList and then add it with the index of the list into the map, using as key the field you want to have as order.

Example:

    ArrayList<Song> originalSort = new ArrayList<>();
    Map<String, Song> theMap = new HashMap<>(); //we will sort by name of song

    //add some items
    Song song = new Song();
    song.author = "thatMan";
    song.name = "someTitle";
    //the index will be the index within the ArrayList
    song.index = originalSort.size();

    originalSort.add(song);
    theMap.put(song.name, song);

    //...

    //iterate with the original order:
    for (Song song : originalSort) {
        System.out.print(song.name);
    }

    //fast search and remove
    song = theMap.remove(song.name);
    originalSort.remove(song.index); //ArrayList has a fast acces by index

If it's for testing purposes you can store the original order simply by making a copy of the list before sorting it:

List listBackup = originalList.clone(); //remember to use clone method or you will be pointing to the same intance
Brierson
  • 96
  • 4