我需要将字符串List转换为userdefinedType数组,对于数组我需要将字符串转换为long.
我已经使用以下方法实现了相同的目标
TeamsNumberIdentifier[] securityPolicyIdArray = securityPolicyIds.stream()
.map(securityPolicy -> new TeamsNumberIdentifier(Long.valueOf(securityPolicy)))
.collect(Collectors.toCollection(ArrayList::new))
.toArray(new TeamsNumberIdentifier[securityPolicyIds.size()]);
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来转换它?
我正在使用Java-8 lambda对列表进行排序.
这是列表
[(t1, tester1), (t4, tester4), (t3, tester3), (t2, tester2)]
Run Code Online (Sandbox Code Playgroud)
排序后
[(t2, tester2), (t1, tester1), (t3, tester3), (t4, tester4)]
Run Code Online (Sandbox Code Playgroud)
我想得到如上所述的结果
String specialId = 't2'
List<Test> list = new ArrayList<>();
Test test1 = new Test();
test1.setId = "t1"
test1.setName = "tester1"
list.add(test1)
Test test2 = new Test();
test2.setId = "t4"
test2.setName = "tester4"
list.add(test2)
Test test3 = new Test();
test3.setId = "t3"
test3.setName = "tester3"
list.add(test3)
Test test4 = new Test();
test4.setId = "t2"
test4.setName = "tester2"
list.add(test4)
Stream<Test> s1 …Run Code Online (Sandbox Code Playgroud) 我正在做一些Java 8流的"代数",也就是说,我正在尝试编写一个简单的操作Op,它将两个流作为输入并产生另一个流.
所以我有这个简单的代码,其目的是在一系列数字中打印secund最高值:
import java.util.Arrays;
import java.util.stream.IntStream;
public class SecundHighestValue {
public static void main(String[] args) {
//setting the input parameters
int [] numbers = {1, 2, 3, 4, 3, 4, 2, 1};
IntStream S1 = Arrays.stream(numbers);
IntStream S2 = Arrays.stream(new int[] {Arrays.stream(numbers).max().getAsInt()} );
// setting the operation
IntStream S3 = S1.filter(x-> x != S2.toArray()[0]); // doesn't work
/*** does work ***
int maxNumber = S2.toArray()[0];
IntStream S3 = S1.filter(x-> x != maxNumber);
*/
// accessing the operation's result stream …Run Code Online (Sandbox Code Playgroud) java functional-programming lazy-evaluation java-8 java-stream
我有List<Person> persons = new ArrayList<>();,我想列出所有独特的名字.我的意思是如果有"John","Max","John","Greg"那么我只想列出"Max"和"Greg".有没有办法用Java流做到这一点?
我正在尝试创建一个素数的无限IntStream,它必须很快 - 在几秒钟内产生大约一百万个素数.所以我尝试创建用过滤器调用IntStream本身生成的流:
IntStream primeStream = IntStream.iterate(2, n -> n + 1)
.filter(PrimeStream::isPrime);
Run Code Online (Sandbox Code Playgroud)
与isPrime像这样:
public boolean isPrime(int n, IntStream primeStream) {
primeStream.forEach((p) -> {
if (n % p == 0)
return false;
if (p > Math.sqrt(n))
break;
});
return true;
}
Run Code Online (Sandbox Code Playgroud)
那可能吗?或者还有其他方式使用Stream来完成我的任务吗?
我有一本字典和一条打印语句,如下所示:
d = {'ar':4, 'ma':4, 'family':pf.Normal()}
print(d)
Run Code Online (Sandbox Code Playgroud)
这给了我
{'ar': 4, 'ma': 4, 'family': <pyflux.families.normal.Normal object at 0x11b6bc198>}
Run Code Online (Sandbox Code Playgroud)
有什么办法可以清理“family”键的值吗?重要的是,调用保持简单的“print(d)”,因为它用于打印其他字典而不会出现此问题。这可能吗?谢谢你的时间。
编辑:
感谢您的回答,我会将其标记为正确,但我还没有尝试过,无法确认。我最终创建了另一个字典,其中清理后的字符串作为键,对象作为值。这需要更多的工作,但我在阅读/得到回复之前就完成了,所以我坚持了下来。还是谢谢了!
public class FetchVarableList {
public static void main(String[] args) {
List<List<Employee>> empsList = new ArrayList<>();
Employee e1 = new Employee(1, "Abi", "Fin", 2000);
Employee e2 = new Employee(2, "Chandu", "OPs", 5000);
Employee e3 = new Employee(3, "mahesh", "HR", 8000);
Employee e4 = new Employee(4, "Suresh", "Main", 1000);
List<Employee> empList = new ArrayList<>();
empList.add(e1); empList.add(e2);
List<Employee> empList2 = new ArrayList<>();
empList2.add(e3); empList2.add(e4);
empsList.add(empList);
empsList.add(empList2);
}
Run Code Online (Sandbox Code Playgroud)
}
上面这段代码是empList2中的emp1,e3,e4中的员工e1,e2列表,这两个员工列表添加到empslist,我想在Integer的单个列表中获取所有员工号码
如何从java8中的一个列表中获取所有员工编号列表
我有一个 ArrayList list,其中包含90, 80, 75要转换为 IntStream 的值。
我曾尝试使用 function list.stream(),但我遇到的问题是,当我尝试使用 lambda 时,例如:
list.stream().filter(x -> x >= 90).forEach(System.out.println(x);
它不会让我执行操作,因为它是常规流,而不是 IntStream。基本上这行应该做的是,如果 90 在列表中,则将其打印出来。
我在这里缺少什么?我不知道如何将 Stream 转换为 IntStream。
我想确定“行”中的“列”或更好:构建地图列表的总和,例如 List> rows
是否可以以某种方式对每个不同列的所有值求和?该函数应返回一个 Map,其中列作为键,所有值的总和作为值。
summMap.get("columname")
Run Code Online (Sandbox Code Playgroud)
假设我有以下地图列表:
List<Map<String, Long>> mapList = new ArrayList();
Map<String, Object> map1 = new HashMap<>();
Map<String, Object> map2 = new HashMap<>();
Map<String, Object> map3 = new HashMap<>();
map1.put("col1", 90);
map1.put("col2", 50);
map1.put("col3", 10);
map2.put("col1", 90);
map2.put("col2", 50);
map2.put("col3", 10);
map3.put("col1", 90);
map3.put("col2", 50);
map3.put("col3", 10);
mapList.add(map1);
mapList.add(map2);
mapList.add(map3);
Map<String, Long> sum = mapList.stream().distinct().sum() // Example
// result i'm awaiting/expecting
Long sumVal1 = sum.get("col1"); // 270
Long sumVal2 = sum.get("col2"); // 150
Long sumVal3 = sum.get("col3"); // …Run Code Online (Sandbox Code Playgroud) 假设我有以下流:
...
import javafx.util.Pair;
...
Pair[] testPairs = {
new Pair<>("apple", "James"),
new Pair<>("banana", "John"),
new Pair<>("grapes", "Tom"),
new Pair<>("apple", "Jenkins"),
new Pair<>("banana", "Edward"),
new Pair<>("grapes", "Pierre")
};
Map<String, List<String>> result1 = Arrays.stream(testPairs)...;
Map<String, String> result2 = Arrays.stream(testPairs)...;
Run Code Online (Sandbox Code Playgroud)
对于result1,我想按对的键进行分组并获取所有对应的名称。对于result2,我想按键分组并获取字符串列表中的任何一个(前一个结果)。
如何通过使用 java 8 stream api 来实现这一点?