Shortest code for assigning values to a List
What could be the shortest possible code for this:
public void update(final Product object, final Callback<Product> callback) {
if(object.getIsDayDependent()) {
Double priceDay0 = object.getSundayPrice();
Double priceDay1 = object.getMondayPrice();
Double priceDay2 = object.getTuesdayPrice();
Double priceDay3 = object.getWednesdayPrice();
Double priceDay4 = object.getThursdayPrice();
Double priceDay5 = object.getFridayPrice();
Double priceDay6 = object.getSaturdayPrice();
List<DayPrice> dayPrices = new LinkedList<>();
dayPrices.add(new DayPrice(0, priceDay0));
dayPrices.add(new DayPrice(1, priceDay1));
dayPrices.add(new DayPrice(2, priceDay2));
dayPrices.add(new DayPrice(3, priceDay3));
dayPrices.add(new DayPrice(4, priceDay4));
dayPrices.add(new DayPrice(5, priceDay5));
dayPrices.add(new DayPrice(6, priceDay6));
object.setDayDependent(dayPrices);
} else {
object.setPrice(null);
object.setDayDependent(new LinkedList<>());
}
callback.onSuccess(object);
}
java
|
show 3 more comments
What could be the shortest possible code for this:
public void update(final Product object, final Callback<Product> callback) {
if(object.getIsDayDependent()) {
Double priceDay0 = object.getSundayPrice();
Double priceDay1 = object.getMondayPrice();
Double priceDay2 = object.getTuesdayPrice();
Double priceDay3 = object.getWednesdayPrice();
Double priceDay4 = object.getThursdayPrice();
Double priceDay5 = object.getFridayPrice();
Double priceDay6 = object.getSaturdayPrice();
List<DayPrice> dayPrices = new LinkedList<>();
dayPrices.add(new DayPrice(0, priceDay0));
dayPrices.add(new DayPrice(1, priceDay1));
dayPrices.add(new DayPrice(2, priceDay2));
dayPrices.add(new DayPrice(3, priceDay3));
dayPrices.add(new DayPrice(4, priceDay4));
dayPrices.add(new DayPrice(5, priceDay5));
dayPrices.add(new DayPrice(6, priceDay6));
object.setDayDependent(dayPrices);
} else {
object.setPrice(null);
object.setDayDependent(new LinkedList<>());
}
callback.onSuccess(object);
}
java
I'm guessing you cannot modify theProduct
class, is that correct?
– KevinO
Nov 13 '18 at 16:03
2
Well you could avoid all those local variables usingdayPrices.add(new DayPrice(0, object.getSundayPrice()))
for example. But if you could addProduct.getPrice(int day)
or similar, that would help a lot too...
– Jon Skeet
Nov 13 '18 at 16:03
2
this belongs to codegolf.stackexchange.com
– leonardkraemer
Nov 13 '18 at 16:05
How aboutList<DayPrice> dayPrices = Arrays.asList(new DayPrice(0, object.getSundayPrice()), new DayPrice(1, object.getMondayPrice()),...)
? But why are you not using the callback argument in your method? What's its purpose?
– SpiderPig
Nov 13 '18 at 16:06
@KevinO I can modify Product class somehow, what is your suggestion, maybe that's something that can be worked out in the Product class.
– xybrek
Nov 13 '18 at 16:12
|
show 3 more comments
What could be the shortest possible code for this:
public void update(final Product object, final Callback<Product> callback) {
if(object.getIsDayDependent()) {
Double priceDay0 = object.getSundayPrice();
Double priceDay1 = object.getMondayPrice();
Double priceDay2 = object.getTuesdayPrice();
Double priceDay3 = object.getWednesdayPrice();
Double priceDay4 = object.getThursdayPrice();
Double priceDay5 = object.getFridayPrice();
Double priceDay6 = object.getSaturdayPrice();
List<DayPrice> dayPrices = new LinkedList<>();
dayPrices.add(new DayPrice(0, priceDay0));
dayPrices.add(new DayPrice(1, priceDay1));
dayPrices.add(new DayPrice(2, priceDay2));
dayPrices.add(new DayPrice(3, priceDay3));
dayPrices.add(new DayPrice(4, priceDay4));
dayPrices.add(new DayPrice(5, priceDay5));
dayPrices.add(new DayPrice(6, priceDay6));
object.setDayDependent(dayPrices);
} else {
object.setPrice(null);
object.setDayDependent(new LinkedList<>());
}
callback.onSuccess(object);
}
java
What could be the shortest possible code for this:
public void update(final Product object, final Callback<Product> callback) {
if(object.getIsDayDependent()) {
Double priceDay0 = object.getSundayPrice();
Double priceDay1 = object.getMondayPrice();
Double priceDay2 = object.getTuesdayPrice();
Double priceDay3 = object.getWednesdayPrice();
Double priceDay4 = object.getThursdayPrice();
Double priceDay5 = object.getFridayPrice();
Double priceDay6 = object.getSaturdayPrice();
List<DayPrice> dayPrices = new LinkedList<>();
dayPrices.add(new DayPrice(0, priceDay0));
dayPrices.add(new DayPrice(1, priceDay1));
dayPrices.add(new DayPrice(2, priceDay2));
dayPrices.add(new DayPrice(3, priceDay3));
dayPrices.add(new DayPrice(4, priceDay4));
dayPrices.add(new DayPrice(5, priceDay5));
dayPrices.add(new DayPrice(6, priceDay6));
object.setDayDependent(dayPrices);
} else {
object.setPrice(null);
object.setDayDependent(new LinkedList<>());
}
callback.onSuccess(object);
}
java
java
edited Nov 13 '18 at 16:11
xybrek
asked Nov 13 '18 at 16:00
xybrekxybrek
12.4k47192370
12.4k47192370
I'm guessing you cannot modify theProduct
class, is that correct?
– KevinO
Nov 13 '18 at 16:03
2
Well you could avoid all those local variables usingdayPrices.add(new DayPrice(0, object.getSundayPrice()))
for example. But if you could addProduct.getPrice(int day)
or similar, that would help a lot too...
– Jon Skeet
Nov 13 '18 at 16:03
2
this belongs to codegolf.stackexchange.com
– leonardkraemer
Nov 13 '18 at 16:05
How aboutList<DayPrice> dayPrices = Arrays.asList(new DayPrice(0, object.getSundayPrice()), new DayPrice(1, object.getMondayPrice()),...)
? But why are you not using the callback argument in your method? What's its purpose?
– SpiderPig
Nov 13 '18 at 16:06
@KevinO I can modify Product class somehow, what is your suggestion, maybe that's something that can be worked out in the Product class.
– xybrek
Nov 13 '18 at 16:12
|
show 3 more comments
I'm guessing you cannot modify theProduct
class, is that correct?
– KevinO
Nov 13 '18 at 16:03
2
Well you could avoid all those local variables usingdayPrices.add(new DayPrice(0, object.getSundayPrice()))
for example. But if you could addProduct.getPrice(int day)
or similar, that would help a lot too...
– Jon Skeet
Nov 13 '18 at 16:03
2
this belongs to codegolf.stackexchange.com
– leonardkraemer
Nov 13 '18 at 16:05
How aboutList<DayPrice> dayPrices = Arrays.asList(new DayPrice(0, object.getSundayPrice()), new DayPrice(1, object.getMondayPrice()),...)
? But why are you not using the callback argument in your method? What's its purpose?
– SpiderPig
Nov 13 '18 at 16:06
@KevinO I can modify Product class somehow, what is your suggestion, maybe that's something that can be worked out in the Product class.
– xybrek
Nov 13 '18 at 16:12
I'm guessing you cannot modify the
Product
class, is that correct?– KevinO
Nov 13 '18 at 16:03
I'm guessing you cannot modify the
Product
class, is that correct?– KevinO
Nov 13 '18 at 16:03
2
2
Well you could avoid all those local variables using
dayPrices.add(new DayPrice(0, object.getSundayPrice()))
for example. But if you could add Product.getPrice(int day)
or similar, that would help a lot too...– Jon Skeet
Nov 13 '18 at 16:03
Well you could avoid all those local variables using
dayPrices.add(new DayPrice(0, object.getSundayPrice()))
for example. But if you could add Product.getPrice(int day)
or similar, that would help a lot too...– Jon Skeet
Nov 13 '18 at 16:03
2
2
this belongs to codegolf.stackexchange.com
– leonardkraemer
Nov 13 '18 at 16:05
this belongs to codegolf.stackexchange.com
– leonardkraemer
Nov 13 '18 at 16:05
How about
List<DayPrice> dayPrices = Arrays.asList(new DayPrice(0, object.getSundayPrice()), new DayPrice(1, object.getMondayPrice()),...)
? But why are you not using the callback argument in your method? What's its purpose?– SpiderPig
Nov 13 '18 at 16:06
How about
List<DayPrice> dayPrices = Arrays.asList(new DayPrice(0, object.getSundayPrice()), new DayPrice(1, object.getMondayPrice()),...)
? But why are you not using the callback argument in your method? What's its purpose?– SpiderPig
Nov 13 '18 at 16:06
@KevinO I can modify Product class somehow, what is your suggestion, maybe that's something that can be worked out in the Product class.
– xybrek
Nov 13 '18 at 16:12
@KevinO I can modify Product class somehow, what is your suggestion, maybe that's something that can be worked out in the Product class.
– xybrek
Nov 13 '18 at 16:12
|
show 3 more comments
2 Answers
2
active
oldest
votes
The following doesn't minimize code as much as it reduces the number statements, but it also eliminates unnecessary variables:
if(object.getIsDayDependent()) {
List<Double> prices = Arrays.asList(object.getSundayPrice(),
object.getMondayPrice(),
object.getTuesdayPrice(),
object.getWednesdayPrice(),
object.getThursdayPrice(),
object.getFridayPrice(),
object.getSaturdayPrice());
object.setDayDependent(IntStream.range(0, 7)
.mapToObj(i -> new DayPrice(i, prices.get(i)))
.collect(Collectors.toCollection(LinkedList<Double>::new)));
} else {
object.setPrice(null);
object.setDayDependent(new LinkedList<>());
}
Interesting approach
– xybrek
Nov 13 '18 at 16:23
add a comment |
Without modifying the associated class (Product class) as a challenge, the shortest code I have for this is this:
if(object.getIsDayDependent())) {
Double priceDay0 = object.getSundayPrice();
Double priceDay1 = object.getMondayPrice();
Double priceDay2 = object.getTuesdayPrice();
Double priceDay3 = object.getWednesdayPrice();
Double priceDay4 = object.getThursdayPrice();
Double priceDay5 = object.getFridayPrice();
Double priceDay6 = object.getSaturdayPrice();
object.setDayDependent(new LinkedList<>(Arrays.asList(
new DayPrice(0, priceDay0),
new DayPrice(1, priceDay1),
new DayPrice(2, priceDay2),
new DayPrice(3, priceDay3),
new DayPrice(4, priceDay4),
new DayPrice(5, priceDay5),
new DayPrice(6, priceDay6))));
}
Not much, but maybe there's some more clever way that someone can answer.
The reason I wrapped it withLinkedList
is due to reflection used in my app, so just disregard with I didn't use just aList
– xybrek
Nov 13 '18 at 16:22
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284891%2fshortest-code-for-assigning-values-to-a-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The following doesn't minimize code as much as it reduces the number statements, but it also eliminates unnecessary variables:
if(object.getIsDayDependent()) {
List<Double> prices = Arrays.asList(object.getSundayPrice(),
object.getMondayPrice(),
object.getTuesdayPrice(),
object.getWednesdayPrice(),
object.getThursdayPrice(),
object.getFridayPrice(),
object.getSaturdayPrice());
object.setDayDependent(IntStream.range(0, 7)
.mapToObj(i -> new DayPrice(i, prices.get(i)))
.collect(Collectors.toCollection(LinkedList<Double>::new)));
} else {
object.setPrice(null);
object.setDayDependent(new LinkedList<>());
}
Interesting approach
– xybrek
Nov 13 '18 at 16:23
add a comment |
The following doesn't minimize code as much as it reduces the number statements, but it also eliminates unnecessary variables:
if(object.getIsDayDependent()) {
List<Double> prices = Arrays.asList(object.getSundayPrice(),
object.getMondayPrice(),
object.getTuesdayPrice(),
object.getWednesdayPrice(),
object.getThursdayPrice(),
object.getFridayPrice(),
object.getSaturdayPrice());
object.setDayDependent(IntStream.range(0, 7)
.mapToObj(i -> new DayPrice(i, prices.get(i)))
.collect(Collectors.toCollection(LinkedList<Double>::new)));
} else {
object.setPrice(null);
object.setDayDependent(new LinkedList<>());
}
Interesting approach
– xybrek
Nov 13 '18 at 16:23
add a comment |
The following doesn't minimize code as much as it reduces the number statements, but it also eliminates unnecessary variables:
if(object.getIsDayDependent()) {
List<Double> prices = Arrays.asList(object.getSundayPrice(),
object.getMondayPrice(),
object.getTuesdayPrice(),
object.getWednesdayPrice(),
object.getThursdayPrice(),
object.getFridayPrice(),
object.getSaturdayPrice());
object.setDayDependent(IntStream.range(0, 7)
.mapToObj(i -> new DayPrice(i, prices.get(i)))
.collect(Collectors.toCollection(LinkedList<Double>::new)));
} else {
object.setPrice(null);
object.setDayDependent(new LinkedList<>());
}
The following doesn't minimize code as much as it reduces the number statements, but it also eliminates unnecessary variables:
if(object.getIsDayDependent()) {
List<Double> prices = Arrays.asList(object.getSundayPrice(),
object.getMondayPrice(),
object.getTuesdayPrice(),
object.getWednesdayPrice(),
object.getThursdayPrice(),
object.getFridayPrice(),
object.getSaturdayPrice());
object.setDayDependent(IntStream.range(0, 7)
.mapToObj(i -> new DayPrice(i, prices.get(i)))
.collect(Collectors.toCollection(LinkedList<Double>::new)));
} else {
object.setPrice(null);
object.setDayDependent(new LinkedList<>());
}
edited Nov 13 '18 at 16:26
answered Nov 13 '18 at 16:21
ernest_kernest_k
20.8k42344
20.8k42344
Interesting approach
– xybrek
Nov 13 '18 at 16:23
add a comment |
Interesting approach
– xybrek
Nov 13 '18 at 16:23
Interesting approach
– xybrek
Nov 13 '18 at 16:23
Interesting approach
– xybrek
Nov 13 '18 at 16:23
add a comment |
Without modifying the associated class (Product class) as a challenge, the shortest code I have for this is this:
if(object.getIsDayDependent())) {
Double priceDay0 = object.getSundayPrice();
Double priceDay1 = object.getMondayPrice();
Double priceDay2 = object.getTuesdayPrice();
Double priceDay3 = object.getWednesdayPrice();
Double priceDay4 = object.getThursdayPrice();
Double priceDay5 = object.getFridayPrice();
Double priceDay6 = object.getSaturdayPrice();
object.setDayDependent(new LinkedList<>(Arrays.asList(
new DayPrice(0, priceDay0),
new DayPrice(1, priceDay1),
new DayPrice(2, priceDay2),
new DayPrice(3, priceDay3),
new DayPrice(4, priceDay4),
new DayPrice(5, priceDay5),
new DayPrice(6, priceDay6))));
}
Not much, but maybe there's some more clever way that someone can answer.
The reason I wrapped it withLinkedList
is due to reflection used in my app, so just disregard with I didn't use just aList
– xybrek
Nov 13 '18 at 16:22
add a comment |
Without modifying the associated class (Product class) as a challenge, the shortest code I have for this is this:
if(object.getIsDayDependent())) {
Double priceDay0 = object.getSundayPrice();
Double priceDay1 = object.getMondayPrice();
Double priceDay2 = object.getTuesdayPrice();
Double priceDay3 = object.getWednesdayPrice();
Double priceDay4 = object.getThursdayPrice();
Double priceDay5 = object.getFridayPrice();
Double priceDay6 = object.getSaturdayPrice();
object.setDayDependent(new LinkedList<>(Arrays.asList(
new DayPrice(0, priceDay0),
new DayPrice(1, priceDay1),
new DayPrice(2, priceDay2),
new DayPrice(3, priceDay3),
new DayPrice(4, priceDay4),
new DayPrice(5, priceDay5),
new DayPrice(6, priceDay6))));
}
Not much, but maybe there's some more clever way that someone can answer.
The reason I wrapped it withLinkedList
is due to reflection used in my app, so just disregard with I didn't use just aList
– xybrek
Nov 13 '18 at 16:22
add a comment |
Without modifying the associated class (Product class) as a challenge, the shortest code I have for this is this:
if(object.getIsDayDependent())) {
Double priceDay0 = object.getSundayPrice();
Double priceDay1 = object.getMondayPrice();
Double priceDay2 = object.getTuesdayPrice();
Double priceDay3 = object.getWednesdayPrice();
Double priceDay4 = object.getThursdayPrice();
Double priceDay5 = object.getFridayPrice();
Double priceDay6 = object.getSaturdayPrice();
object.setDayDependent(new LinkedList<>(Arrays.asList(
new DayPrice(0, priceDay0),
new DayPrice(1, priceDay1),
new DayPrice(2, priceDay2),
new DayPrice(3, priceDay3),
new DayPrice(4, priceDay4),
new DayPrice(5, priceDay5),
new DayPrice(6, priceDay6))));
}
Not much, but maybe there's some more clever way that someone can answer.
Without modifying the associated class (Product class) as a challenge, the shortest code I have for this is this:
if(object.getIsDayDependent())) {
Double priceDay0 = object.getSundayPrice();
Double priceDay1 = object.getMondayPrice();
Double priceDay2 = object.getTuesdayPrice();
Double priceDay3 = object.getWednesdayPrice();
Double priceDay4 = object.getThursdayPrice();
Double priceDay5 = object.getFridayPrice();
Double priceDay6 = object.getSaturdayPrice();
object.setDayDependent(new LinkedList<>(Arrays.asList(
new DayPrice(0, priceDay0),
new DayPrice(1, priceDay1),
new DayPrice(2, priceDay2),
new DayPrice(3, priceDay3),
new DayPrice(4, priceDay4),
new DayPrice(5, priceDay5),
new DayPrice(6, priceDay6))));
}
Not much, but maybe there's some more clever way that someone can answer.
answered Nov 13 '18 at 16:22
xybrekxybrek
12.4k47192370
12.4k47192370
The reason I wrapped it withLinkedList
is due to reflection used in my app, so just disregard with I didn't use just aList
– xybrek
Nov 13 '18 at 16:22
add a comment |
The reason I wrapped it withLinkedList
is due to reflection used in my app, so just disregard with I didn't use just aList
– xybrek
Nov 13 '18 at 16:22
The reason I wrapped it with
LinkedList
is due to reflection used in my app, so just disregard with I didn't use just a List
– xybrek
Nov 13 '18 at 16:22
The reason I wrapped it with
LinkedList
is due to reflection used in my app, so just disregard with I didn't use just a List
– xybrek
Nov 13 '18 at 16:22
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284891%2fshortest-code-for-assigning-values-to-a-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I'm guessing you cannot modify the
Product
class, is that correct?– KevinO
Nov 13 '18 at 16:03
2
Well you could avoid all those local variables using
dayPrices.add(new DayPrice(0, object.getSundayPrice()))
for example. But if you could addProduct.getPrice(int day)
or similar, that would help a lot too...– Jon Skeet
Nov 13 '18 at 16:03
2
this belongs to codegolf.stackexchange.com
– leonardkraemer
Nov 13 '18 at 16:05
How about
List<DayPrice> dayPrices = Arrays.asList(new DayPrice(0, object.getSundayPrice()), new DayPrice(1, object.getMondayPrice()),...)
? But why are you not using the callback argument in your method? What's its purpose?– SpiderPig
Nov 13 '18 at 16:06
@KevinO I can modify Product class somehow, what is your suggestion, maybe that's something that can be worked out in the Product class.
– xybrek
Nov 13 '18 at 16:12