I have a Multidimensional ArrayList, composed of multiple rows of different length. I would like to save the ArrayList as a single tab-delimited file with multiple columns, in which each column corresponds to a specific row of the ArrayList. I have tried to come up with a solution, but the only thing I could think of is to save each ArrayList row in separate files.
The ArrayList is called "array" and it contains several rows of different length. Here is the code:
try {
PrintWriter output = new PrintWriter(new FileWriter("try"));
output.print(array.get(0));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
In this case, I can save the first row of the ArrayList as a single file. The other solution I have been thinking about is to loop through the rows, to get as many separate files as the row numbers. However, I would like to get a single file with multiple tab-delimited columns.
Ecxuse me, my old answer was based on a wrong interpretation of your needs.
I made you an example code:
package com.test;
import java.util.ArrayList;
import java.util.List;
public class MulitArrayList {
public static void main(String[] args) {
List<String> innerArrayList1;
List<String> innerArrayList2;
List<String> innerArrayList3;
List<List> outerArrayList;
innerArrayList1 = new ArrayList<String>();
innerArrayList2 = new ArrayList<String>();
innerArrayList3 = new ArrayList<String>();
//comic heros
innerArrayList1.add("superman");
innerArrayList1.add("batman");
innerArrayList1.add("catwoman");
innerArrayList1.add("spiderman");
//historical persons
innerArrayList2.add("Stalin");
innerArrayList2.add("Gandy");
innerArrayList2.add("Lincoln");
innerArrayList2.add("Churchill");
//fast food
innerArrayList3.add("mc donalds");
innerArrayList3.add("burger king");
innerArrayList3.add("subway");
innerArrayList3.add("KFC");
//fill outerArrayList
outerArrayList = new ArrayList<List>();
outerArrayList.add(innerArrayList1);
outerArrayList.add(innerArrayList2);
outerArrayList.add(innerArrayList3);
//print
for(List<String> innerList : outerArrayList) {
for(String s : innerList) {
System.out.print(s + "\t");
}
System.out.println("\n");
}
}
}
The result is:
As you can see, the results are printed with tabs, but the space between them differentiate. You can write it the same way into your file, but this is not the best way to save data. I hope I could help you bit, greetings.
Related
Now i have List of ProductDTO, and Product.
This list can contains 100 objects, and can also contains 1m of objects.
This list i am reading from csv file.
How i am filtering it now:
productDtos.parralelStream()
.filter(i -> i.getName.equals(product.getName))
.filter(i -> Objects.equals(i.getCode(), product.getCode()))
.map(Product::new)
// getting object here
So, which is the best way to parse it ? I thought i should use multithreading, one thread will start from beggining of list, other will start from the end of list.
Any ideas how to improve spreed of filtering list in big data cases ?
Thank you
First of all, I see, you have already uploaded all productsDtos right in memory.
It could lead you to very high memory consumption.
I suggest you read CSV files by rows and filter them one by one. In that case, your code might look like the next:
public class Csv {
public static void main(String[] args) {
File file = new File("your.csv");
try (final BufferedReader br = new BufferedReader(new FileReader(file))) {
final List<String> filtered = br.lines().parallel()
.map(Csv::toYourDTO)
.filter(Csv::yourFilter)
.collect(Collectors.toList());
System.out.println(filtered);
} catch (IOException e) {
//todo something with the error
}
}
private static boolean yourFilter(String s) {
return true; //todo
}
private static String toYourDTO(String s) {
return "";//todo
}
}
I used to construct map and use get on it to avoid filter on loop.
For instance, if you Have N code for 1 product, you can do :
Map<String, Map<String, List<ProductDTO>>> productDtoByNameAndCode= productDtos.stream().collect(groupingBy(ProductDTO::getName, groupingBy(ProductDTO::getCode)));
Then you will just have to do for each product :
List<ProductDTO> correspondingProductDTOs = productDtoByNameAndCode.get(product.getName()).get(Product.getCode());
Like that, you haven't to filter all your list every time for each product.
My problem is that I created an array from a csv file and I now have to output any values with duplicates.
The file has a layout of 5x9952. It consists of the data:
id,birthday,name,sex, first name
I'd now like the program to show me in each column (e.g. name) which duplicates there are. Like if there are two people which the same name. But whatever I try from what I found on the Internet only shows me the duplicates of rows (like if name and first name are the same).
Here's what I got so far:
package javacvs;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
/**
*
* #author Tobias
*/
public class main {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
String csvFile = "/Users/Tobias/Desktop/PatDaten/123.csv";
String line = "";
String cvsSplitBy = ",";
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
while ((line = br.readLine()) != null) {
// use comma as separator
String[] patDaten = line.split(cvsSplitBy);
for (int i = 0; i < patDaten.length-1; i++)
{
for (int j = i+1; j < patDaten.length; j++)
{
if( (patDaten[i].equals(patDaten[j])) && (i != j) )
{
System.out.println("Duplicate Element is : "+patDaten[j]);
}
}
}
}
}catch (IOException e) {
e.printStackTrace();
}
}
}
(I changed the name of the csv as it contains confidential data)
The real thing here: stop thinking "low level". Good OOP is about creating helpful abstractions.
In other words: your first stop should be to create a meaningful class definition that represents the content of one row, lets call it the Person class for now. And then you separate your further concerns:
you create one class/method that does nothing else but reading that CSV file - and creating one Person object per row
you create a meaningful data structure that tells you about duplicates
The later could (for example) some kind of reverse indexing. Meaning: you have a Map<String, List<Person>>. And after you have read all your Person objects (maybe in a simple list), you can do this:
Map<String, List<Person>> personsByName = new HashMap<>();
for (Person p : persons) {
List<Person> personsForName = personsByName.get(p.getName());
if (personsByName == null) {
personsForName = new ArrayList<>();
personsByName.put(p.getName(), personsForName);
}
personsForName.add(p);
}
After that loop that map contains all names used in your table - and for each name you have a list of the corresponding persons.
You are iterating upon the rows instead of iterating upon the column. What you need to do is to have the same cycle but upon the column.
What you can do is to acumulate the names in a separate array and than iterate it. I am sure you know what index is the column you want to compare. So you will need one cycle extra to accumulate the column you want to check for duplications.
It's a bit unclear what you want presented, the whole record, or only that there are duplicate names.
For the name only:
String csvFile = "test.csv";
List<String> readAllLines = Files.readAllLines(Paths.get(csvFile));
Set<String> names = new HashSet<>();
readAllLines.stream().map(s -> s.split(",")[2]).forEach(name -> {
if (!names.add(name)) {
System.out.println("Duplicate name: " + name);
}
});
For the whole record:
String csvFile = "test.csv";
List<String> readAllLines = Files.readAllLines(Paths.get(csvFile));
Set<String> names = new HashSet<>();
readAllLines.stream().forEach(record -> {
String name = record.split(",")[2];
if (!names.add(name)) {
System.out.println("Duplicate name: " + name + " with record " + record);
}
});
Your problem is the nesting of your loops. What you do is, that you read one line, split it up and then you compare the fields of this one row with each other. You do not even compare one line with other lines!
So first you need an array for all lines so you can compare these lines. As GhostCat recommended in his answer you should use your own class Person which has the five fields as attributes. But you could use a second array, so you can work with the indexes as Alexander Petrov said in his answer. In the latter case, you get a two-dimensional array:
String[][] patDaten;
After that you read all lines of your csv-file and for each line you create a new Person or a new inner array.
After reading the entire file, you compare the fields as you want. Here you use your double loop. So you compare patDaten[i].getName() with patDaten[j].getName() or with the array patDaten[i][1] with patDaten[j][1].
I've created a method to read a text file and pull out the name of a contact from each line.
private ArrayList<String> readContacts()
{
File cFile = new File ("Contacts.txt");
BufferedReader buffer = null;
ArrayList <String> contact = new ArrayList<String>();
try
{
buffer = new BufferedReader (new FileReader (cFile));
String text;
String sep;
while ((sep = buffer.readLine()) != null)
{
String [] name = sep.split (",");
text = name[1];
contact.add(text);
}
}
catch (FileNotFoundException e)
{
}
catch (IOException k)
{
}
return contact;
}
I'm trying to populate a JList with each contacts name using the method I've created above using this:
model = new DefaultListModel();
for (int i = 1; i < readContacts().size(); i++)
{
ArrayList <String> name = readContacts();
model.addElement(name);
}
nameList = new JList (model);
add(nameList);
When I run the program, the JList only has the numbers 1-10, instead of each of the contacts names. Is the problem I'm running into here logical or problems with syntax? Any help would be great, thanks!
Don't call readContacts() from within the for loop as that makes no sense. You're creating a new ArrayList multiple times and then adding the entire same ArrayList to your JList, in other words, each element in your JList is an ArrayList (???).
Instead call it in the for loop condition or before the for loop.
Do not have empty catch(...) blocks. Doing this is the programming equivalent of driving your car with your eyes closed -- very dangerous.
For example,
model = new DefaultListModel();
// call readContacts() only *once*
for (String name: readContacts()) {
model.addElement(name);
}
I would say we may create a ArrayList object first, then assign the new readContacts() to this object. After that, read the elements in a loop, if you want, you can print out each elements in the ArrayList to ensure those are you want.
If your program is running, you do not have a syntax problem. Your program is running, but is not giving the expected results. That is a logical problem.
You state:
"the JList only has the numbers 1-10, instead of each of the contacts names."
When I look at your readContacts(), you populate your contact List with name[1]. What is in name[1]? I'm lead to believe that name[1] contains 1 - 10 as you read through your file. What is in name[0], or name[2] or name[3], if they even exist. So my first suggestion will be to try a different index for name since name[1] isn't giving you the right result.
Second, you populated your model with the ArrayList name, instead of using the contents of name. Follow Hovercraft Full Of Eels example to populate your model.
This is for my beginning Java class. There are similar questions asking to sort the values that are given in an array; I know how to do that, but here I need to read in a text file, and then sort the values and display them by employee name and the hours that they worked, while also keeping the order from most to least. This is what the text file looks like:
Jim 4 5 6 1 2 3 4
Harry 6 5 1 3 9 2 0
John 2 3 1 6 7 8 4
Lisa 2 1 5 4 1 2 6
And here is all that I know about reading in text files and my current code for this project.
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class EmployeeWorkHours {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
File file = new File("/Users/ODonnell/Desktop/inputData.txt");
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Parsing
Look at the Javadoc for java.util.Scanner, or use autocomplete in your IDE, you'll see a lot more methods than nextLine() etc, the ones of interest
hasNextInt() returns true when next token is a number
nextInt() the next integer
Storage
Now you need to store your numbers, I'd recommend a List as you won't know how many there are upfront which rules out primitive arrays.
Create a list with List hours = new ArrayList();
Add to it with add()
You'll also need to store your employees names, for simplicity I'd recommend using a Map of String to hours list, i.e. Map>.
Create with Map> employeeHours = new HashMap>()
Add to using employeeHours.put(name, hours)
Sorting
java.util.Collections.sort is all you need. This will sort your list by default in ascending order.
Displaying
Most if not all built in list implementations by default implement toString() so you can simply call System.out.println(hours)
You should save the hours worked and the names of your employees in a HashMap.
http://en.wikipedia.org/wiki/Hash_table For an explation of a HashMap.
After storing your values you can sort the HashMap like it is explained in the following link:
Sort a Map<Key, Value> by values (Java)
You should use next() method on scanner instance to read next token instead of whole line. Then you have to try to parse it to integer to recognize if it is a name of employee or its work hour. In the for loop we are sorting data (using Collections utility class) and printing it.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class EmployeeWorkHours {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
File file = new File("inputData.txt");
Map<String, List<Integer>> data = new HashMap<String, List<Integer>>();
try {
Scanner scanner = new Scanner(file);
List<Integer> currentEmployee = null;
while (scanner.hasNextLine()) {
String token = scanner.next();
try {
currentEmployee.add(new Integer(token));
} catch (NumberFormatException e) {
currentEmployee = new ArrayList<Integer>();
data.put(token, currentEmployee);
}
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
for (String name : data.keySet()) {
Collections.sort(data.get(name));
Collections.reverse(data.get(name));
System.out.println(name + " " + data.get(name));
}
}
}
I am trying to write a method that takes an ArrayList of Strings as a parameter and that places a string of four asterisks in front of every string of length 4.
However, in my code, I am getting an error in the way I constructed my method.
Here is my mark length class
import java.util.ArrayList;
public class Marklength {
void marklength4(ArrayList <String> themarklength){
for(String n : themarklength){
if(n.length() ==4){
themarklength.add("****");
}
}
System.out.println(themarklength);
}
}
And the following is my main class:
import java.util.ArrayList;
public class MarklengthTestDrive {
public static void main(String[] args){
ArrayList <String> words = new ArrayList<String>();
words.add("Kane");
words.add("Cane");
words.add("Fame");
words.add("Dame");
words.add("Lame");
words.add("Same");
Marklength ish = new Marklength();
ish.marklength4(words);
}
}
Essentially in this case, it should run so it adds an arraylist with a string of "****" placed before every previous element of the array list because the lengths of the strings are all 4.
BTW
This consists of adding another element
I am not sure where I went wrong. Possibly in my for loop?
I got the following error:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
at Marklength.marklength4(Marklength.java:7)
at MarklengthTestDrive.main(MarklengthTestDrive.java:18)
Thank you very much. Help is appreciated.
Let's think about this piece of code, and pretend like you don't get that exception:
import java.util.ArrayList;
public class Marklength {
void marklength4(ArrayList <String> themarklength){
for(String n : themarklength){
if(n.length() ==4){
themarklength.add("****");
}
}
System.out.println(themarklength);
}
}
Ok, so what happens if your list just contains item.
You hit the line if(n.length() ==4){, which is true because you are looking at item, so you go execute its block.
Next you hit the line themarklength.add("****");. Your list now has the element **** at the end of it.
The loop continues, and you get the next item in the list, which happens to be the one you just added, ****.
The next line you hit is if(n.length() ==4){. This is true, so you execute its block.
You go to the line themarklength.add("****");, and add **** to the end of the list.
Do we see a bad pattern here? Yes, yes we do.
The Java runtime environment also knows that this is bad, which is why it prevents something called Concurrent Modification. In your case, this means you cannot modify a list while you are iterating over it, which is what that for loop does.
My best guess as to what you are trying to do is something like this:
import java.util.ArrayList;
public class Marklength {
ArrayList<String> marklength4(ArrayList <String> themarklength){
ArrayList<String> markedStrings = new ArrayList<String>(themarklength.size());
for(String n : themarklength){
if(n.length() ==4){
markedStrings.add("****");
}
markedStrings.add(n);
}
System.out.println(themarklength);
return markedStrings;
}
}
And then:
import java.util.ArrayList;
public class MarklengthTestDrive {
public static void main(String[] args){
ArrayList <String> words = new ArrayList<String>();
words.add("Kane");
words.add("Cane");
words.add("Fame");
words.add("Dame");
words.add("Lame");
words.add("Same");
Marklength ish = new Marklength();
words = ish.marklength4(words);
}
}
This...
if(n.length() ==4){
themarklength.add("****");
}
Is simply trying to add "****" to the end of the list. This fails because the Iterator used by the for-each loop won't allow changes to occur to the underlying List while it's been iterated.
You could create a copy of the List first...
List<String> values = new ArrayList<String>(themarklength);
Or convert it to an array of String
String[] values = themarklength.toArray(new String[themarklength.size()]);
And uses these as you iteration points...
for (String value : values) {
Next, you need to be able to insert a new element into the ArrayList at a specific point. To do this, you will need to know the original index of the value you are working with...
if (value.length() == 4) {
int index = themarklength.indexOf(value);
And then add a new value at the required location...
themarklength.add(index, "****");
This will add the "****" at the index point, pushing all the other entries down
Updated
As has, correctly, been pointed out to me, the use of themarklength.indexOf(value) won't take into account the use case where the themarklength list contains two elements of the same value, which would return the wrong index.
I also wasn't focusing on performance as a major requirement for the providing a possible solution.
Updated...
As pointed out by JohnGarnder and AnthonyAccioly, you could use for-loop instead of a for-each which would allow you to dispense with the themarklength.indexOf(value)
This will remove the risk of duplicate values messing up the index location and improve the overall performance, as you don't need to create a second iterator...
// This assumes you're using the ArrayList as the copy...
for (int index = 0; index < themarklength.size(); index++) {
String value = themarklength.get(index);
if (value.length() == 4) {
themarklength.add(index, "****");
index++;
But which you use is up to you...
The problem is that in your method, you didn't modify each string in the arraylist, but only adds 4 stars to the list. So the correct way to do this is, you need to modify each element of the arraylist and replace the old string with the new one:
void marklength4(ArrayList<String> themarklength){
int index = 0;
for(String n : themarklength){
if(n.length() ==4){
n = "****" + n;
}
themarklength.set(index++, n);
}
System.out.println(themarklength);
}
If this is not what you want but you want to add a new string "**" before each element in the arraylist, then you can use listIterator method in the ArrayList to add new additional element before EACH string if the length is 4.
ListIterator<String> it = themarklength.listIterator();
while(it.hasNext()) {
String name = it.next();
if(name.length() == 4) {
it.previous();
it.add("****");
it.next();
}
}
The difference is: ListIterator allows you to modify the list when iterating through it and also allows you to go backward in the list.
I would use a ListIterator instead of a for each, listiterator.add likely do exactly what you want.
public void marklength4(List<String> themarklength){
final ListIterator<String> lit =
themarklength.listIterator(themarklength.size());
boolean shouldInsert = false;
while(lit.hasPrevious()) {
if (shouldInsert) {
lit.add("****");
lit.previous();
shouldInsert = false;
}
final String n = lit.previous();
shouldInsert = (n.length() == 4);
}
if (shouldInsert) {
lit.add("****");
}
}
Working example
Oh I remember this lovely error from the good old days. The problem is that your ArrayList isn't completely populated by the time the array element is to be accessed. Think of it, you create the object and then immediately start looping it. The object hence, has to populate itself with the values as the loop is going to be running.
The simple way to solve this is to pre-populate your ArrayList.
public class MarklengthTestDrive {
public static void main(String[] args){
ArrayList <String> words = new ArrayList<String>() {{
words.add("Kane");
words.add("Cane");
words.add("Fame");
words.add("Dame");
words.add("Lame");
words.add("Same");
}};
}
}
Do tell me if that fixes it. You can also use a static initializer.
make temporary arraylist, modify this list and copy its content at the end to the original list
import java.util.ArrayList;
public class MarkLength {
void marklength4(ArrayList <String> themarklength){
ArrayList<String> temp = new ArrayList<String>();
for(String n : themarklength){
if(n.length() ==4){
temp.add(n);
temp.add("****");
}
}
themarklength.clear();
themarklength.addAll(temp);
System.out.println(themarklength);
}
}