• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 652
  • Last Modified:

Java: how to avoid adding duplicates to vector?

Hello Experts,

The code below adds duplicates to the vector, how can I avoid duplicates?

Pattern p = Pattern.compile("Sip\\w*");

String file = "message SipRoute test in SipUser " + "\n"
	+ "receipt SipCall at time SipUpdate " + "\n"
	+ "message SipReg during SipDiv " + "\n"
	+ "receipt SipCall at time SipUpdate " + "\n";
		    
ArrayList<String> list = new ArrayList<String>();

Matcher matcher = p.matcher(file);
		    
while(matcher.find() == true){
	list.add(matcher.group());
}
		    
Vector<String> events = new Vector<String>();		    
String evts[] = list.toString().split("[\\[,\\s\\]]");

for(String evt: evts){				
	if(evt.isEmpty()){
		evt.trim();
	}else{					
		events.add(evt);
	}					
} 
			
//test
for (int x = 0; x < events.size(); x++) {
	System.out.println("events: " + events.get(x));
}

Open in new window


In advance, thank you for your help.
0
epifanio67
Asked:
epifanio67
  • 3
  • 2
  • 2
1 Solution
 
ChetOS82Commented:
You will have to scan the vector before adding an entry to be sure it doesn't exist.

for(String evt: evts){				
	if(evt.isEmpty()){
		evt.trim();
	}else{
                        bool foundOne = false;
                           for(String curEvt: events){
                                 				
		if (curEvt == evt) {
                                          foundOne = true;
                                          break;
                                   }
                           }

                           if (!foundOne) {
		       events.add(evt);
                            }
	}					
}

Open in new window

0
 
epifanio67Author Commented:
Thank you expert,

hmmm... it is still adding duplicates though..
just tested it...

Regards,
0
 
ChetOS82Commented:
I am a C# programmer, it is possible that my string comparison is wrong.  Set a breakpoint at

if (curEvt == evt) {

and make sure it does what I think (if the strings are the same, set foundOne to true).
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
CEHJCommented:
You should probably be making Java beans (e.g. something like 'SipData') out of each set of data and adding them to a Set (e.g. Set<SipData>) thereby

a. organising your data properly
b. eliminating duplicates naturally
0
 
epifanio67Author Commented:
Thank you CEHJ,

I am dealing with large data... over 1 million lines or > 50MB per file.... when I try that my heap fills up....

Regards,
0
 
epifanio67Author Commented:
thank you experts,

your string comparison was wrong; but the logic was right:

if (curEvt.contains(evt)) {

thank you for your help
0
 
CEHJCommented:
I am dealing with large data... over 1 million lines or > 50MB per file.... when I try that my heap fills up....

Ah i see. In that case, you should probably be using a database
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now