我有以下情况:
//Case 1
final Set<String> first = A.stream().filter().map().collect()
//Case 2
final Set<String> second = B.stream().filter().map().collect()
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我将两次调用相同的lambda表达式,但使用不同的"consumer"(A或B).有没有解决方案来避免这种重复?
请考虑以下代码段:
List<String> strList = new ArrayList<>();
List<String> strList2 = new ArrayList<>();
strList.forEach(strList2::add);
Run Code Online (Sandbox Code Playgroud)
当list.forEach()只接受具有签名的方法时,如何才能实现Consumer<T>这一点?返回类型不匹配吧?void accept(T t)list::addboolean accept(T t)
我有一个对象:
LocalDate localDate;
int amount;
Run Code Online (Sandbox Code Playgroud)
我有一个这样的对象列表,如:
{2018-01-01, 5}
{2018-01-01, 10}
{2018-01-02, 15}
Run Code Online (Sandbox Code Playgroud)
现在我想要实现我想要从同一天解析金额.预期结果将列出不重复金额的总和
{2018-01-01, 15}
{2018-01-02, 15}
Run Code Online (Sandbox Code Playgroud)
好像我错过了一些流方法来做到这一点......
我一直在尝试重写以下内容以使用Stream API.我似乎无法弄清楚如何到Map(或字段)为System.setProperty(k,v)设置键值对
我的数据只是在它们之间有空格的行,它们分别分为键和值:
foo bar
mykey myvalue
nextkey nextvalue
Run Code Online (Sandbox Code Playgroud)
我的工作源代码在这里:
try {
Scanner scanner = new Scanner(Paths.get("/path/to/file.txt"));
while(scanner.hasNextLine()){
String line = scanner.nextLine();
String[] array = line.split(" ");
if(array.length == 2){
System.setProperty("twitter4j.oauth." + array[0], array[1]);
}
}
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
在这里放一个破碎的样本,虽然我已经遍布地图(呻吟)试图用流写它,但这里只是为了证明我尝试了一次迭代:-p
Stream<String> lines = null;
try {
lines = Files.lines(Paths.get("/Users/bellis/dev/data/twitter.txt"));
} catch (IOException e) {
e.printStackTrace();
}
String[] words = lines.collect((Collectors.joining("\n"))).split(" ");
System.out.println("twitter4j.oauth." + words[0] + " " + words[1]);
Run Code Online (Sandbox Code Playgroud)
上面当然是不正确的我知道有更好的方法来使用函数和其他常见的流成语来编写它,但我似乎无法做到正确.您如何建议使用Stream和功能API编写此代码?
不寻找杰克逊解决方案。
进口清单:
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.util.List;
import java.util.stream.Collectors;
Run Code Online (Sandbox Code Playgroud)
JSON数据:
{
"Name": "ABC.com",
"Developer": "Vishwa ratna",
"Project List": [
"Compnay: National",
"Compnay: Enterprise",
"Compnay: Alamo"
]
}
Run Code Online (Sandbox Code Playgroud)
我的程序:
@SuppressWarnings("unchecked")
public static void main(String[] args)
{
File text = new File("R:/TestJSON.txt");
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("R:\\Desktop\\TestJSON.txt"));
JSONObject jsonObject = (JSONObject) obj;
List<String> list = new ArrayList<>();
String name = (String) jsonObject.get("Name");
System.out.println(name);
String author = …Run Code Online (Sandbox Code Playgroud) 为什么不能 convert java.util.stream.IntStream to java.util.stream.Stream<? extends R>
Stream<String> streamOfStrings = Stream.of("hello", "world");
streamOfStrings.flatMap(String::chars).forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud)
此代码给出了编译错误
Bad return type in method reference: cannot convert java.util.stream.IntStream to java.util.stream.Stream<? extends R>
Run Code Online (Sandbox Code Playgroud) 我正在尝试从orElse / orElseGet流运算符返回HashMap,但似乎无法在这些运算符中使用HashMap。
public class Main {
public static void main(String args[]) {
List<String> names = new ArrayList<String>() {{
add("test1");
}};
HashMap<Integer, String> indexToNameMap = names.stream()
.filter(name -> name.equals("test"))
.map(name -> new HashMap<Integer, String>() {{
put(names.indexOf(name), name);
}})
.findFirst()
.orElseGet(() -> new HashMap<Integer, String>() {{
put(0, "UN_AVAILABLE");
}});
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
Bad return type in lambda expression: HashMap<Integer, String> cannot be converted to HashMap<Integer, String>
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题?
我有一个像下面这样的课程:
public class Company {
private List<Team> teams;
}
public class Team {
String name;
}
Run Code Online (Sandbox Code Playgroud)
我有这样的Compony对象的列表:
List<Company> companies = ...
Run Code Online (Sandbox Code Playgroud)
团队可以属于并出现在多个公司的列表中。
我正在寻找使用Java流将公司分组为地图的方式,并以团队名称作为地图键。每个地图条目的值是团队所属公司的列表。像这样。
Map<String, List<Company>> companiesGroupedByTeam;
Run Code Online (Sandbox Code Playgroud)
因此,同一家公司可能会出现在不同团队的列表中。
有谁知道如何使用Java流进行分组?努力做小组。
谢谢!
假设列表firstName已被初始化。
List<String> firstName= new ArrayList<String>();
firstName.add("John");
firstName.add("Peter");
firstName.add("Foo");
Run Code Online (Sandbox Code Playgroud)
是否可以使用它在一行代码中填充另一个列表?我不知道如何在少于以下几行的情况下做到这一点:
List<FullName> fullName= new ArrayList<FullName>();
firstName.stream().forEach( n -> fullName.add(new FullName(n, "SomeSurname")));
Run Code Online (Sandbox Code Playgroud) 我有两个清单。我想创建一个映射里面会有真正的匹配部件及虚假的唯一一个在Java 8
EG。
输入 -
列表1 = [A,B,C,D]
列表2 = [B,C,Y,Z]
输出 -
地图:
A,假
B,真
C真
D,假
我的代码:
Map<String,Boolean> map = new HashMap<>();
for(String var1 : list1) {
boolean value;
if (CollectionUtils.isNotEmpty(list2)) {
Optional<String> valueOptional = list2.stream()
.filter(e1 -> e1.equalsIgnoreCase(var1))
.findAny();
value = valueOptional.isPresent();
map.put(var1, value);
}
}
Run Code Online (Sandbox Code Playgroud)