「java集合如何使用流」java流的使用

博主:adminadmin 2023-01-09 09:12:10 681

今天给各位分享java集合如何使用流的知识,其中也会对java流的使用进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java8中如何将多个集合的数据拼接成一个统一的流

java8中stream的提供了一个拼接流的方法Stream.concat,可以将两个stream拼接成一个stream, 保持了两个stream中的元素顺序。

那么如果我们需要对多个集合中的元素拼接成一个stream来统一处理,可以怎么做呢?

比如有三个CollectionString c1, c2, c3.

方法一,使用Stream.concat方法来拼接,可以使用一个for循环来处理。

private static StreamString concat1(ListCollectionString collections) {

Stream result = Stream.empty();

for (CollectionString strings : collections) {

              result = Stream.concat(result,  strings.stream());

}

return   result;

}

方法二,使用flatMap方法,将集合变成stream, 再压平

private static StreamString concat2(ListCollectionString collections) {

return  collections.stream()

           .flatMap(Collection::stream);

}

对于不同集合类型的数据,如何做成一个统一的流?还是可以使用flatMap方法来做

方法三:

private static StreamString concat3(ListString s1,String[] s2, SetString s3) {

return  Stream.of(s1.stream(), Arrays.stream(s2), s3.stream())

           .flatMap(Function.identity());

}

方法三和方法二相比,可以使用不同类型的集合类型来拼接流,方法二在拥有共同基类的情况下使用会显得简洁很多。

JAVA 集合类与输入流问题

你能不能把你写的程序贴一下??我大致写了一个将Object输入到文件之后再从文件读出的程序没有出现你的问题。

程序如下:

import java.io.*;

import java.util.ArrayList;

public class Test {

private static File f= new File("/home/kse/test.txt");

public static void main(String[] args){

ArrayListStudent al = new ArrayListStudent();

Student s = new Student("aaaa",20);

al.add(s);

froze(al,f);

ArrayListStudent al2=(ArrayListStudent)refresh(f);

Student s2 = al2.get(0);

System.out.println(s2.age);

System.out.println(s2.name);

}

private static void froze(Object o,File ff){

try{

FileOutputStream fos = new FileOutputStream(ff);

ObjectOutputStream out = new ObjectOutputStream(fos);

out.writeObject(o);

out.flush();

out.close();

}catch (Exception e){

e.printStackTrace();

}

}

private static Object refresh(File ff){

Object o = new Object();

try{

FileInputStream fis = new FileInputStream(ff);

ObjectInputStream in = new ObjectInputStream(fis);

o=in.readObject();

in.close();

}catch(Exception e){

e.printStackTrace();

}

return o;

}

}

class Student implements Serializable{

String name;

int age;

Student(String name,int age){

this.name=name;

this.age=age;

}

}

-----------------------------------------

嗯~你需要重写toString()方法。比如

public String toString(){

return name + " " + age;

}

类似很简单的方法就可以。

java中list集合stream流怎么把数据10个一分组

java中list集合stream流把数据10个一分组步骤如下:

1、首先使用summingDouble和averagingDouble来实现DoublesummingScore=students.stream().collect(Collectors.summingDouble(Student:getScore));DoubleaveragingScore=students.stream().collect(Collectors.averagingDouble(Student::getScore));

2、使用summarizingDouble来实现它更为综合,可以直接计算出相关的汇总信息,DoubleSummaryStatisticssummarizingDouble=students.stream().collect(Collectors.summarizingDouble(Student::getScore))。

java 如何用io流 读取list集合里我需要的内容,然后写入到.txt文件里?各位大侠请赐教

import org.junit.Test;

import java.io.*;

import java.util.ArrayList;

import java.util.List;

public class TestIo {

    //路径

    private static final String PATH = "D:\\demo1.txt";

    @Test

    public void testIo() throws IOException {

        ListString list = new ArrayList();

        list.add("1");

        list.add("2");

        list.add("3");

        list.add("4");

        list.add("5");

        File file = new File(PATH);

        //如果没有文件就创建

        if (!file.isFile()) {

            file.createNewFile();

        }

        //

        BufferedWriter writer = new BufferedWriter(new FileWriter(PATH));

            for (String l:list){

                writer.write(l);

            }

            writer.close();

        System.out.println("完成");

    }

}

