Could someone explain, how the following code printed the Vector elements (in which order they were printed)?
Code:
import java.util.HashSet;
import java.util.Vector;
class Test
{
public static void main(String [] args) throws Exception
{
Vector data = new Vector();
data.add("apple");
data.add("mango");
data.add("papaya");
data.add("cherry");
data.add("banana");
data.add("apple");
System.out.println(getData(data));
}
public static Vector getData(Vector v)
{
return new Vector(new HashSet(v));
}
}
Output:
[banana, cherry, papaya, apple, mango]
Hashset doesn't store elements in a user specified Order. As soon as you created the Hashset using Vector, Elements lost the specified order.
Moreover they don't allow duplicates, so the second apple got lost.
Related
I a homework where I need to merge two linkedlist, "songs" list and "artists" linked list, it works on the first list the "song" list, but whenever i add another list it just wont work
as you can see here the public static void now has errors
package mergedlinkedlist;
import java.util.LinkedList;
public class Mergedlinkedlist {
public static void main(String[] args) {
LinkedList<String>song = new LinkedList<>();
song.add("imagine");
song.add("bohemian rhapsody");
song.add("highway to hell");
System.out.println(song);
}
public static void main(String[] args) {
LinkedList<String>artists = new LinkedList<>();
artists.add("john lennon");
artists.add("queen");
artists.add("ACDC");
System.out.println(artists);
}
Okey, as one comment has pointed out, no program can have 2 main methods. You cannot create another one.
I suggest to integrate the code from your second into your first. You can safely just add the contents together.
The task is to merge the lists. Do you need any more help with that?
Assuming the task says you know the size of the list you can just create a new list and use the same method you used above to add them to the list. If you do not, you will need to use a loop. It would look like this:
LinkedList<String>artistsAndSongs = new LinkedList<>();
if (artists.size() == songs.size()) {
for (int i = 0; i < artists.size(); i++) {
artistsAndSongs.add(artists.get(i));
artistsAndSongs.add(songs.get(i));
}
} else {
System.out.println("Not every artist is matched with a song!");
}
But you should read up on how linked lists work exactly in the documentation. This will not only improve your understanding of the class but also teach you how to quickly find solutions to your problem.
https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html
So I have recently started my work on Selenium Webdriver using Java. Below is the code:
List<LineDashboardDetails> LineDashboardDetailList = new ArrayList<LineDashboardDetails>();
LineDashboardDetails objLineDetail = new LineDashboardDetails();
for (int i = 0; i < lineUITextList.size(); i++) {
objLineDetail.lineName=lineUITextList.get(i);
objLineDetail.machineNames = machineUITextList.get(i).toString();
LineDashboardDetailList.add(objLineDetail);
}
System.out.println(LineDashboardDetailList);
I have created a lineUITextList ArrayList of String. This array will always have only 1 value in it. So the issue is above for loop does not work. The loop only executes once and comes out to print the LineDashboardDetailList. The machineUITextList array has around 5-6 values in it. My expectation is to have LineDashboardDetailList such that the lineUITextList common value is paired with each new value of machineUITextList.
For example if lineUITextList= {"Noun"}
machineUITextList= {"Pen","Box","Note","Scale"}
So my list i.e LineDashboardDetailList should give me output as:
Noun,Pen
Noun,Box
Noun,Note
Noun,Scale
I am using LineDashboardDetailList List further in my code.
Thanks in Advance.
I am not totally sure what you are trying to achive. Personally, I would use a java.util.Map to associate each value of the lineUITextList with the list of values of machineUITextList.
However, to help you in achiving your goal, first of all, I would design the LineDashboardDetails class in order to maintain the single value of the lineUITextList, along with the list of machineUITextList, so as you can combine them by using a specific method. That has lots of advantages in terms of encapsulation, distribution of responsabilities, etc.., plus you can always reuse for other purposes.
The function to combine the values can be easily implemented by taking advantages of Java stream and built-in functional interfaces.
Here is the code of the LineDashboardDetails class:
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;
public class LineDashboardDetails {
private String lineName;
private List<String> machineNames;
public String getLineName() {
return lineName;
}
public void setLineName(String lineName) {
this.lineName = lineName;
}
public List<String> getMachineNames() {
return new ArrayList<>(machineNames);
}
public void setMachineNames(List<String> machineNames) {
this.machineNames = new ArrayList<>(machineNames);
}
public List<String> getCombinedList() {
return machineNames.stream()
.map(s -> lineName + "," + s)
.collect(Collectors.toList());
}
}
Here is instead the code that you tried to implement, which uses the class above and combine the two list, and finally prints out the list of values as you expect. You can see, I prepared two simple list in the main method, but you can actually generalize it in relation to your own needs:
import java.util.List;
import java.util.ArrayList;
public class SeleniumWebdriverListTest {
public List<LineDashboardDetails> combineUITextListWithMachineUIText(List<String> lineUITextList,
List<String> machineUITextList) {
List<LineDashboardDetails> lineDashboardDetailList = new ArrayList<>();
for (String lineUIText : lineUITextList) {
LineDashboardDetails objLineDetail = new LineDashboardDetails();
objLineDetail.setLineName(lineUIText);
objLineDetail.setMachineNames(machineUITextList);
lineDashboardDetailList.add(objLineDetail);
}
return lineDashboardDetailList;
}
public static void main(String[] args) {
List<String> lineUITextList = new ArrayList<>();
lineUITextList.add("Noun");
List<String> machineUITextList = new ArrayList<>();
machineUITextList.add("Pen");
machineUITextList.add("Box");
machineUITextList.add("Note");
machineUITextList.add("Scale");
List<LineDashboardDetails> lineDashboardDetailList =
new SeleniumWebdriverListTest().combineUITextListWithMachineUIText(
lineUITextList, machineUITextList);
lineDashboardDetailList.stream()
.map(s -> s.getCombinedList())
.forEach(System.out::println);
}
}
I could have used Java streams and a specific lamba expression to implement the combineUITextListWithMachineUIText method as well, but I kept somehow your original version to let you understand my idea of implementation around your code.
Here is the output I get:
[Noun,Pen, Noun,Box, Noun,Note, Noun,Scale]
Feel free to ask for any clarification.
I tried Arrays.asList().contains(string) to compare array items with string but return is false every time even emailid matches .
This is my array:
List<String> arraySE = new ArrayList<String>();
if(Arrays.asList(arraySE).contains(c.getString(c.getColumnIndex(KEY_EMAIL)))){
user.setSelected(true);
} else{
user.setSelected(isSelect);
}
Sample ARRAY:
[user1#gmail.com, user2#gmail.com, user3#gmail.com]
NO ERROR, returning false
A primitive example that works correctly:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> arrayList = new ArrayList<>();
arrayList.add("Hello");
arrayList.add("Hola");
arrayList.add("Hallo");
boolean contains = arrayList.contains("Hello");
System.out.println(contains);
}
}
I would recommend that you debug through your code and especially see what c.getString(c.getColumnIndex(KEY_EMAIL)) returns.
boolean isThere = Arrays.asList(yourArray).contains("test");
if(isThere){
}
else{
}
At first, your list in your example is empty.
Sencodly, you don't need to use Arrays.asList(arraySE), because it is already a list.
You can simply use the contains function to check:
List<String> arraySE = Arrays.asList("user1#gmail.com", "user2#gmail.com", "user3#gmail.com");
if (arraySE.contains("user1#gmail.com")) {
System.out.println("true");
} else {
System.out.println("false");
}
You already have a list of strings, call contains on it. It will work if a match is found.
Why it is failing now is basically when you are calling asList the returned list is not a list of strings, so equals method works differently in that case.
import java.util.ArrayList;
import java.util.Arrays;
public class Test2 {
public static void main(String[] args) {
String str[] = {"A","B","C","D"};
ArrayList a = new ArrayList(Arrays.asList(str));
System.out.println(a.contains("A"));
}
}
I'm new to Java and I want to use one method, getCount, to get the sizes of two arraylists that are not the same size. What code could I use so that I can call, for example, examp1.getCount and examp2.getCount, and get the two different sizes?
This is frankly as basic a question as it gets, and google could have helped you with this easily. Here's your entire method if you want to design your own.
public static int getCount(ArrayList A){
return A.size();
}
In your method, use .size() to get the sizes of the two ArrayLists
and store the sizes in the array and then return that array.
Suppose you create 2 lists li1 and li2 then :
package output;
import java.util.ArrayList;
import java.util.List;
public class CalculateLength {
public static void main(String[] args) {
List<Integer> li1= new ArrayList<>();
li1.add(1);
li1.add(2);
getCount(li1);
List<Integer> li2= new ArrayList<>();
li2.add(1);
li2.add(2);
li2.add(3);
li2.add(4);
getCount(li2);
}
public static int getCount(List list) {
System.out.println("LEngth = " + list.size());
return list.size();
}
}
I'm doing a program that gets as parameter more sentences. And I made 2 lists, one called "propozitie", which contains each sentence, and one called "propozitii", which contains all sentences.
The problem is: When I clear the "propozitie" list after it meets a ".", it also clears the "propozitii" list and it's ending up by printing 3 empty lists.
Input data:
Andrei goes there. Andrei plays football. I enjoy staying home.
Can anybody help me ?
Thanks in advance.
import java.util.ArrayList;
import java.util.List;
public class Propozitie {
private static List<List<String>> propozitii;
private static List<String> propozitie;
public static void main(String args[]) {
propozitii = new ArrayList<List<String>>();
propozitie = new ArrayList<String>();
for (String arg : args) {
propozitie.add(arg);
if (arg.contains(".")) {
propozitii.add(propozitie);
propozitie.clear();
}
}
System.out.println(propozitii);
}
}
This is because, the statement
propozitii.add(propozitie);
adds the reference to propozitie to propozitii list. A copy of the object is not added. To do a copy, use this.
propozitii.add(new ArrayList<String>(propozitie));
now you can clear propozitie