java使用單向鏈表解決數(shù)據(jù)存儲自定義排序問題
表設(shè)計
CREATE TABLE `test` ( `id` bigint NOT NULL COMMENT '主鍵id', `name` varchar(50) COLLATE NOT NULL COMMENT '名稱', `next_id` bigint DEFAULT NULL COMMENT '指向下一個節(jié)點的主鍵id', ) ;
1. 新增一條記錄
指定位置插入
- 參數(shù)傳遞前一條數(shù)據(jù)的主鍵 id,根據(jù) id 查詢出該條數(shù)據(jù)的 next_id 記為 A
- 設(shè)置新增數(shù)據(jù)的 next_id 為 A 并保存,
- 修改前一條數(shù)據(jù)的 next_id 為新增數(shù)據(jù)的主鍵 id
尾插
- 默認(rèn) next_id 為 -1,表示為新增的數(shù)據(jù),排序在最后,需要先查詢出新增前最后一條數(shù)據(jù),并將其 next_id 修改為新增數(shù)據(jù)的 id
2. 修改排序
- 參數(shù)傳遞
被移動數(shù)據(jù),移動前,前一條數(shù)據(jù)的 id (C)
被移動數(shù)據(jù),移動后,前一條數(shù)據(jù)的 id (A)
被移動數(shù)據(jù)的 id (D)
- 查詢 A 的 next_id (記為 B )
- 查詢 D 的 next_id (記為 E )
- 修改 A 的 next_id 為 D 的主鍵 id
- 修改 D 的 next_id 為 B
- 修改 C 的 next_id 為 E
移動思路如下
3. 刪除
- 參數(shù)傳遞前一條數(shù)據(jù)的id、和要刪除數(shù)據(jù)的id,查詢出刪除數(shù)據(jù)的 next_id 記為 A
- 修改前一條數(shù)據(jù)的 next_id 為 A
- 對要刪除的數(shù)據(jù)執(zhí)行刪除
代碼實現(xiàn)
1. 簡單對象
@Data public class Tag { ? ? private Integer id; ? ? private String name; ? ? private Integer nextId; }
2. 對數(shù)據(jù)按照 nextId 排序
public class Test { ? ? public static void main(String[] args) { ? ? ? ? // 添加測試數(shù)據(jù) ? ? ? ? // 這里生成的鏈表應(yīng)為:10 -> 40 -> 20 -> 30 -> 50 ? ? ? ? List<Tag> tags = addData(); ? ? ? ? // 根據(jù)每項數(shù)據(jù)的nextId建立map ? ? ? ? Map<Integer, Tag> map = tags.stream().collect(Collectors.toMap(Tag::getNextId, t -> t)); ? ? ? ? // -1 默認(rèn)為最后一項 ? ? ? ? Tag lastTag = map.get(-1); ? ? ? ? LinkedList<Tag> tagLinkedList = new LinkedList<>(); ? ? ? ? tagLinkedList.addFirst(lastTag) ? ? ? ? // 使用遞歸從map中提取數(shù)據(jù) ? ? ? ? get(lastTag.getId(), map, tagLinkedList); ? ? ? ? tagLinkedList.forEach(System.out::println); ? ? } ? ? private static void get(int preId, Map<Integer, Tag> map, LinkedList<Tag> tagList) { ? ? ? ? Tag tag = map.get(preId); ? ? ? ? if (tag == null) { ? ? ? ? ? ? return; ? ? ? ? } ? ? ? ? tagList.addFirst(tag); ? ? ? ? get(tag.getId(), map, tagList); ? ? } ? ? private static List<Tag> addData() { ? ? ? ? List<Tag> tagList = new ArrayList<>(); ? ? ? ? Tag tag1 = new Tag(); ? ? ? ? tag1.setId(10); ? ? ? ? tag1.setName("tag1"); ? ? ? ? tag1.setNextId(40); ? ? ? ? tagList.add(tag1); ? ? ? ? Tag tag4 = new Tag(); ? ? ? ? tag4.setId(40); ? ? ? ? tag4.setName("tag4"); ? ? ? ? tag4.setNextId(20); ? ? ? ? tagList.add(tag4); ? ? ? ? Tag tag2 = new Tag(); ? ? ? ? tag2.setId(20); ? ? ? ? tag2.setName("tag2"); ? ? ? ? tag2.setNextId(30); ? ? ? ? tagList.add(tag2); ? ? ? ? Tag tag3 = new Tag(); ? ? ? ? tag3.setId(30); ? ? ? ? tag3.setName("tag3"); ? ? ? ? tag3.setNextId(50); ? ? ? ? tagList.add(tag3); ? ? ? ? Tag tag5 = new Tag(); ? ? ? ? tag5.setId(50); ? ? ? ? tag5.setName("tag5"); ? ? ? ? tag5.setNextId(-1); ? ? ? ? tagList.add(tag5); ? ? ? ? return tagList; ? ? } }
3. 輸出結(jié)果
Tag(id=10, name=tag1, nextId=40)
Tag(id=40, name=tag4, nextId=20)
Tag(id=20, name=tag2, nextId=30)
Tag(id=30, name=tag3, nextId=50)
Tag(id=50, name=tag5, nextId=-1)
到此這篇關(guān)于java使用單向鏈表解決數(shù)據(jù)存儲自定義排序問題的文章就介紹到這了,更多相關(guān)java 數(shù)據(jù)存儲自定義排序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java中List、Set、Map的區(qū)別和實現(xiàn)方式示例代碼
- Java中List Set和Map之間的區(qū)別_動力節(jié)點Java學(xué)院整理
- Java中HashSet和HashMap的區(qū)別_動力節(jié)點Java學(xué)院整理
- Java中的Set、List、Map的用法與區(qū)別介紹
- Java中HashMap和Hashtable及HashSet的區(qū)別
- 淺析Java中Map與HashMap,Hashtable,HashSet的區(qū)別
- java8中NIO緩沖區(qū)(Buffer)的數(shù)據(jù)存儲詳解
- Java數(shù)據(jù)存儲的“雙子星”對決(Map和Set的區(qū)別)
相關(guān)文章
Springboot讀取templates文件html代碼實例
這篇文章主要介紹了Springboot讀取templates文件html代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04SpringMVC注解@RequestParam方法原理解析
這篇文章主要介紹了SpringMVC注解@RequestParam方法原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04