カスタムオブジェクトのArrayListをプロパティでソートするJavaプログラム
この記事では、カスタムオブジェクトのarrayListをプロパティで並べ替える方法を理解します。 ArrayListクラスはAbstractListを拡張し、Listインターフェイスを実装します。 ArrayListは、必要に応じて拡張できる動的配列をサポートしています。
配列リストは初期サイズで作成されます。このサイズを超えると、コレクションが自動的に拡大されます。オブジェクトが削除されると、アレイが縮小する可能性があります。
以下は同じのデモンストレーションです-
入力がであると仮定します −
The list is defined as Java Scala Python Mysql
必要な出力は −
The list after sorting values: Java Mysql Python Scala
アルゴリズム
Step 1 - START Step 2 - Declare namely Step 3 - Define the values. Step 4 - Use the ‘sort’ method to sort the list. Step 5 - Use the ‘compareTo’ method to compare properties of the list. Step 6 - Use the ‘add’ method to add new values to the list. Step 7 - In the main method, create an array list, and invoke the ‘sort’ method. Step 8 - Display the result Step 9 - Stop
例1
ここでは、「main」関数の下ですべての操作をバインドします。
import java.util.*;
class CustomObject {
private String custom_property;
public CustomObject(String property){
this.custom_property = property;
}
public String get_custom_property(){
return this.custom_property;
}
}
public class Demo {
public static void print(ArrayList<CustomObject> input_list){
for (CustomObject object : input_list) {
System.out.println(object.get_custom_property());
}
}
public static void sort(ArrayList<CustomObject> input_list){
input_list.sort((object_1, object_2)
-> object_1.get_custom_property().compareTo(
object_2.get_custom_property()));
}
public static void add(ArrayList<CustomObject> input_list){
input_list.add(new CustomObject("Java"));
input_list.add(new CustomObject("Scala"));
input_list.add(new CustomObject("Python"));
input_list.add(new CustomObject("Mysql"));
}
public static void main(String[] args){
System.out.println("Required packages have been imported");
ArrayList<CustomObject> input_list = new ArrayList<>();
add(input_list);
System.out.println("The list is defined as ");
print(input_list);
sort(input_list);
System.out.println("\nThe list after sorting values: ");
print(input_list);
}
} 出力
Required packages have been imported The list is defined as Java Scala Python Mysql The list after sorting values: Java Mysql Python Scala
例2
ここでは、操作をオブジェクト指向プログラミングを示す関数にカプセル化します。
import java.util.*;
class CustomObject {
private String custom_property;
public CustomObject(String property){
this.custom_property = property;
}
public String get_custom_property(){
return this.custom_property;
}
}
public class Demo {
public static void main(String[] args){
System.out.println("Required packages have been imported");
ArrayList<CustomObject> input_list = new ArrayList<>();
input_list.add(new CustomObject("Java"));
input_list.add(new CustomObject("Scala"));
input_list.add(new CustomObject("Python"));
input_list.add(new CustomObject("Mysql"));
System.out.println("The number is defined as ");
for (CustomObject object : input_list) {
System.out.println(object.get_custom_property());
}
input_list.sort((object_1, object_2)
-> object_1.get_custom_property().compareTo(
object_2.get_custom_property()));
System.out.println("\nThe list after sorting values: ");
for (CustomObject object : input_list) {
System.out.println(object.get_custom_property());
}
}
} 出力
Required packages have been imported The number is defined as Java Scala Python Mysql The list after sorting values: Java Mysql Python Scala
-
ソートをカウントするためのJavaプログラム
カウントソートは、個別のキー値を持つオブジェクトの数をカウントします。例を見てみましょう- 注 −以下のコードは、負の数でも使用できます。 例 import java.util.*; public class Demo{ static void count_sort(int[] arr){ int max_val = Arrays.stream(arr).max().getAsInt(); int min_val = Arrays.stream(arr).min().getAsInt
-
カクテルソート用のJavaプログラム
カクテルソートは、要素が左から右に繰り返され、最大の要素が最初に正しい位置に移動されるバブルソートとは対照的に機能します。シェーカーソートでは、要素が交互に両方向(左と右)に繰り返されます。 以下は、カクテルソートのプログラムです- 例 public class Demo{ static int temp; static void Cocktail(int a[], int n){ boolean swap = true; int begin = 0,i;