Sort listview by availability in another arraylist

In the android program that I create, each user will have an arraylist preference, the code that I use will just make the question useless, but I will say that I have a list of pets that I show on the list. On each page there will be an entire list of pets, say

ArrayList<Pet> allPetsList = {"dogs", "cats", "parrots", "mice", "hamsters", "guinea pigs"}

The user wants to see all the information about pets, but wants to see the kind of pet that they have, so there is another list of arrays, I have animals that have ArrayList<Pet> myPets {"cats", "mice"}. How can I sort the list of all pets to display cats and mice first and then display the rest?

I was going to use

allPetsList.sort(myPetsList, new Comparator<Item>() {
public int compare(Item left, Item right) {
    if (myPetsList.contains(left)) {
return 1;
}
else {
return 0;}
     }
});`

, , ArrayList.sort , , . ? , , Pet ints, .getName(). !

+4
3

, LinkedList ( ), :

List<Pets> resultList = new LinkedList<>();
List<Pets> firstList  =  {"cats", "mice"};
List<Pets> secondList = {"dogs", "cats", "parrots", "mice", "hamsters", "guinea pigs"};

secondList.removeAll(firstList);//{"dogs", "parrots", "hamsters", "guinea pigs"}

resultList.addAll(firstList);//{"cats", "mice"}
resultList.addAll(secondList);//{"cats", "mice", "dogs", "parrots", "hamsters", "guinea pigs"}

Edit

, :

  • hashCode() equals(..) Object:

...

@Override
public int hashCode() {
    int hash = 3;
    hash = 53 * hash + Objects.hashCode(this.name);
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final Pet other = (Pet) obj;
    if (!Objects.equals(this.name, other.name)) {
        return false;
    }
    return true;
}

...

Java 8:

List<Pet> namesList = new LinkedList<>(
        Arrays.asList(
                new Pet("cats", 0, 0),
                new Pet("mice", 0, 0)
        )
);

List<Pet> petsList = new LinkedList<>(
        Arrays.asList(
                new Pet("dogs", 16, 18),
                new Pet("cats", 36, 99),
                new Pet("parrots", 85, 25),
                new Pet("mice", 70, 28),
                new Pet("hamsters", 12, 41),
                new Pet("guinea pigs", 75, 95)
        )
);

List<Pet> newList = petsList.stream()
        .filter(t -> namesList.contains(t))
        .collect(Collectors.toList());//find the necessary objects

petsList.removeAll(newList);//remove them from the principal list
petsList.addAll(0, newList);//add the result on the top

+2

Here is the tested code below, hope you find this useful:

        List<Pet> listOfAllPets = new ArrayList<Pet>();
        listOfAllPets.add(new Pet("dogs"));
        listOfAllPets.add(new Pet("cats"));
        listOfAllPets.add(new Pet("parrots"));
        listOfAllPets.add(new Pet("mice"));
        listOfAllPets.add(new Pet("hamsters"));

        List<Pet> listOfMyPets = new ArrayList<Pet>();
        listOfMyPets.add(new Pet("mice"));
        listOfMyPets.add(new Pet("cats"));

       //Fill the Map with All Pets
        final HashMap<String, Integer> allPetsMap = new HashMap<String, Integer>();
        for (int i = 0; i < listOfAllPets.size(); i++) {
            allPetsMap.put(listOfAllPets.get(i).Name, i);
        }

        //sort AllPets
        Collections.sort(listOfAllPets);

        //sort MyPets using Map of AllPets
        Collections.sort(listOfMyPets, new Comparator<Pet>() {
            public int compare(Pet obj1, Pet obj2) {
                return allPetsMap.get(obj1.getName()) - allPetsMap.get(obj2.getName());
            }
        });

        //this is just to print Results:[cats, mice, cats, dogs, hamsters, mice, parrots]

        List<Pet> sortedPets = new ArrayList<Pet>(listOfMyPets);

        sortedPets.addAll(listOfAllPets.removeAll(listOfMyPets)? listOfMyPets :listOfAllPets);

        System.out.println(sortedPets.stream().map(i -> i.getName()).collect( toList() ) );
0
source

Source: https://habr.com/ru/post/1687965/


All Articles