java StreamAPI不使用stream.sorted()对列表进行排序

wydwbb8l  于 2023-04-10  发布在  Java
关注(0)|答案(1)|浏览(198)

我需要对序列进行排序。我不能使用sorted()。这样的任务

List<Integer> integerList = new ArrayList<>(List.of(55,2,3,4,5,1,2));
integerList.stream().
42fyovps

42fyovps1#

public class StringName { public static String stringName(String stringName){

List<Integer> integerList = new ArrayList();
    integerList.add(55);
    integerList.add(2);
    integerList.add(3);
    integerList.add(4);
    integerList.add(5);
    integerList.add(1);
    integerList.add(2);

    //a bubble sort in stream
    integerList.stream().forEach(a -> {
        integerList.forEach(b -> {
            Integer indexB = integerList.indexOf(b);
            if (indexB == integerList.size() - 1) {
                return;
            }
            if (b > integerList.get(indexB + 1)) {
                Integer temp = integerList.get(indexB);
                temp = b;
                integerList.set(indexB, integerList.get(indexB + 1));
                integerList.set(indexB + 1, temp);
            }
        });

    });

    for (Integer integerPri : integerList) {
        System.out.println(integerPri);
    }
}

}

相关问题