-1

I have a question regarding linked-lists. How would you search through one which looks like this and find the string "Freda"?

private LinkedList<Boat> boats = new LinkedList<Boat>();

boats.add(new Boat(1, "Ed", 3));

boats.add(new Boat(2, "Fred", 7));

boats.add(new Boat(3, "Freda", 5));
Zorian
  • 175
  • 1
  • 13
  • Iterate through the list, and for each boat compare the name. – Seelenvirtuose Mar 18 '15 at 08:27
  • 1
    Better use [Map](http://docs.oracle.com/javase/7/docs/api/java/util/Map.html) if sole purpose is to search !!!! – Neeraj Jain Mar 18 '15 at 08:27
  • Possible duplicate [http://stackoverflow.com/questions/5187888/java-searching-within-a-list-of-objects](http://stackoverflow.com/questions/5187888/java-searching-within-a-list-of-objects) – Flown Mar 18 '15 at 08:31

6 Answers6

0

I assume, getName is the getter that will return the name of the boat.

for(int i=0;i<boats.size();i++){
  if(searchedWord.equals(boats.get(i).getName()){
   //do your operation here
  }
}
Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
0

Itrate over your list and then search your value

for(Boat a: boats){
    if(a.name.equals("Freda")){ // name is the variable you have used to store "Fread" 
        //   sout("Found");
         break;
     }
}
AJ.
  • 4,526
  • 5
  • 29
  • 41
  • Isn't **Map** a preferrable approach for this kind of searching !! PS: [This clarifies what I am trying to say](http://stackoverflow.com/a/3770893/3143670) – Neeraj Jain Mar 18 '15 at 08:35
0

Supposing that you have getters and setters on your Boat:

  public Boat search (LinkedList<Boat> list, String searchedName) {
      for (Boat boat : list) {
         if(boat.getName().equals(searchedName) {
               return boat;
                         }
                 }
     throw new IllegalArgumentExcetion("This boat was not found");
     }
drgPP
  • 926
  • 2
  • 7
  • 22
0

As already Added In comments your should opt for Map<Key,Value> Pair Approach

private Map<String,Boat> boats = new HashMap<String,Boat>();
boats.put("ED",new Boat(1, "Ed", 3));
boats.put("Fred",new Boat(2, "Fred", 5));
boats.put("Ted",new Boat(3, "Ted", 8));

Then when you want to search you can always do this way :

boats.get("Fred");
Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62
0

If you want to search for stuff, you want to use an indexed collection like Data Store: https://github.com/jparams/data-store

Example:

Store<Boat> store = new MemoryStore<>() ;
store.add(new Boat(1, "Ed", 3));
store.add(new Boat(2, "Fred", 7));
store.add(new Boat(3, "Freda", 5));
store.index("name", Boat::getName);
Boat boat = store.getFirst("name", "Ed");

With data store you can create case-insensitive indexes and all sorts of cool stuff. If you are doing a lot of lookups, you definitely want to use a library like this over looping.

0

You can solve it easily via filtering the list stream;

final List<Boat> ed = boats.stream().filter(boat -> boat.name.equals("Ed")).collect(Collectors.toList());

This will return a list of all the boats with name Ed

fmatar
  • 3,490
  • 1
  • 15
  • 11