JAVA: Iterating vertical object values in a collection to horizontal object values
I have a list of an object with 3 items that accepts BookID, bookseries, and Format as shown below:
List<BookFormat> book= new ArrayList<BookFormat>();
book.add(1,A,A0);
book.add(1,A,A1);
book.add(1,A,A2);
book.add(2,B,A1);
book.add(2,B,A2);
book.add(3,C,A4);
book.add(4,D,A0);
book.add(4,D,A2);
book.add(4,D,A3);
Now I'm trying to sort it on the horizontal basis on the "BookID" on which I have created another new class object "BookFormatNew" that returns true or false if the specified "Format" is available or not.
class BookFormatNew{
String BookID,
String BookSeries,
String A0,
String A1,
String A2,
String A3,
String A4}
where A0, A1, A2, A3, and A4 takes up "T" if the value is available or "F" if not.
The expected output result should be something like this
List<BookFormatNew> bookNew= new ArrayList<BookFormatNew>();
bookNew.add(1,A,T,T,T,F,F);
bookNew.add(2,B,F,T,T,F,F);
bookNew.add(3,C,F,F,F,F,T);
bookNew.add(4,D,T,F,T,T,F);
How to iterate the vertical values for the BookFormat in order to get an output which is the NewBookFormat as horizontal values?
Thanks for taking out your time!
java android arrays
add a comment |
I have a list of an object with 3 items that accepts BookID, bookseries, and Format as shown below:
List<BookFormat> book= new ArrayList<BookFormat>();
book.add(1,A,A0);
book.add(1,A,A1);
book.add(1,A,A2);
book.add(2,B,A1);
book.add(2,B,A2);
book.add(3,C,A4);
book.add(4,D,A0);
book.add(4,D,A2);
book.add(4,D,A3);
Now I'm trying to sort it on the horizontal basis on the "BookID" on which I have created another new class object "BookFormatNew" that returns true or false if the specified "Format" is available or not.
class BookFormatNew{
String BookID,
String BookSeries,
String A0,
String A1,
String A2,
String A3,
String A4}
where A0, A1, A2, A3, and A4 takes up "T" if the value is available or "F" if not.
The expected output result should be something like this
List<BookFormatNew> bookNew= new ArrayList<BookFormatNew>();
bookNew.add(1,A,T,T,T,F,F);
bookNew.add(2,B,F,T,T,F,F);
bookNew.add(3,C,F,F,F,F,T);
bookNew.add(4,D,T,F,T,T,F);
How to iterate the vertical values for the BookFormat in order to get an output which is the NewBookFormat as horizontal values?
Thanks for taking out your time!
java android arrays
add a comment |
I have a list of an object with 3 items that accepts BookID, bookseries, and Format as shown below:
List<BookFormat> book= new ArrayList<BookFormat>();
book.add(1,A,A0);
book.add(1,A,A1);
book.add(1,A,A2);
book.add(2,B,A1);
book.add(2,B,A2);
book.add(3,C,A4);
book.add(4,D,A0);
book.add(4,D,A2);
book.add(4,D,A3);
Now I'm trying to sort it on the horizontal basis on the "BookID" on which I have created another new class object "BookFormatNew" that returns true or false if the specified "Format" is available or not.
class BookFormatNew{
String BookID,
String BookSeries,
String A0,
String A1,
String A2,
String A3,
String A4}
where A0, A1, A2, A3, and A4 takes up "T" if the value is available or "F" if not.
The expected output result should be something like this
List<BookFormatNew> bookNew= new ArrayList<BookFormatNew>();
bookNew.add(1,A,T,T,T,F,F);
bookNew.add(2,B,F,T,T,F,F);
bookNew.add(3,C,F,F,F,F,T);
bookNew.add(4,D,T,F,T,T,F);
How to iterate the vertical values for the BookFormat in order to get an output which is the NewBookFormat as horizontal values?
Thanks for taking out your time!
java android arrays
I have a list of an object with 3 items that accepts BookID, bookseries, and Format as shown below:
List<BookFormat> book= new ArrayList<BookFormat>();
book.add(1,A,A0);
book.add(1,A,A1);
book.add(1,A,A2);
book.add(2,B,A1);
book.add(2,B,A2);
book.add(3,C,A4);
book.add(4,D,A0);
book.add(4,D,A2);
book.add(4,D,A3);
Now I'm trying to sort it on the horizontal basis on the "BookID" on which I have created another new class object "BookFormatNew" that returns true or false if the specified "Format" is available or not.
class BookFormatNew{
String BookID,
String BookSeries,
String A0,
String A1,
String A2,
String A3,
String A4}
where A0, A1, A2, A3, and A4 takes up "T" if the value is available or "F" if not.
The expected output result should be something like this
List<BookFormatNew> bookNew= new ArrayList<BookFormatNew>();
bookNew.add(1,A,T,T,T,F,F);
bookNew.add(2,B,F,T,T,F,F);
bookNew.add(3,C,F,F,F,F,T);
bookNew.add(4,D,T,F,T,T,F);
How to iterate the vertical values for the BookFormat in order to get an output which is the NewBookFormat as horizontal values?
Thanks for taking out your time!
java android arrays
java android arrays
edited Dec 31 '18 at 7:10
Nagesh Katna
409516
409516
asked Dec 31 '18 at 6:46
Hussain Abdul MajeedHussain Abdul Majeed
33
33
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can use Map while iterating vertical list.
HashMap<Integer, BookFormatNew> map = new HashMap<>();
for(BookFormat b: book){
BookFormatNew bfn = map.get(b.getBookId());
if(bfn == null){
// Create new EntryPair and add
BookFormatNew bfn_new = new BookFormatNew(b.getBookId(), b.getBookSeries(), "F","F","F","F","F");
map.put(b.getBookId(), bfn_new);
bfn = map.get(b.getBookId());
}
if(b.getFormat().equals("A0"){
bfn.setA0("T");
}
else if(b.getFormat().equals("A1"){
bfn.setA1("T");
}
else if(b.getFormat().equals("A2"){
bfn.setA2("T");
}
else if(b.getFormat().equals("A3"){
bfn.setA3("T");
}
else if(b.getFormat().equals("A4"){
bfn.setA4("T");
}
map.put(b.getBookId(),bfn); // Puts updated T,F set
}
Then iterate through the map and add it to new horizontal list if necessary or use the map itself.
What is bfn_new here?
– Hussain Abdul Majeed
Dec 31 '18 at 8:32
New BookFormatNew Object. Since corresponding BookFormatNew doesn't exist yet in Map, create one and put it to map.
– okcomputer_kid
Dec 31 '18 at 8:36
How do you create one?
– Hussain Abdul Majeed
Dec 31 '18 at 9:16
I have updated answer with complete code. That gives you required hashmap
– okcomputer_kid
Dec 31 '18 at 9:33
Is this line "map.add(b.getBookId(), bfn_new);" supposed to be "map.put(b.getBookId(), bfn_new);"as HashMap doesnt have .add?
– Hussain Abdul Majeed
Dec 31 '18 at 10:22
|
show 7 more comments
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%2f53984447%2fjava-iterating-vertical-object-values-in-a-collection-to-horizontal-object-valu%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use Map while iterating vertical list.
HashMap<Integer, BookFormatNew> map = new HashMap<>();
for(BookFormat b: book){
BookFormatNew bfn = map.get(b.getBookId());
if(bfn == null){
// Create new EntryPair and add
BookFormatNew bfn_new = new BookFormatNew(b.getBookId(), b.getBookSeries(), "F","F","F","F","F");
map.put(b.getBookId(), bfn_new);
bfn = map.get(b.getBookId());
}
if(b.getFormat().equals("A0"){
bfn.setA0("T");
}
else if(b.getFormat().equals("A1"){
bfn.setA1("T");
}
else if(b.getFormat().equals("A2"){
bfn.setA2("T");
}
else if(b.getFormat().equals("A3"){
bfn.setA3("T");
}
else if(b.getFormat().equals("A4"){
bfn.setA4("T");
}
map.put(b.getBookId(),bfn); // Puts updated T,F set
}
Then iterate through the map and add it to new horizontal list if necessary or use the map itself.
What is bfn_new here?
– Hussain Abdul Majeed
Dec 31 '18 at 8:32
New BookFormatNew Object. Since corresponding BookFormatNew doesn't exist yet in Map, create one and put it to map.
– okcomputer_kid
Dec 31 '18 at 8:36
How do you create one?
– Hussain Abdul Majeed
Dec 31 '18 at 9:16
I have updated answer with complete code. That gives you required hashmap
– okcomputer_kid
Dec 31 '18 at 9:33
Is this line "map.add(b.getBookId(), bfn_new);" supposed to be "map.put(b.getBookId(), bfn_new);"as HashMap doesnt have .add?
– Hussain Abdul Majeed
Dec 31 '18 at 10:22
|
show 7 more comments
You can use Map while iterating vertical list.
HashMap<Integer, BookFormatNew> map = new HashMap<>();
for(BookFormat b: book){
BookFormatNew bfn = map.get(b.getBookId());
if(bfn == null){
// Create new EntryPair and add
BookFormatNew bfn_new = new BookFormatNew(b.getBookId(), b.getBookSeries(), "F","F","F","F","F");
map.put(b.getBookId(), bfn_new);
bfn = map.get(b.getBookId());
}
if(b.getFormat().equals("A0"){
bfn.setA0("T");
}
else if(b.getFormat().equals("A1"){
bfn.setA1("T");
}
else if(b.getFormat().equals("A2"){
bfn.setA2("T");
}
else if(b.getFormat().equals("A3"){
bfn.setA3("T");
}
else if(b.getFormat().equals("A4"){
bfn.setA4("T");
}
map.put(b.getBookId(),bfn); // Puts updated T,F set
}
Then iterate through the map and add it to new horizontal list if necessary or use the map itself.
What is bfn_new here?
– Hussain Abdul Majeed
Dec 31 '18 at 8:32
New BookFormatNew Object. Since corresponding BookFormatNew doesn't exist yet in Map, create one and put it to map.
– okcomputer_kid
Dec 31 '18 at 8:36
How do you create one?
– Hussain Abdul Majeed
Dec 31 '18 at 9:16
I have updated answer with complete code. That gives you required hashmap
– okcomputer_kid
Dec 31 '18 at 9:33
Is this line "map.add(b.getBookId(), bfn_new);" supposed to be "map.put(b.getBookId(), bfn_new);"as HashMap doesnt have .add?
– Hussain Abdul Majeed
Dec 31 '18 at 10:22
|
show 7 more comments
You can use Map while iterating vertical list.
HashMap<Integer, BookFormatNew> map = new HashMap<>();
for(BookFormat b: book){
BookFormatNew bfn = map.get(b.getBookId());
if(bfn == null){
// Create new EntryPair and add
BookFormatNew bfn_new = new BookFormatNew(b.getBookId(), b.getBookSeries(), "F","F","F","F","F");
map.put(b.getBookId(), bfn_new);
bfn = map.get(b.getBookId());
}
if(b.getFormat().equals("A0"){
bfn.setA0("T");
}
else if(b.getFormat().equals("A1"){
bfn.setA1("T");
}
else if(b.getFormat().equals("A2"){
bfn.setA2("T");
}
else if(b.getFormat().equals("A3"){
bfn.setA3("T");
}
else if(b.getFormat().equals("A4"){
bfn.setA4("T");
}
map.put(b.getBookId(),bfn); // Puts updated T,F set
}
Then iterate through the map and add it to new horizontal list if necessary or use the map itself.
You can use Map while iterating vertical list.
HashMap<Integer, BookFormatNew> map = new HashMap<>();
for(BookFormat b: book){
BookFormatNew bfn = map.get(b.getBookId());
if(bfn == null){
// Create new EntryPair and add
BookFormatNew bfn_new = new BookFormatNew(b.getBookId(), b.getBookSeries(), "F","F","F","F","F");
map.put(b.getBookId(), bfn_new);
bfn = map.get(b.getBookId());
}
if(b.getFormat().equals("A0"){
bfn.setA0("T");
}
else if(b.getFormat().equals("A1"){
bfn.setA1("T");
}
else if(b.getFormat().equals("A2"){
bfn.setA2("T");
}
else if(b.getFormat().equals("A3"){
bfn.setA3("T");
}
else if(b.getFormat().equals("A4"){
bfn.setA4("T");
}
map.put(b.getBookId(),bfn); // Puts updated T,F set
}
Then iterate through the map and add it to new horizontal list if necessary or use the map itself.
edited Dec 31 '18 at 10:23
answered Dec 31 '18 at 7:22
okcomputer_kidokcomputer_kid
33839
33839
What is bfn_new here?
– Hussain Abdul Majeed
Dec 31 '18 at 8:32
New BookFormatNew Object. Since corresponding BookFormatNew doesn't exist yet in Map, create one and put it to map.
– okcomputer_kid
Dec 31 '18 at 8:36
How do you create one?
– Hussain Abdul Majeed
Dec 31 '18 at 9:16
I have updated answer with complete code. That gives you required hashmap
– okcomputer_kid
Dec 31 '18 at 9:33
Is this line "map.add(b.getBookId(), bfn_new);" supposed to be "map.put(b.getBookId(), bfn_new);"as HashMap doesnt have .add?
– Hussain Abdul Majeed
Dec 31 '18 at 10:22
|
show 7 more comments
What is bfn_new here?
– Hussain Abdul Majeed
Dec 31 '18 at 8:32
New BookFormatNew Object. Since corresponding BookFormatNew doesn't exist yet in Map, create one and put it to map.
– okcomputer_kid
Dec 31 '18 at 8:36
How do you create one?
– Hussain Abdul Majeed
Dec 31 '18 at 9:16
I have updated answer with complete code. That gives you required hashmap
– okcomputer_kid
Dec 31 '18 at 9:33
Is this line "map.add(b.getBookId(), bfn_new);" supposed to be "map.put(b.getBookId(), bfn_new);"as HashMap doesnt have .add?
– Hussain Abdul Majeed
Dec 31 '18 at 10:22
What is bfn_new here?
– Hussain Abdul Majeed
Dec 31 '18 at 8:32
What is bfn_new here?
– Hussain Abdul Majeed
Dec 31 '18 at 8:32
New BookFormatNew Object. Since corresponding BookFormatNew doesn't exist yet in Map, create one and put it to map.
– okcomputer_kid
Dec 31 '18 at 8:36
New BookFormatNew Object. Since corresponding BookFormatNew doesn't exist yet in Map, create one and put it to map.
– okcomputer_kid
Dec 31 '18 at 8:36
How do you create one?
– Hussain Abdul Majeed
Dec 31 '18 at 9:16
How do you create one?
– Hussain Abdul Majeed
Dec 31 '18 at 9:16
I have updated answer with complete code. That gives you required hashmap
– okcomputer_kid
Dec 31 '18 at 9:33
I have updated answer with complete code. That gives you required hashmap
– okcomputer_kid
Dec 31 '18 at 9:33
Is this line "map.add(b.getBookId(), bfn_new);" supposed to be "map.put(b.getBookId(), bfn_new);"as HashMap doesnt have .add?
– Hussain Abdul Majeed
Dec 31 '18 at 10:22
Is this line "map.add(b.getBookId(), bfn_new);" supposed to be "map.put(b.getBookId(), bfn_new);"as HashMap doesnt have .add?
– Hussain Abdul Majeed
Dec 31 '18 at 10:22
|
show 7 more comments
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%2f53984447%2fjava-iterating-vertical-object-values-in-a-collection-to-horizontal-object-valu%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