Solved

making a new List

Posted on 2014-01-28
7
273 Views
Last Modified: 2014-02-13
I  have two  lists:
List<Student> studentList1
List<Student> studentList2


student class looks like below...

class Student{
 private int ID
 ......
 ........

}


Now ...I want to build a a new  list  


Logic:  replace  students from studentList1 with the students from studentList2  whoose studentId matches.

What is the quicker approach ?  I may have to do such operations  for  5/6  lists.
0
Comment
Question by:cofactor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 39814725
replace  students from studentList1 with the students from studentList2  whoose studentId matches.
Why are they not identical in all respects - how do they differ?
0
 

Author Comment

by:cofactor
ID: 39814742
they differ by some fields values -  they are just populated. I want to keep them.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 400 total points
ID: 39814749
replace  students from studentList1 with the students from studentList2  whoose studentId matches.
Why are they not identical in all respects - how do they differ?

Assuming the Student class is uniquely identified by its ID (it should be) then it follows the ID can be used by Student.hashCode and Student.equals. For a List<Student> 'sl' then the following might be used:

	Set<Student> s = new HashSet<Student>(sl1);
	s.addAll(sl2); 

Open in new window

If there are instances in the second List you don't want to end up in the first, then an iterative approach might be better
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

Author Comment

by:cofactor
ID: 39814872
>>Set<Student> s = new HashSet<Student>(sl1);
>>s.addAll(sl2);

what is sl1   and sl2 in your code ? is this the complete code ?  

I am bit confused.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39814960
what is sl1   and sl2 in your code
The first and second List<Student>
0
 
LVL 2

Expert Comment

by:c_kedar
ID: 39855368
If the order in the list is not important then use set as suggested by CEHJ.
Remember to implement equals and hashcode in Student for this to work.
Eclipse can do this for you: Source > Generate hashCode() and equals()
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		LogTest other = (LogTest) obj;
		if (id != other.id)
			return false;
		return true;
	}

Open in new window


Also note that size of combined set can be greater than first list, i.e. combined set will have Students that were not present in first list but present in second list.
If this is not what you want, then
s.retainAll(sl1);

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39855717
:)

If you need a hashCode method for Student, you can just use its ID (which should be unique)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question