0

I have an object (Object 1 for reference) which contains an arraylist of another object (Object 2 for reference).

I want to include a method in Object1, which will take a search term as a parameter and will then iterate through the arraylist of objects2. Through every iteration, it will need to compare the search term against every variable the object has (It has 5 different variable). For example :

The user inputs a search term, the system will then iterate through the arraylists of objects. The first object in the arraylist, it will compare the 5 String variables of that object with the search term, and if there is a single match, it will print that object. It will then move on to the second object in the array list and do the same...

I'm not quite sure how to implement this in a nice readable way. The only way I can think of is implementing multiple IF OR OR statements...

Any ideas ? Any help is appreciated, thanks!

5
  • 4
    Please show your best good faith attempt -- let's see how good or bad it is in the flesh. Commented Apr 25, 2016 at 12:26
  • 1
    Are you using Java 8? Commented Apr 25, 2016 at 12:26
  • is this the type of arraylist you have List<List<String>> listOfLists = new ArrayList<List<String>>(); if not atleast mention the structure of your arraylist Commented Apr 25, 2016 at 12:35
  • The arraylist is a ArrayList<Books> in which Books is an object with 5 String variables Commented Apr 25, 2016 at 12:56
  • Give your Books class a public boolean containsText(String key) method that does the checking for you. Then when you iterate through the ArrayList, call this method. Commented Apr 25, 2016 at 13:02

2 Answers 2

2

Here is my solution, assuming you are not comfortable with lambdas (java8)

public class MyObject {


    public String[] list = {"one", "two", "three", "four"};



    public String findMyString(String  searchCriteria){
        String result= null;
        for(int i=0; i<list.length; i++){
            if(list[i].equals(searchCriteria) ) return list[i];    
        }

        return result;
    }




    public static void main(String args[]){

        MyObject object = new MyObject();

        System.out.println(object.findMyString("zero"));
        System.out.println(object.findMyString("one"));
    }

}

The output is as follow:

null
one

There are probably more efficient solutions but you are searching with strings and, unless you have a very big dataset, it should not be that big of a performance issue.

Sign up to request clarification or add additional context in comments.

Comments

1

Say you had a Book class with four Strings:

public class Book {
    private String title;
    private String publisher;
    private String author;
    private String isbn;

and you wanted to check if a key String matched any String in this class, you could give Book this method for example:

public boolean containsText(String key) {
    // your choice whether to use equals or equalsIgnoreCase
    return key.equalsIgnoreCase(title) 
            || key.equalsIgnoreCase(publisher) 
            || key.equalsIgnoreCase(author)
            || key.equalsIgnoreCase(isbn);
}

Then if you had a List<Book> bookList and you wanted to display books with matching text you could easily check it with:

public void displayTextMatch(String key) {
    // iterate through list
    for (Book book : bookList) {
        // call Book's containText method
        if (book.containsText(key)) {
            // assuming Book has a decent `toString()` override:
            System.out.println(book);
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.