• Post Reply Bookmark Topic Watch Topic
  • New Topic

Intersection In ArrayList  RSS feed

 
Arsh Boy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hie my code is like follows.I want add the lines of data from text file to arraylist.After add the lines to array list,i want to get the interesection between the lines of data.But i could not get the intersection and it returns empty.Could someone help me to rectify the problem?thank you.My sample data from specific line in text file as follows,

line 1 data:5 1 1 1 2 1 3 1 1

line 2 data:5 4 4 5 7 10 3 2 1

try {

File f1 = new File("C:/users/User1/Desktop/Datasets/Dataset.txt");
String filename = f1.getAbsolutePath();
FileReader reader = new FileReader(filename);
BufferedReader br1 = new BufferedReader(reader);

//adding the text file data to arraylist called lines

List<String> lines =Files.readAllLines(Paths.get(f1.getPath()),Charset.forName("UTF-8"));

//print out the number of lines in text file
System.out.println("Number Of Lines :"+lines.size());


// add first line data into arraylist called one

one.add(lines.get(1).toString());


//add second line data to arraylist called two

Set<String> two = new HashSet<String>();
two.add(lines.get(2).toString());

//Finding intersection between two lines
ArrayList<String> intersec = new ArrayList<String>(one);
intersec.retainAll(two);
for(String s: intersec){
System.out.println(s);
}



}
catch (FileNotFoundException ex) {

}
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So according to you, what's the intersection of "5 1 1 1 2 1 3 1 1" and "5 4 4 5 7 10 3 2 1"?
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The list is zero based, so change it to this...

 
Arsh Boy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:So according to you, what's the intersection of "5 1 1 1 2 1 3 1 1" and "5 4 4 5 7 10 3 2 1"?


I meant the intersection would be 5,1,3
 
Arsh Boy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arsh Boy wrote:
Stephan van Hulst wrote:So according to you, what's the intersection of "5 1 1 1 2 1 3 1 1" and "5 4 4 5 7 10 3 2 1"?


I meant the intersection would be 5,1,3


When i run it,I get the output like this

Intersection of two Set [5 1 1 1 2 1 3 1 1] and [5 4 4 5 7 10 3 2 1] is []

Can you see the NULL value there?
Actually i want the answer be like

Intersection of two Set [5 1 1 1 2 1 3 1 1] and [5 4 4 5 7 10 3 2 1] is [5 1 3]

i still could not figure it out why it does not print the intersections..
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arsh Boy wrote: . . .
Can you see the NULL value there?
. . .
It is null not NULL.

And there is no null value there. What you have is an empty set. If you had null there it would look like [null]
And from what you have written, the (set) intersection of those two Lists is not [1,3,5]. Have you read about retainAll?

I think you need to go through your code and write down carefully what you are adding to which List and when. Particularly the types. Then you should be able to see what you are doing wrong.
 
Arsh Boy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Arsh Boy wrote: . . .
Can you see the NULL value there?
. . .
It is null not NULL.

And there is no null value there. What you have is an empty set. If you had null there it would look like [null]
And from what you have written, the (set) intersection of those two Lists is not [1,3,5]. Have you read about retainAll?

I think you need to go through your code and write down carefully what you are adding to which List and when. Particularly the types. Then you should be able to see what you are doing wrong.


Okay..Thanks for the explanation..
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what did you find the problem was?
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The bit I am failing to see is the bit where you (presumably) split the string into individual numbers and add them one by one to the set.


is going to result in one element in your set: ["5 1 1 1 2 1 3 1 1"]
where as what you appear to want is [5, 1, 1, 1, 2, 1, 3, 1, 1 ] or as it is a set, it would end up as [5, 1, 2, 3 ]
Four individual numbers.
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I tried it, I passed the String to a Scanner constructor and looped through it with hasNextInt. There are lots of other ways to do it.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, but that's not what OP is doing, and where his problem lies.
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought OP had sorted the problem. I was obviously mistaken.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!