add String to a List of Objects in Java [duplicate] - java

This question already has answers here:
Run piece of code contained in a String
(9 answers)
Closed 3 years ago.
List<PhoneBook> list = new ArrayList<PhoneBook>();
String str = "new PhoneBook("David","234567");";
How can I add (or convert) str to code and add it to the list?
I have searched all over the internet, and didn't find anything helpful.

To convert it to fit the arraylist you've given:
List<PhoneBook> list = new ArrayList<>();
PhoneBook not_a_str = new PhoneBook("David","234567");
list.add(not_a_str);
To add strings to a list of objects:
List<Object> list = new ArrayList<>();
String str = "new PhoneBook(\"David\",\"234567\")";
list.add(str);
To add strings to a list of strings (also objects):
List<String> list = new ArrayList<>();
String str = "new PhoneBook(\"David\",\"234567\")";
list.add(str);
list.add("Anything else");
Good luck with java

Related

How to add two lists as attributes to an array of objects in APEX class [duplicate]

This question already has answers here:
Combine two lists of same size (and different type) into list of domain objects using java streams
(2 answers)
How do I combine two lists with the same amount of elements in Java?
(5 answers)
Closed 10 days ago.
How to add two List<String> as array of object attributes
I have two List<String> containing Names and Email Ids. How to add them to an array of objects.
For Ex:
List<String> list1= ['acc1','acc2','acc3','acc4'];
List<String> list2= ['acc1#abc.com','acc2#def.com','acc3#prq.com','acc4.xyz.com'];
I'm looking for an array of objects that look something like below:
Expected Result:
[{'name':'acc1','email':'acc1#abc.com'},{'name':'acc2','email':'acc2#def.com'},{'name':'acc3','email':'acc3#pqr.com'},{'name':'acc4','email':'acc3#xyz.com'}]
Actual result I'm getting:
[{'name':'acc1','email':'acc1#abc.com'},{'name':'acc1','email':'acc2#def.com'},{'name':'acc1','email':'acc3#pqr.com'},{'name':'acc1','email':'acc3#xyz.com'},{'name':'acc2','email':'acc1#abc.com'},{'name':'acc2','email':'acc2#def.com'},{'name':'acc2','email':'acc3#pqr.com'},{'name':'acc2','email':'acc3#xyz.com'},{'name':'acc3','email':'acc1#abc.com'},{'name':'acc3','email':'acc2#def.com'},{'name':'acc3','email':'acc3#pqr.com'},{'name':'acc3','email':'acc3#xyz.com'},{'name':'acc4','email':'acc1#abc.com'},{'name':'acc4','email':'acc2#def.com'},{'name':'acc4','email':'acc3#pqr.com'},{'name':'acc4','email':'acc3#xyz.com'}]
I tried in below code by looping on the two lists but it gives me a different result.
List<ContactWrapper> contactList = new List<ContactWrapper>();
List<String> contEmail = ['acc1','acc2','acc3','acc4'];
List<String> contName = ['acc1#abc.com','acc2#def.com','acc3#prq.com','acc4.xyz.com'];
for(String cEmail: contEmail){
for(String cName: contName){
ContactWrapper cWrap = new ContactWrapper();
cWrap.contactEmail = cEmail;
cWrap.contactName = cName;
contactList.add(cWrap);
}
}
public class ContactWrapper{
public String contactEmail;
public String contactName;
}
I cannot user Java streams as I'm writing the code in APEX

How to convert List object to string in java [duplicate]

This question already has answers here:
How to get the first element of the List or Set? [duplicate]
(9 answers)
Closed 3 years ago.
How do I get the string value from below snippet:
List<Map<String, Object>> list1= (List<Map<String, Object>>) mymap.get("getProduct");
Map<String, Object> myList= list1.get(0);
List<String> myproduct= (List<String>) myList.get("productName");
System.out.println("value " +myproduct.toString());
When I try to print this I'm always getting as an object instead of plain string.
My Output:
[Apple]
Expected output:
Apple
I'm very new to Java, any help would be really appreciated.
Because myproduct is List with string values, for example below is list with fruit names
List<String> fruits = new ArrayList<>();
list.add("apple");
list.add("mango");
System.out.println(fruits); //[apple,mango]
If you want each value from list you can simply iterate using for each loop
for(String str : myproduct) {
System.out.println(str);
}
If you just need first element you can get by index, but might end up with index out of bound exception if list is empty
String ele = myproduct.get(0);
or by using java-8 stream
String ele = myproduct.stream().findFirst().orElse(null);

List initialialization with a first element and an additional collection [duplicate]

This question already has answers here:
What is the shortest way to initialize List of strings in java?
(7 answers)
Closed 3 years ago.
Is there a shorthand way (may be guava or any lib) to initialize a Java List like this?
List list = MagicListUtil.newArrayList(firstElement, moreElementsList);
Guava offers several possibilities
If you have arrays, use Lists.asList(...)
String first = "first";
String[] rest = { "second", "third" };
List<String> list = Lists.asList(first, rest);
If you have lists or other Iterables, use FluentIterable.of(...).append(...).toList():
String first = "first";
List<String> rest = Arrays.asList("second", "third");
List<String> list = FluentIterable.of(first).append(rest).toList();
But you can do that in Java 8 as well
Even though, it's way more verbose, but still...
With an array
String first = "first";
String[] rest = { "second", "third" };
List<String> list = Stream.concat(Stream.of(first), Arrays.stream(rest))
.collect(Collectors.toList());
With a Collection
String first = "first";
List<String> rest = Arrays.asList("second", "third");
List<String> list = Stream.concat(Stream.of(first), rest.stream())
.collect(Collectors.toList());
Yeah you can simply use the java.util.Arrays class for single and multiple elements.
List<String> strings = Arrays.asList("first", "second", "third");
You can use the java.util.Collections for a list with a single element.
List<String> strings = Collections.singletonList("first");
If you wanna copy list, you can do it by constructor like this
List<Float> oldList = new ArrayList<>();
List<Float> newList = new ArrayList<>(oldList);

Adding value to List<String> = new ArrayList<String>(); [duplicate]

This question already has answers here:
Initialization of an ArrayList in one line
(34 answers)
Closed 8 years ago.
Sorry i really dont know how to do this, last resort but to ask.
I wanted to add values to the string list. But my code is not working
private List<String> sample = new ArrayList<String>(){"item1","item2","item3"};
Here it is:
List<String> sample = new ArrayList<String>(Arrays.asList("item1", "item2", "item3"));
Try,
ArrayList<String> list = new ArrayList<String>() {{
add("item1");
add("item2");
add("item3");
}}
OR
ArrayList<String> list = new ArrayList<String>();
list.add("item1");
list.add("item2");
list.add("item3");

Changing an Array to an ArrayList [duplicate]

This question already has answers here:
Create ArrayList from array
(42 answers)
Closed 9 years ago.
I am attempting to change the following array line into an ArrayList that will function the same way:
private String[] books = new String[5];
I changed it to this but it is not functioning properly:
private ArrayList<String>(Arrays.asList(books))
I thought this was how an ArrayList was created
You need to create it like this:
private ArrayList<String> booksList = new ArrayList<String>(Arrays.asList(books));
new ArrayList<String>(Arrays.asList(books)) is the part which is turning your array into an ArrayList.
You could also do:
private List<String> booksList = Arrays.asList(books);
If the fact that it is an ArrayList doesn't matter.
Answered here
new ArrayList<Element>(Arrays.asList(array))
*** new

Categories

Resources