jeudi 22 mai 2014

Java - créer une liste de tableaux de valeurs uniques - Stack Overflow


I have, in Java, an arraylist with those values (many lines, this is just an extract)



20/03/2013 23:31:46 6870 6810 6800 6720 6860 6670 6700 6650 6750 6830 34864 34272 20/03/2013 23:31:46 6910 6780 6800 6720 6860 6680 6620 6690 6760 6790 35072 34496



Where the first two values are strings that containes data and are stored in a single element.


What i want to do is comparing the string data elements and deleting for example the second one and all the elements referred to that line.


For now, i've used a for cycle that every 13 elements compares the string (in order to compare only data strings)


My Question: can i implement other better solutions?


This is my code:


import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Downsampler {
public static void main(String[] args) throws Exception{

//The input file
Scanner s = new Scanner(new File("prova.txt"));


//Saving each element of the input file in an arraylist
ArrayList<String> list = new ArrayList<String>();
while (s.hasNext()){
list.add(s.next());
}
s.close();

//Arraylist to save modified values
ArrayList<String> ds = new ArrayList<String>();

//
int i;
for(i=0; i<=list.size()-13; i=i+14){

//combining the first to values to obtain data
String str = list.get(i)+" "+list.get(i+1);
ds.add(str);
//add all the other values to arraylist ds
int j;
for(j=2; j<14; j++){
ds.add(list.get(i+j));
}

//comparing data values
int k;
for(k=0; k<=ds.size()-12; k=k+13){
ds.get(k); //first data string element
//Comparing with other strings and delete
//TODO
}
}
}
}




Create an Arraylist of unique values



You could use Set.toArray() method.



A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.



http://docs.oracle.com/javase/6/docs/api/java/util/Set.html




You could use a Set. It is a collection which doesn't accept duplicates.




You can easily do this with a Hashmap. You obviously have a key (which is the String data) and some values.


Loop on all your lines and add them to your Map.


Map<String, List<Integer>> map = new HashMap<>();
...
while (s.hasNext()){
String stringData = ...
List<Integer> values = ...
map.put(stringData,values);
}

Note that in this case, you will keep the last occurence of duplicate lines. If you prefer keeping the first occurence and removing the others, you can add a check with Map.containsKey(String stringData); before putting in the map.




If you need unique values, you should use the implementation of the SET interface




Use Set


      ...
Set<String> list = new HashSet<>();
while (s.hasNext()){
list.add(s.next());
}
...



You can read from file to map, where the key is the date and skip if the the whole row if the date is already in map


        Map<String, List<String>> map = new HashMap<String, List<String>>();

int i = 0;
String lastData = null;
while (s.hasNext()) {
String str = s.next();
if (i % 13 == 0) {
if (map.containsKey(str)) {
//skip the whole row
lastData = null;
} else {
lastData = str;
map.put(lastData, new ArrayList<String>());
}
} else if (lastData != null) {
map.get(lastData).add(str);
}


i++;
}


I have, in Java, an arraylist with those values (many lines, this is just an extract)



20/03/2013 23:31:46 6870 6810 6800 6720 6860 6670 6700 6650 6750 6830 34864 34272 20/03/2013 23:31:46 6910 6780 6800 6720 6860 6680 6620 6690 6760 6790 35072 34496



Where the first two values are strings that containes data and are stored in a single element.


What i want to do is comparing the string data elements and deleting for example the second one and all the elements referred to that line.


For now, i've used a for cycle that every 13 elements compares the string (in order to compare only data strings)


My Question: can i implement other better solutions?


This is my code:


import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Downsampler {
public static void main(String[] args) throws Exception{

//The input file
Scanner s = new Scanner(new File("prova.txt"));


//Saving each element of the input file in an arraylist
ArrayList<String> list = new ArrayList<String>();
while (s.hasNext()){
list.add(s.next());
}
s.close();

//Arraylist to save modified values
ArrayList<String> ds = new ArrayList<String>();

//
int i;
for(i=0; i<=list.size()-13; i=i+14){

//combining the first to values to obtain data
String str = list.get(i)+" "+list.get(i+1);
ds.add(str);
//add all the other values to arraylist ds
int j;
for(j=2; j<14; j++){
ds.add(list.get(i+j));
}

//comparing data values
int k;
for(k=0; k<=ds.size()-12; k=k+13){
ds.get(k); //first data string element
//Comparing with other strings and delete
//TODO
}
}
}
}



Create an Arraylist of unique values



You could use Set.toArray() method.



A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.



http://docs.oracle.com/javase/6/docs/api/java/util/Set.html



You could use a Set. It is a collection which doesn't accept duplicates.



You can easily do this with a Hashmap. You obviously have a key (which is the String data) and some values.


Loop on all your lines and add them to your Map.


Map<String, List<Integer>> map = new HashMap<>();
...
while (s.hasNext()){
String stringData = ...
List<Integer> values = ...
map.put(stringData,values);
}

Note that in this case, you will keep the last occurence of duplicate lines. If you prefer keeping the first occurence and removing the others, you can add a check with Map.containsKey(String stringData); before putting in the map.



If you need unique values, you should use the implementation of the SET interface



Use Set


      ...
Set<String> list = new HashSet<>();
while (s.hasNext()){
list.add(s.next());
}
...


You can read from file to map, where the key is the date and skip if the the whole row if the date is already in map


        Map<String, List<String>> map = new HashMap<String, List<String>>();

int i = 0;
String lastData = null;
while (s.hasNext()) {
String str = s.next();
if (i % 13 == 0) {
if (map.containsKey(str)) {
//skip the whole row
lastData = null;
} else {
lastData = str;
map.put(lastData, new ArrayList<String>());
}
} else if (lastData != null) {
map.get(lastData).add(str);
}


i++;
}

0 commentaires:

Enregistrer un commentaire