结果

希望对你有帮助

Java集合和IO流

刚学的,楼主看一下

public class SongUtils {

    private static final String ROOT_PATH = "D:\\songs";

    public static void selectAllSong() {

        File file = new File(ROOT_PATH);

        File[] files = file.listFiles();

        if (files != null) {

            for (File fi : files) {

                // 只匹配mp3后缀文件

                if (fi.getPath().endsWith(".mp3")) {

                    // 输出除后缀的文件名

                    System.out.println(fi.getName().substring(0, fi.getName().lastIndexOf(".")));

                }

            }

        }

    }

    public static boolean copySong(String songName, String path) {

        InputStream is = null;

        OutputStream os = null;

        BufferedInputStream bis = null;

        BufferedOutputStream bos = null;

        try {

            File file = new File(path + "\\" + songName + ".mp3");

            if (!file.exists()) {

                file.getParentFile().mkdirs();

                file.createNewFile();

            }

            is = new FileInputStream(ROOT_PATH + "\\" + songName + ".mp3");

            os = new FileOutputStream(file);

            bis = new BufferedInputStream(is);

            bos = new BufferedOutputStream(os);

            byte[] buf = new byte[1024];

            int length = 0;

            while ((length = bis.read(buf)) != -1) {

                bos.write(buf, 0, length);

            }

            bos.flush();

        } catch (IOException e) {

            e.printStackTrace();

            return false;

        } finally {

            try {

                if (bis != null) {

                    bis.close();

                }

                if (bos != null) {

                    bos.close();

                }

                if (is != null) {

                    is.close();

                }

                if (os != null) {

                    os.close();

                }

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

        return true;

    }

    public static boolean deleteSong(String song) {

        File file = new File(ROOT_PATH + "\\" + song + ".mp3");

        if (!file.exists()) {

            System.out.println("删除文件失败:" + song + ".mp3不存在!");

            return false;

        } else {

            return file.isFile()  file.delete();

        }

    }

    public static void main(String[] args) {

        System.out.println("请选择您要进行的操作:\n1:查询所有歌曲 \n2:根据歌曲名称复制 \n3:根据歌曲名称删除 \n4: 退出");

        Scanner sc = new Scanner(System.in);

        int num;

        outer:while (sc.hasNext()) {

            num = sc.nextInt();

            switch (num) {

                case 1:

                    System.out.println("存在以下歌曲:");

                    SongUtils.selectAllSong();

                    System.out.println("请选择您要进行的操作:\n1:查询所有歌曲 \n2:根据歌曲名称复制 \n3:根据歌曲名称删除 \n4: 退出");

                    break;

                case 2:

                    System.out.print("请输入要复制的歌曲名称: ");

                    String songName = sc.next();

                    System.out.println("请输入存储路径");

                    String path = sc.next();

                    if (SongUtils.copySong(songName, path)) {

                        System.out.println("复制结果: 歌曲" + songName + "已经成功复制到" + path + "目录中");

                    } else {

                        System.out.println("失败");

                    }

                    System.out.println("请选择您要进行的操作:\n1:查询所有歌曲 \n2:根据歌曲名称复制 \n3:根据歌曲名称删除 \n4: 退出");

                    break;

                case 3:

                    System.out.print("请输入要删除的歌曲名称: ");

                    String songDel = sc.next();

                    if (SongUtils.deleteSong(songDel)) {

                        System.out.println("\n删除结果: 歌曲" + songDel + "已经成功删除");

                    } else {

                        System.out.println("失败");

                    }

                    System.out.println("请选择您要进行的操作:\n1:查询所有歌曲 \n2:根据歌曲名称复制 \n3:根据歌曲名称删除 \n4: 退出");

                    break;

                case 4:

                    System.out.println("退出系统");

                    break outer;

                default:

                    System.out.println("请输入1-4");

                    break ;

            }

        }

    }

}

java集合如何使用流的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java流的使用、java集合如何使用流的信息别忘了在本站进行查找喔。