- Writing in a file
- Formatting string in simple java
- Reading from a file inm java
- forEAch Stream, interate wihtotu strem
- intin set, and list
- config readers
- Enum ordinal
- search in a set by class names
List<Account> topAccounts = new ArrayList<>(accounts.values());
topAccounts.sort((a, b) -> Double.compare(b.getBalance(), a.getBalance()));
int withdrawalSum = (int) trxList.stream()
.filter(trx -> trx.getType() == Trx.Type.WITHDRAWAL)
.mapToDouble(Trx::getAmount)
.sum();
Map<Integer, AccountWithTrx> topAccounts = new HashMap<>();
Map<Integer, AccountWithTrx> topAccountSorted = new java.util.TreeMap<>(java.util.Collections.reverseOrder());
// Return only the top N entries (up to 3) as a new map
return topAccounts.entrySet().stream()
.limit(N)
.collect(java.util.LinkedHashMap::new,
(m, e) -> m.put(e.getKey(), e.getValue()),
java.util.LinkedHashMap::putAll
);
ArrayList<Integer> num = new ArrayList<>();
boolean removed = num.removeIf(n -> (n % 3 == 0));
String myStr = "Hello";
System.out.println(myStr.startsWith("Hel"));
for (Object o: objects) {
// Do operation on o
}
Map<String, String> map = ...
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + "/" + entry.getValue());
}
Optional<Person> result = people.stream()
.filter(p -> p.getName().equals("Rama") && p.getAge() == 25)
.findFirst();
if (result.isPresent()) {
System.out.println("Found: " + result.get().getName());
} else {
System.out.println("Not found");
}
List<Person> matches = people.stream()
.filter(p -> p.getCity().equals("Vrindavan") && p.getAge() > 20)
.collect(Collectors.toList());
🧠Notes:
- Use .findFirst() or .findAny() when you just want one.
- Use .collect(Collectors.toList()) to get all matches.
- You can also combine with .map() or .sorted() if needed.