List < Bean > stores a collection. How to recursively find out all the Bean to form a new collection according to a certain attribute value of Bean

Bean b1=new Bean ("1111", "test1", "data 1");
Bean b2=new Bean ("test1", "test2", "data 2");
Bean b3=new Bean ("test2", "test3", "data 3");
Bean b4=new Bean ("test5", "test6", "data 4");
Bean b5=new Bean ("test6", "test7", "data 5");
Bean b6=new Bean ("test3", "test4", "data 6");
Bean b7=new Bean ("test4", "test5", "data 7");

the second attribute of the previous bean is the first attribute value of the latter Bean
how to compose this List set into a new set according to b1 b2 b3 b6 b7 b4 b5-sharp-sharp-sharp problem description

Dec.03,2021

if there are no duplicate attribute values, you can use the second attribute value of each bean as key into a HashMap , and then read it all the way from the second attribute value of b1 from Map .


TreeMap itself is an ordered data structure, which can be sorted in ascending order by default and can be re-saved once. However, whether the storage of a data structure such as List < Bean > is reasonable needs to be considered by yourself.

package github.banana.demo;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class BeanSort {

    public static void main(String[] args) {
        List<Bean> beans = new ArrayList<>();
        beans.add(new Bean("1111", "test1", "1"));
        beans.add(new Bean("test1", "test2", "2"));
        beans.add(new Bean("test2", "test3", "3"));
        beans.add(new Bean("test5", "test6", "4"));
        beans.add(new Bean("test6", "test7", "5"));
        beans.add(new Bean("test3", "test4", "6"));
        beans.add(new Bean("test4", "test5", "7"));

        Map<String, Bean> map = new TreeMap<>();
        for (Bean b : beans) {
            map.put(b.getTwo(), b);
        }

        for (Map.Entry<String, Bean> entry : map.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}

@Data
class Bean {
    private String one;
    private String two;
    private String desc;

    Bean(String one, String two, String desc) {
        this.one = one;
        this.two = two;
        this.desc = desc;
    }
}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b34974-2bf3e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b34974-2bf3e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?