How to filter a list with linq depends on counting a property at the same list and take a random group at least minimum
By : Etienne Marais
Date : March 29 2020, 07:55 AM
like below fixes the issue Not sure how you wanted the null vlanids handled... Did you never want to return those? If so, just add a .Where(v=>v.vlandid!=null) at the end of the other Where clauses. The code supplied is able to be dropped into LinqPad (using statements). Feel free to play with it as needed. code :
var ips= new[]{
new {ip="100.100.101",VLanID=(int?)100,isReserved=0,PackageId=0},
new {ip="100.100.102",VLanID=(int?)100,isReserved=0,PackageId=0},
new {ip="200.200.201",VLanID=(int?)200,isReserved=0,PackageId=0},
new {ip="200.200.202",VLanID=(int?)200,isReserved=0,PackageId=1},
new {ip="300.300.301",VLanID=(int?)null,isReserved=0,PackageId=0},
new {ip="300.300.302",VLanID=(int?)null,isReserved=0,PackageId=0},
new {ip="400.400.401",VLanID=(int?)400,isReserved=0,PackageId=0},
new {ip="400.400.402",VLanID=(int?)400,isReserved=0,PackageId=0}
};
var numOfIps=2;
var result=ips.GroupBy(k=>k.VLanID)
.Where(v=>v.Count()>=numOfIps)
.Where(v=>v.All(i=>i.isReserved==0))
/*.Where(v=>v.Key!=null) Remove null vlans */
/*.OrderBy(x => Guid.NewGuid()) Pseudo Randomize */
.First(v=>v.All(i=>i.PackageId==0))
.Select(v=>v);
result.Dump();
|
Can I filter out duplicates within an array list using hashset in Java without distorting the order of the array?
By : mdwfp
Date : March 29 2020, 07:55 AM
hope this fix your issue What you are are looking for is a Collection that maintains insertion order and does not allow duplicates. Thankfully, there is one available in the form of a LinkedHashSet
|
java: searching for the minimum in a 2d array and giving back the position of the minimum (row and coumn)
By : Mya
Date : March 29 2020, 07:55 AM
this one helps. Indxing in Java is 0-based, so you are ignoring the first row completely. After that you are ignoring all the elements in the first column except excelMatrix[1][1]. The correct implementation will be: code :
double lowest = Double.parseDouble(excelMatrix[0][0]);
int row = 0, column = 0;
for(int r = 0; r< excelMatrix.length; r++) {
for(int c = 0; c<excelMatrix[r].length; c++) {
double number = Double.parseDouble(excelMatrix[r][c]);
if(lowest > number) {
lowest = number;
row = r, column = c;
}
}
}
System.out.print(lowest + " at row: " + row + "and column: " + column);
|
I need to use java 8 filter to filter my list of custom objects in efficient manner
By : Dave
Date : March 29 2020, 07:55 AM
Does that help The code below is in java 6. How to code it in java 8 using streams and functional capabilities ? , The equivalent of your code in Java 8 would be : code :
public static void main(String[] args) {
List<FileTransAuditInfo> workRequestFileTransAuditList = new ArrayList<>();
final String type1 = "type 1";
final String type2 = "type 2";
List<FileTransAuditInfo> workRequestFileTransAuditListforSubmission = workRequestFileTransAuditList.stream()
.filter(file -> file.getFileEventType().equalsIgnoreCase(type1) || file.getFileEventType().equalsIgnoreCase(type2))
.collect(Collectors.toList());
}
|
Filter Custom Objects in Array list or List
By : Mohaned Derar
Date : March 29 2020, 07:55 AM
wish of those help You can use retrolambda library to get Java 8 stream similar functions: code :
List<Student> filteredList = StreamSupport
.stream(yourInputStudentList)
.filter(item -> item.getAge() == "20")
.collect(Collectors.toList());
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.4.0'
classpath 'me.tatarka:gradle-retrolambda:3.2.0' // Add this
}
}
|