【问题标题】:sort listview by existence in another arraylist按存在于另一个数组列表中对列表视图进行排序
【发布时间】:2018-04-02 22:19:13
【问题描述】:

在我正在创建的一个 android 程序中,每个用户都会有一个 arraylist 首选项,我使用的代码只会使问题变得不必要地复杂,但是假设我有一个我在列表视图中显示的宠物列表。每个页面都会有完整的宠物列表,比如说

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

用户希望查看所有宠物信息,但希望首先查看他们拥有的宠物种类,所以还有另一个数组列表,我拥有的宠物,上面写着ArrayList&lt;Pet&gt; myPets {"cats", "mice"}。如何对所有宠物的列表进行排序,使其首先显示猫和老鼠,然后显示其余的?

我打算用

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 对象包含名称字符串和两个整数,因此变量必须是 .getName()。提前致谢!

【问题讨论】:

  • 你能给 Lists 输入的期望值吗?

标签: java android sorting listview arraylist


【解决方案1】:

首先从第二个列表中删除第一个列表的所有元素,然后创建一个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"}

编辑

您可以按照以下步骤解决您的问题:

  • 您的类 Object 中的用户 hashCode()equals(..) 方法如下:

...

@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

Check the demo code

【讨论】:

  • 我之前应该提到过这一点,但存在一个问题,即 secondList 包含我需要检索的整个宠物对象,而 firstList 要么只有与宠物名称相对应的字符串,要么只有 Pet 对象一个名字。有没有办法更改您的代码,以便我仍然从 secondList 获取对象但只是更改顺序?
  • @PaxanaNonGrata 如果我能理解的话,有一个所有宠物的列表,以及一个应该在列表开头的宠物名称列表,第二个列表是字符串列表,你能确认?
  • 是的,没错,但是应该在开头的宠物列表只存在于默认构造函数中,因此其中的 Dog 对象实际上是 Pet("Dog", 0, 0) ,但是较大列表中的狗对象包含更多有价值的信息,例如 Pet("Dog", 16, 9) 并且该狗对象是我需要显示的对象,我实际上不能互换使用它们
  • @PaxanaNonGrata 检查我的编辑希望这是你需要的:)
【解决方案2】:

请点击链接。希望您能在示例中找到您的解决方案。

https://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/

主要是你必须在你的Pet类中添加 compareTo 方法和一些小操作。很抱歉给你提供了要点。

【讨论】:

    【解决方案3】:

    下面是测试代码,希望对你有帮助:

            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() ) );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-06
      • 2021-01-23
      • 1970-01-01
      • 2020-03-08
      • 2014-05-28
      • 2012-06-01
      • 2022-07-26
      相关资源
      最近更新 更多