Kadangkala, data kami adalah hierarki Contohnya, hubungan tiga peringkat biasa antara wilayah dan majlis perbandaran ialah satu lapisan di dalam yang lain, seperti yang ditunjukkan di bawah:
@Data @Data public class Place { @TreeKey private String id; @TreeParentKey private String parentId; private String name; @TreeChildren private List<Place> children; public Place(String id, String name, String parentId) { this.id = id; this.name = name; this.parentId = parentId; } }
Kesan akhir:
Kod alat
rreee
@TreeParentKeypublic class Test {
public static void main(String[] args) {
List<Place> places = new ArrayList<>();
places.add(new Place("510000", "四川省", "0"));
places.add(new Place("510100", "成都市", "510000"));
places.add(new Place("510107", "武侯区", "510100"));
places.add(new Place("510116", "双流区", "510100"));
places.add(new Place("511600", "广安市", "510000"));
places.add(new Place("511603", "前锋区", "511600"));
places.add(new Place("511621", "岳池县", "511600"));
List<Place> treeList = TreeUtils.getTree(places, "0");
System.out.println(JSON.toJSONString(treeList));
}
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TreeKey {
}
Atas ialah kandungan terperinci Cara menggunakan rekursi untuk melaksanakan kelas alat struktur pokok di Jawa. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TreeParentKey {
}