Home > other >  How to search for given word or character in ArrayList and show those who match and sort them
How to search for given word or character in ArrayList and show those who match and sort them

Time:12-17

I'm still new to java. My task was to find all Producers in ArrayList who matches the word or character that the user inserts and return a list of those words sorted by their field.

For example, we have Producers {"Alex","Bob","Amanda","Jack", "Jari"}

User wants to seach for "A"program returns sorted==>"Alex","Amanda" User wants to seach for "Ja"program returns sorted==>"Jack", "Jari" User wants to seach for "Bob"program returns sorted==>"Bob"

this is all code I have for now in my main method:

Producers producer1 = new Producers("Alex");
Producers producer2 = new Producers("Jack");
Producers producer3 = new Producers("Jari");

Adding all producers into ArrayList

Producers.allProducers.add(producer1);
Producers.allProducers.add(producer2);
Producers.allProducers.add(producer3);`

Then we have another Object called Clothing where we had a task to add our producer for each piece of clothing

Clothing wClothing1 = new Clothing("Sneakers", 6.99, ProductsType.SHOES); // name,price,ProductsType as ENUMClothing 
Clothing wClothing2 = new Clothing("AShoe", 1.99, ProductsType.SHOES);
Clothing wClothing3 = new Clothing("Cocktail dress", 15.99, ProductsType.DRESSES);
wClothing1.setProducers(producer1);
wClothing2.setProducers(producer2);
wClothing3.setProducers(producer3);

Adding all clothing into ArrayList

Items.clothingItemList.add(wClothing1);
Items.clothingItemList.add(wClothing2);
Items.clothingItemList.add(wClothing3);`

We need to search, find and sort words only by producers in Items.clothingItemList. So I created a sorting method that can only sort all producers but can't search for them and then sort.

public static void getItemByProducer() {
 Collections.sort(Items.clothingItemList, new SortByProducersComparator());
    System.out.println(Items.clothingItemList.toString());
}

I created also a new class called SortByProducersComparator():

import java.util.Comparator;

public class SortByProducersComparator implements Comparator<Items> {
    @Override
    public int compare(Items o1, Items o2) {
        int c =0;
        c=o1.getProducersName().compareTo(o2.getProducersName());
        return c;
}}

We need to do this task using: Comparator, Comparable. we cant use it to stream.

Please help, I hope my explanation of the task was good enough

CodePudding user response:

As far as I understand you want to search for a word or character in your ArrayList and return only the elements that match.

You can create a method that receives the String as a parameter to search for that word or character as you mentioned.

Then I would iterate the Producers to check each object's name field to see if it contains the string you inputted. And if there is a match then add it to a different (let's call it matchingProducers ArrayList) that will collect all the matches.

Finally, you can sort the new ArrayList using the Comparator as you need and return the Sorted "new" Producers ArrayList.

A simplified version of your solution would look something like this.

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Main {
    public static void main(String[] args) {
        
        ArrayList<Producer> producers = new ArrayList<>();
        producers.add(new Producer("Alex"));
        producers.add(new Producer("Bob"));
        producers.add(new Producer("Amanda"));
        producers.add(new Producer("Jack"));
        producers.add(new Producer("Jari"));

        // Search for producers that match the word or character (string), for example "A"
    
        ArrayList<Producer> results = searchAndSortProducers(producers, "A");
        System.out.println(results); // Output: [Alex, Amanda]
    }

    public static ArrayList<Producer> searchAndSortProducers(ArrayList<Producer> producers, String searchTerm) {

        ArrayList<Producer> matchingProducers = new ArrayList<>();

        // Loop over the producers and if there is a match, add it to the new list
        for (Producer producer : producers) {
            if (producer.getName().contains(searchTerm)) {
                matchingProducers.add(producer);
            }
        }

        // sort the new list
        matchingProducers.sort(new SortByNameComparator());

        // return matchingProducers
        return matchingProducers;
    }
}

class SortByNameComparator implements Comparator<Producer> {
    @Override
    public int compare(Producer p1, Producer p2) {
        return p1.getName().compareTo(p2.getName());
    }
}

class Producer {
    private String name;

    public Producer(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}
  • Related