Converting the below code from Java 7 to Java 8 using streams
Java 7:
if (Objects.nonNull(input.getList())) {
if (Objects.nonNull(input.getId()) && !input.getList().contains(input.getId())) {
input.getList().add(input.getId());
}
} else if (Objects.nonNull(input.getId())) {
List<Long> ids = new ArrayList<>();
ids.add(input.getId());
input.setList(ids);
}
I tried something like this but it does not work properly.
Java 8
Stream.concat(input.getList().stream(),
input.getList().stream().filter(list2 -> !input.getList().contains(input.getId())));
Input class has two fields List<Long> list and Long id
Can someone please correct where I am missing.
Related
I am new to Java 8, I want to do something like this in java 8:
Map<String, List<Tuple>> userTupleMap = new HashMap<>();
for (Tuple tuple : tupleList) {
userTupleMap.get(tuple.get("user_id",String.class)).add(tuple);
}
I want to create a list of tuples which have same "user_id"
You can use groupingBy of Stream API
Map<String, List<Tuple>> userTupleMap = tupleList.stream()
.collect(Collectors.groupingBy(tuple -> tuple.get("user_id",String.class)));
I am Using Spring batch withing spring boot.
reading a table from source and writing to destination.
i am populating three tables where the read-count for one of the table is less than Actual.
Say In source table there are 2656274 Rows but the read-count I am getting is readCount=2577203, writeCount=2577203
other tables having 8 lacks records and 10 k records working fine
public class DxDatabaseItemReader extends JdbcCursorItemReader<DxDataRead> {
public DxDatabaseItemReader(DxJobContext jobCntx) {
synchronized (this){
System.err.print("Into DxDatabaseItemReader");
String[] dsParam =jobCntx.srcDbName.split("#");
this.setDataSource(createOracleDataSource(dsParam[1],dsParam[2],dsParam[3]));
this.setSql(createFetchQuery(jobCntx.srcFieldNames,jobCntx.srcTableName));//"SELECT SOEID, FST_NAM, LST_NAM FROM REF_PRSNL_MSTR");
this.setFetchSize(0);
this.setRowMapper((ResultSet rs, int rowNum) -> {
Map<String, String> fieldNameMap = new HashMap<>();
for(int i=0;i<jobCntx.srcFieldNames.size();i++)
fieldNameMap.put(getSrcFieldName(jobCntx.srcFieldNames, i), rs.getString(jobCntx.srcFieldNames.get(i)));
DxDataRead dataRead = new DxDataRead();
dataRead.setFieldNameMap(fieldNameMap);
return dataRead;
});
}
}
Expected : it should read all the data and write
Can someone please help me convert the below statements to Java8:
I have a hashmap like this:
private Map<String, Pair<List<XYZFiles>, List<XYZFiles>>> someMap;
I want to convert the below logic in java8:
private String searchFiles(String transmittedFileId) {
for (Pair<List<XYZFiles>, List<XYZFiles>> pair : someMap.values()) {
List<XYZFiles> createdFilesList = pair.getKey();
Optional<XYZFiles> xYZFiles= createdFilesList.stream()
.filter(file ->
file.getId().endsWith(transmittedFileId)).findFirst();
if (xYZFiles.isPresent()) {
return xYZFiles.get().getOriginId();
}
}
}
return someMap.values().stream()
.map(Pair::getKey)
.flatMap(List::stream)
.filter(file ->
file.getId().endsWith(transmittedFileId)
).findFirst().map(XYZFiles::getOriginId).orElse(null);
I think that should do it. It basically does it flat map, which flattens all those lists into one big stream and filters the whole thing.
My map looks like this
private Map<String, LinkedHashSet<String>> map = new HashMap<>();
in traditional approach I can add value to map with key check as below
public void addEdge(String node1, String node2) {
LinkedHashSet<String> adjacent = map.get(node1);
if (adjacent == null) {
adjacent = new LinkedHashSet();
map.put(node1, adjacent);
}
adjacent.add(node2);
}
with java 8, I can do something like this, with this one also I'm getting same output.
map.compute(node1, (k,v)-> {
if(v==null) {
v=new LinkedHashSet<>();
}
v.add(node2);
return v;
});
is there any better way to do with java 8?
Use
map.computeIfAbsent(node1, k -> new LinkedHashSet<>()).add(node2);
If node1 is already found in the map, it will be equivalent to:
map.get(node1).add(node2);
If node1 is not already in the map, it will be equivalent to:
map.put(node1, new LinkedHashSet<>()).add(node2);
This is exactly what you're looking for, and is even described as a use case in the documentation.
you can also use
map.merge(node1,new LinkedHashSet<>(),(v1,v2)->v1!=null?v1:v2).add(node2);
and also
map.compute(node1,(k,v)->v!=null?v:new LinkedHashSet<>()).add(node2);
How do I convert the below mentioned piece of code in Java 1.5?
final List<String> filteredEntries = entries.stream()
.filter(e -> e.toLowerCase().contains(getText().toLowerCase()))
.collect(Collectors.toList());
searchResult.addAll(fitleredEntries)
Kindly suggest.
You can use a for loop:
for (String e : entries) {
if (e.toLowerCase().contains(getText().toLowerCase())) {
searchResult.add(e);
}
}