什么是最好的处理 csv Spring Multipartfile?我以前使用过这样的东西:
public void handleFile(MultipartFile multipartFile){
try{
InputStream inputStream = multipartFile.getInputStream();
IOUtils.readLines(inputStream, StandardCharsets.UTF_8)
.stream()
.forEach(this::handleLine);
} catch (IOException e) {
// handle exception
}
}
private void handleLine(String s) {
// do stuff per line
}
Run Code Online (Sandbox Code Playgroud)
据我所知,在处理它之前,这首先将整个文件加载到内存中的一个列表中,对于具有数十万行的文件,这可能需要相当长的时间。
有没有办法逐行处理它而无需手动实现迭代的开销(即使用诸如read(), hasNext(), ... 之类的东西?我正在为文件系统中的文件寻找类似于此示例的简洁内容:
try (Stream<String> stream = Files.lines(Paths.get("file.csv"))) {
stream.forEach(this::handleLine);
} catch (IOException e) {
// handle exception
}
Run Code Online (Sandbox Code Playgroud) 我认为 .class 是它被调用的对象的类。然而,这不符合我试图用程序做的事情。我会举一些例子。
//o is of type Object
//this method throws error "cannot find symbol" for the cast method
SampleClass.cast(o);
Run Code Online (Sandbox Code Playgroud)
//sampleClass is an instance of SampleClass
//this method throws error "<identifier> expected"
sampleClass.class.cast(o);
Run Code Online (Sandbox Code Playgroud)
//this method works!
SampleClass.class.cast(o);
Run Code Online (Sandbox Code Playgroud)
//this returns two "<identifier> expected" errors
sampleClass.class.class.cast(o);
Run Code Online (Sandbox Code Playgroud)
//this works too!
((SampleClass) o)
Run Code Online (Sandbox Code Playgroud)
之前我认为sampleClass.class == SampleClass,一个Class类型的对象。我还认为 SampleClass.class 会返回一些很难想象的元数据。现在我知道我真的什么都不知道。感谢任何帮助解决这个难题的方法:)
编辑:谢谢大家!我很高兴能够学习有关 Java 的知识。我很感激帮助
问题很简单:为什么我们不能在流中的操作中使用StringBuilder(...)as ,但可以用作?identity functionreduce(...)java8string1.concat(string2)identity function
string1.concat(string2)可以看作是相似的builder.append(string)(虽然可以理解为这些操作几乎没有区别),但是我无法理解reduce操作的区别。考虑以下示例:
List<String> list = Arrays.asList("1", "2", "3");
// Example using the string concatenation operation
System.out.println(list.stream().parallel()
.reduce("", (s1, s2) -> s1 + s2, (s1, s2)->s1 + s2));
// The same example, using the StringBuilder
System.out.println(list.stream() .parallel()
.reduce(new StringBuilder(""), (builder, s) -> builder
.append(s),(builder1, builder2) -> builder1
.append(builder2)));
// using the actual concat(...) method
System.out.println(list.stream().parallel()
.reduce("", (s1, s2) -> s1.concat(s2), (s1, s2)->s1.concat(s2)));
Run Code Online (Sandbox Code Playgroud)
这是执行上述行后的输出:
123
321321321321 // output when …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Java 8 流来创建一个 CarData 对象,该对象由列表中所有 CarData 字段的平均值组成getCars;
CarData = new CarData();
CarData.getBodyWeight returns Integer
CarData.getShellWeight returns Integer
Run Code Online (Sandbox Code Playgroud)
List<CarData> carData = carResults.getCars();
IntSummaryStatistics averageBodyWeight = carData.stream()
.mapToInt((x) -> x.getBodyWeight())
.summaryStatistics();
averageBodyWeight.getAverage();
IntSummaryStatistics averageShellWeight = carData.stream()
.mapToInt((x) -> x.getShellWeight())
.summaryStatistics();
getShellWeight.getAverage();
Run Code Online (Sandbox Code Playgroud)
我不想在我最终返回的结果中把这些都放在一起。
视觉上,这是我的清单
getCars() : [
{CarData: { getBodyWeight=10, getShellWeight=3 } }
{CarData: { getBodyWeight=6, getShellWeight=5 } }
{CarData: { getBodyWeight=8, getShellWeight=19 } }
]
Run Code Online (Sandbox Code Playgroud)
我试图实现的输出是一个对象,它具有我指定的每个字段的平均值。不确定我是否需要使用Collectors.averagingInt或 IntSummaryStatistics 的一些组合来实现这一点。对于这些技术中的任何一种,在一个字段中都可以轻松完成,只是不确定在使用多个整数字段时我缺少什么。
{CarData: { getBodyWeight=8, getShellWeight=9 } }
Run Code Online (Sandbox Code Playgroud) 为什么该方法relativize在java-8和java-11上的行为不同?
Path path1 = Paths.get("/a/./b/../image.png");
Path path2 = Paths.get("/a/file.txt");
Path path = path1.relativize(path2);
System.out.println(path);
Run Code Online (Sandbox Code Playgroud)
两个版本的 JavaDoc 描述是相同的。我觉得java-11方式对我来说是正确的行为:
path1:/a/./b/../image.png归一化到/a/b/../image.png哪个归一化到/a/image.pngpath2: /a/file.txt/a/image.png并且/a/file.txt是../file.txt问题
如何在Java的8来计算应该呢?它不会使路径正常化吗?我不明白如何从 head得到结果。
为什么这两个版本之间存在根本没有记录的差异?
我们目前有在 Java 8 中编译的代码,但我们正在 Java 11 VM 上运行它。现在我们也在尝试将我们的代码移动到 Java 11 编译时。想知道 Java 8 中的编译代码与 Java 11 中的编译代码在性能方面是否有任何好处,因为两个编译器都会生成不同的类文件(字节码)?一个在效率方面与另一个有何不同?
我有以下 pojo:
class MyPojo {
String name;
int priority;
}
Run Code Online (Sandbox Code Playgroud)
我有一个List<MyPojo>. 现在,我想检索所有具有最高优先级的元素。这些元素的顺序无关紧要。
Java流可以做到这一点吗?我认为我应该首先按优先级分组,然后获取属于最高优先级的所有元素,但我不确定如何以有效的方式执行此操作。
class Employee
{
int id;
String name;
int age;
String gender;
String department;
int yearOfJoining;
double salary;
public Employee(int id, String name, int age, String gender, String department, int yearOfJoining, double salary)
{
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.department = department;
this.yearOfJoining = yearOfJoining;
this.salary = salary;
}
public int getId()
{
return id;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String getGender()
{ …Run Code Online (Sandbox Code Playgroud) 我有以下字符串
String money = "USD0.00"
Run Code Online (Sandbox Code Playgroud)
想把这个转换成 org.joda.money.Money
我是否需要在此处拆分货币单位和双倍值,并需要在 Money. 或者有任何库 api 可以转换它吗?
我有一个用例,我将其简化为以下程序:
public class A {
private int x = 100;
class B {
private int y = new A().x;
}
public static void main(String []s) {
System.out.println(new A().new B().y);
}
}
Run Code Online (Sandbox Code Playgroud)
该程序运行良好并打印100.
根据文档(访问修饰符):private修饰符确保该字段只能在其自己的类中访问。但在上面的程序中,它似乎自相矛盾。
这是那个意思吗?
请帮我理解。
java ×10
java-8 ×10
java-stream ×3
java-11 ×2
compilation ×1
joda-money ×1
jvm ×1
nio2 ×1
path ×1
reduce ×1
spring ×1