java - 我可以对 hashmap (T) 中包含的数组列表进行排序吗?

标签 java arrays sorting hashmap

我有一个 HashMap ,它包含键 - 字符串类型 - 奶酪对象数组:

public void testing(){
    HashMap<String, ArrayList<cheese>> one = new HashMap<>();
    ArrayList<cheese> two = new ArrayList<>();

    cheese seven = new cheese("59");
    cheese eight = new cheese("60");
    cheese nine = new cheese("12");
    cheese ten = new cheese("15");

    two.add(seven);
    two.add(eight);
    two.add(nine);
    two.add(ten);

    one.put("hello", two);

}

这是我的奶酪课:

public class cheese {

    String num;

    public cheese(String num){
        this.num = num;
    }


    public String getString(){
        return num;
    }
}

我想获取ArrayList并对ArrayList中的每个元素进行排序,然后将对象放回 HashMap 中。

这是java。我知道它听起来很疯狂但也很有趣!

我考虑过使用比较器和 TreeMap,但它们似乎不适合这个问题。 任何指示都会有帮助。

最佳答案

您可能想要做的是扩展您的 Cheese类来实现Comparable<Cheese>界面。

但这假设您的 Cheese实例可以构成全序。如果您不知道什么是总订单,我建议您首先了解有关订购的更多信息。

您的同类类(class)应具备的基本特征:

  • 自反性
  • 反对称
  • 传递性

您可以通过以下链接了解更多相关信息: http://algs4.cs.princeton.edu/21elementary/

基本上当你的Cheese类实现 Comparable接口(interface)它继承了方法public int compareTo(T o); 。您必须实现此方法并根据 Cheese 的状态返回负整数或 0 或正整数。根据比较的实例Cheese实例在总排序中更小、等于或更大。

如果 Cheese 中只有一个实例字段就像你的例子一样,那就是 num字段您可以按照以下方式编写一些内容:

public class Cheese implements Comparable<Cheese> {

    String num;

    public Cheese(String num){
        this.num = num;
    }

    public String getString(){
        return num;
    }

    public int compareTo(Cheese that) {
        return this.num.compareTo(that.num);
    }
}

请注意,这是一个有点退化(或幸运?)的情况,因为您的 Cheese 的状态类仅取决于单个 String 。因此,在这种情况下,通过比较 num 可以轻松确定哪个更小、等于或更大。 field 。另一方面,如果您稍后使用其他字段扩展您的类,那么您应该准备修改您的 compareTo方法也考虑这些字段并使类遵循总排序的特征。

一个好的做法是让你的类(class)处于控制之下,为所有 3 个功能(自反性、反对称性、传递性)编写单元测试,当你添加新字段和 compareTo 时,这些功能将会失败。方法违反了总排序。

此外,如果您有一个 String比我还考虑使用字符串来表示奶酪的字段。

排序可能对你不起作用,因为你的类没有实现 Comparable之前。

作为 Comparable 的替代方案,您可能还想实现 Comparator界面,这样你就可以将不同的奶酪排序策略与实际的Cheese分离。类。

<小时/>

编辑:展示如何实现你的目标

所以你这么说

I want to get the ArrayList and sort each element inside the ArrayList, then put the objects back into the hash map

然后你可以这样做(使用我上面的 Cheese 类):

public void testing(){
        HashMap<String, List<Cheese>> one = new HashMap<>();
        List<Cheese> two = new ArrayList<>();

        Cheese seven = new Cheese("59");
        Cheese eight = new Cheese("60");
        Cheese nine = new Cheese("12");
        Cheese ten = new Cheese("15");

        two.add(seven);
        two.add(eight);
        two.add(nine);
        two.add(ten);

        System.out.println(two);
        assert "59".equals(two.get(0).num);
        assert "60".equals(two.get(1).num);
        assert "12".equals(two.get(2).num);
        assert "15".equals(two.get(3).num);

        Collections.sort(two);

        System.out.println(two);
        assert "12".equals(two.get(0).num);
        assert "15".equals(two.get(1).num);
        assert "59".equals(two.get(2).num);
        assert "60".equals(two.get(3).num);
        one.put("hello", two);
}

关于java - 我可以对 hashmap (T) 中包含的数组列表进行排序吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36074396/

相关文章:

java - 如何设置 jComponent 在手动调整大小时自动获取父级大小?

java - AAR 不包括模块的资源

使用文件系统层次结构创建树的 Java 方法

java - 无需下载即可获取文件的时间戳?

c - 如何在C中对结构体指针数组进行快速排序?

arrays - 将自定义类型的数组从node-pg和SQL注入(inject)传递给postgres函数

PHP append 一个带有额外Mysql查询的数组

bash - 我该如何加快速度?

list - 在Flutter/Dart中对具有相同属性(时间戳)的不同对象的列表进行排序

javascript - 如何过滤数据并将其显示在数组顶部?