मैं समतल जावा को स्कैला में बदलकर समझने की कोशिश कर रहा हूं, ताकि मुझे बेहतर समझ मिल सके।जावा 8 मानचित्र, फ़िल्टर और स्ट्रीम को स्कैला में कैसे परिवर्तित करें?
जावा 8 मानचित्र, फ़िल्टर और स्ट्रीम को स्कैला कैसे परिवर्तित करें?
public Set<String> getValidUsages(String itemId, long sNo, Date timeOfAccess) {
Set<String> itemSet = Sets.newHashSet();
TestWindows testWindows = items.get(itemId).getTestWindows();
final boolean isTV = existsEligibleTestWindow(testWindows.getTV(), timeOfAccess);
if (isTV) {
itemSet.add(TV);
} else {
final boolean isCableUseable = existsEligibleTestWindow(testWindows.getCableUse(), timeOfAccess);
final boolean isWifi = existsEligibleTestWindow(testWindows.getWifi(), timeOfAccess);
if (isCableUseable || isWifi) {
itemSet.add(MOVIE);
}
}
if (testWindows.getUsageIds() != null) {
itemSet.addAll(testWindows.getUsageIds()
.entrySet()
.stream()
.filter(entry -> existsEligibleTestWindow(entry.getValue(), timeOfAccess))
.map(Map.Entry::getKey)
.collect(Collectors.toSet()));
}
return itemSet;
}
private boolean existsEligibleTestWindow(List<TestWindow> windows, Date timeOfAccess) {
if (windows != null) {
return windows.stream()
.filter(w -> withinDateRange(timeOfAccess, w))
.findAny()
.isPresent();
}
return false;
}
private boolean withinDateRange(Date toCheck, TestWindow window) {
return toCheck.after(window.getStartTime()) && toCheck.before(window.getEndTime());
}
मैंने कोशिश की:
मैं निम्नलिखित जावा 8 कोड जो मैं स्काला कन्वर्ट करने के लिए कोशिश कर रहा हूँ है
def withinDateRange(toCheck: Date, window: TestWindow): Boolean = {
toCheck.after(window.getStartTime) && toCheck.before(window.getEndTime)
}
def getValidUsages(itemId: String, sNo: Long, timeOfAccess: Date): Set[String] = {
var itemSet = Sets.newHashSet()
val testWindows = items.value(itemId).getTestWindows
val isTV = existsEligibleTestWindow(testWindows.get(0).getTV, timeOfAccess)
if (isTV) {
itemSet += TV
} else {
val isCableUseable = existsEligibleTestWindow(testWindows.get(0).getCableUse, timeOfAccess)
val isWifi = existsEligibleTestWindow(testWindows.get(0).getWifi, timeOfAccess)
if (isCableUseable || isWifi) {
itemSet += MOVIE
}
}
if (testWindows.get(0).getUsageIds != null) {
itemSet.addAll(testWindows.get(0).getUsageIds.entrySet().stream()
.filter((x) => existsEligibleTestWindow(x._2, timeOfAccess)).map(x => Map.Entry._1)
.collect(Collectors.toSet()))
}
itemSet
}
def existsEligibleConsumptionWindow(windows: List[ConsumptionWindow], timeOfAccess: Date): Boolean = {
if (windows != null) {
return windows.exists((x) => withinDateRange(timeOfAccess, x))
}
false
}
लेकिन जब फिल्टर और धारा कर त्रुटि मिल रही है। क्या कोई दिशा निर्देशित कर सकता है? कोई संदर्भ? मैं getValidUsages पर त्रुटि हो रही है:
compile error “cannot resolve reference project with such signature
त्रुटि संदेश क्या है? – Nyavro
जावा सिंटैक्स को स्कैला सिंटैक्स में बदलने के बजाय, क्या आपने जावाडोक की जांच करने की कोशिश की है? आपको गैर आलसी धारा की आवश्यकता नहीं है। 'फ़िल्टर', 'मानचित्र', 'isEmpty'' सूची ',' सेट' पर सीधे 'फ़िल्टर' के बारे में कैसे? – waltersu
यदि आप जावा संग्रह का उपयोग करना जारी रखना चाहते हैं, तो आपको शायद इसकी आवश्यकता होगी: https://github.com/scala/scala-java8-compat#using-java-8-streams-with-scala-function-converters –