<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Java 程序:按屬性對自定義對象的`ArrayList`進行排序 > 原文: [https://www.programiz.com/java-programming/examples/sort-custom-objects-property](https://www.programiz.com/java-programming/examples/sort-custom-objects-property) #### 在此程序中,您將學習按 Java 中給定屬性對自定義對象的`ArrayList`進行排序。 ## 示例:按屬性對自定義對象的`ArrayList`進行排序 ```java import java.util.*; public class CustomObject { private String customProperty; public CustomObject(String property) { this.customProperty = property; } public String getCustomProperty() { return this.customProperty; } public static void main(String[] args) { ArrayList<Customobject> list = new ArrayList<>(); list.add(new CustomObject("Z")); list.add(new CustomObject("A")); list.add(new CustomObject("B")); list.add(new CustomObject("X")); list.add(new CustomObject("Aa")); list.sort((o1, o2) -> o1.getCustomProperty().compareTo(o2.getCustomProperty())); for (CustomObject obj : list) { System.out.println(obj.getCustomProperty()); } } } ``` 運行該程序時,輸出為: ```java A Aa B X Z ``` 在上面的程序中,我們定義了一個`CustomObject`類,它帶有`String`屬性,即`customProperty`。 我們還添加了一個初始化屬性的構造器,以及一個返回`customProperty`的獲取器函數`getCustomProperty()`。 在`main()`方法中,我們創建了以 5 個對象初始化的自定義對象`list`的`ArrayList`。 為了對具有給定屬性的列表進行排序,我們使用`list`的`sort()`方法。`sort()`方法采用要排序的列表(最終排序的列表也相同)和`comparator`。 在我們的例子中,比較器是一個 lambda * 從`o1`和`o2`列表中選取兩個對象, * 使用`compareTo()`方法比較兩個對象的`customProperty`, * 如果`o1`的屬性大于`o2`的屬性,最后返回正數;如果`o1`的屬性小于`o2`的屬性,則最后返回負數;如果相等,則返回零。 基于此,基于最小的屬性將`list`排序為最大,并存儲回`list`。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看