Collection Framework
Please help me out..I have a class as below
public class Employee {
private int Id;
private String name;
private int salary;
private int age;
}
public class Collection {
public static void main(String[] args) {
Employee e1 = new Employee();
e1.setId(1);
e1.setAge(23);
e1.setName("SAM");
e1.setSalary(22);
Employee e2 = new Employee();
e2.setId(1);
e2.setAge(24);
e2.setName("MAK");
e2.setSalary(26);
Employee e3 = new Employee();
e3.setId(2);
e3.setAge(25);
e3.setName("JON");
e3.setSalary(27);
List<Employee> listofEmployee = new ArrayList<Employee>();
listofEmployee.add(e1);
listofEmployee.add(e2);
listofEmployee.add(e3);
I want my output to be the objects having Id 1,2.I do not want the other object which has the same Id as 1(using array list only) .Thanks in advance.
View Answers
January 16, 2013 at 4:53 PM
Hi Sundar, wirte your own loginc to display the employees based on the unique id like
public void dispaly(ArrayList<Employee> list) {
for(int i=0;i<list.size();i++) {
int count=0;
Employee emp=list.get(i);
for(int j=0;j<list.size();j++) {
Employee emp1=list.get(j);
if(emp.id==emp1.id)
count++;
}
if(count==1) {
System.out.println("Employee id="+emp.id);
System.out.println("Employee name="+emp.name);
System.out.printl("Employee salary="+emp.salary);
System.out.println("Employee age="+emp.age);
}
}
}
January 16, 2013 at 4:58 PM
sorry, just chang the second for loop and if statements.
public void dispaly(ArrayList<Employee> list) {
for(int i=0;i<list.size();i++) {
int count=0;
Employee emp=list.get(i);
for(int j=0;j<i;j++) {
Employee emp1=list.get(j);
if(emp.id==emp1.id)
count++;
}
if(count==0) {
System.out.println("Employee
id="+emp.id);
System.out.println("Employee name="+emp.name);
System.out.printl("Employee salary="+emp.salary);
System.out.println("Employee age="+emp.age);
}
}
}
Related Tutorials/Questions & Answers:
COLLECTION FRAMEWORKCOLLECTION FRAMEWORK Hi,
i need complete detailed explanation on
COLLECTION FRAMEWORK with examples(which include set,list,queue,map and operations performed on them).need help...
Regards,
Anu
Advertisements
COLLECTION FRAMEWORKCOLLECTION FRAMEWORK Hi,
i need complete detailed explanation on
COLLECTION FRAMEWORK with examples(which include set,list,queue,map and operations performed on them).need help...
Regards,
Anu
Collection frameworkCollection framework what are the real life examples of using
Collection in java
Collection frameworkCollection framework import java.util.*;
public class CollectFrame {
public static void main(String [] args) {
System.out.println( "
Collection Example!\n" );
//int size;
HashSet
collection = new HashSet();
String str1
Collection FrameworkCollection Framework Please help me out..I have a class as below
public class Employee {
private int Id;
private String name;
private int salary;
private int age;
}
public class
Collection {
public static void main
Collection framework tutorialCollection framework tutorial Hi,
I am learning
Collection framework in Java. I want many examples of Java
collection framework. Tell me the best tutorial website for learning Java
Collection framework.
ThanksADS_TO_REPLACE_1
Collection Framework - Java BeginnersCollection Framework Pls explain the disadvantages of using
collection framework as it was written in this forum
? It must cast to correct type.
? Cannot do compile time checking.
I couldnot get in detail as to what
Collection Framework - Java Interview QuestionsCollection Framework While inserting an object which sould be prefered- ArrayList or LinkedList and why? Inserting an object with ArrayListArrayListExample.javaimport java.util.*;import java.io.*;public class
CollectionCollection What is the exact difference between lagacy classes and
collection classes?
and Enumeration is possible on
Collection classes
collectioncollection As we know array holds the similar kind of elements, then in
collection how toArray() method will convert the
collection having different objects as elements to an array in java
collection, Hashtable and Collections and
Collection?
Enumeration : It is series... : It is re-sizable array implementation. Belongs to 'List' group in
collection... Map in
collection.
Collections : It implements Polymorphic algorithms which
Collections Framework to Collections
Framework
Collection Interfaces
Set Interface
Introduction...The Collections
Framework provides a well-designed set of interfaces.... The
collections
framework is a unified architecture which is used to represent
CollectionCollection actually why do we need collections? means we can also store group of objects in relational data base and dbms also provides all the operatoins insert,delete,update,sort,search etc. then why
collection Collections Framework;
Java provides the Collections
Framework.
In the
Collection Framework, a
collection represents the group of the
objects. And a
collection framework.... This
framework is
based on:
Interfaces that categorize common
collection types
Java Collection : NavigableSet ExampleJava
Collection : NavigableSet Example
In this tutorial we are describing NavigableSet of
collection Framework
NavigableSet :
NavigableSet interface... of NavigableSet
.ADS_TO_REPLACE_3
package
collection;
import
Java Collection : WeakHashMap Java
Collection : WeakHashMap
In this tutorial, we are going to discuss one of concept (WeakHashMap ) of
Collection framework.
WeakHashMap...
collection;
import java.util.Map;
import java.util.WeakHashMap;
class
Java Collection : Hashtable Java
Collection : Hashtable
In this tutorial, we are going to discuss one of concept (Hashtable ) of
Collection framework.
Hashtable :
Hashtable...
and also reorganizes internally.
Example : ADS_TO_REPLACE_2
package
collection collection overview and collection interfacecollection overview and
collection interface write the program for
collection interface?
Please visit the following links:
http://www.roseindia.net/java/jdk6/Collection-Interfaces.shtml
http://www.roseindia.net
Framework......Framework...... What is
framework? What is difference between
framework and package
Jaca CollectionJaca Collection does Java
collection allow incompatible types to be stored in single
collection Collection in javaCollection in java What are the meaning of the letters E,T,K and V that come in the
collection concept
Collection ApiCollection Api which scenario we are using arraylist and linkedlist and vector
collection interfacecollection interface methods of
collection interface and their description with example program?
Please visit the following links:
http://www.roseindia.net/java/jdk6/Collection-Interfaces.shtml
http
collection interfacecollection interface methods of
collection interface and their description with example program?
Please visit the following links:
http://www.roseindia.net/java/jdk6/Collection-Interfaces.shtml
http
Java collectionJava collection What are differences between Enumeration, ArrayList, Hashtable and Collections and
Collection garbage collectiongarbage collection how to use garbage
collection in program using core java
Collection ApiCollection Api what is the use of navidableset and navigablemap why we are using that two class
Garbage collectionGarbage collection why garbage
collection? How it works?
Garbage
collection is one of the most important features of Java. The purpose of garbage
collection is to identify and discard objects that are no longer
Collections Framework Enhancements Collections
Framework Enhancements
In
Collection framework, we are able... It provides some new
Collection interfaces also.
Following new Interfaces
Collection HashMapCollection HashMap Please give me an example in which hash map having key value pair and key should be map again means map within map example.
thanks in advance
Collection HashMapCollection HashMap Please give me an example in which hash map having key value pair and key should be map again means map within map example.
thanks in advance
Java Collection API - Java Tutorials.
Collection framework is
provided in 'java.util.package'.
All collections...
collection interface.
The main benefits of
Collection Framework are :ADS... Interface is the base on which the
Collection framework is
built. The methods
Java Collection iterator with example
The Java
Collection Iterator is present at the highest level interface
in the
Collection framework.
Iterator interface has methods for traversing, but
Collection
doesn't has iterator() method.
So create object with reference
Iterate java collection
Collection is the top level interface of the
Collection framework.
Iterator interface has methods for traversing over the elements
of the
collection.
But
Collection doesn't has iterator() method.
So create object
Array to Collection
Array to
Collection
In this example we are converting values of an array into
collection.
List interface is a member of the Java
Collection
Framework collection frame workcollection frame work explain the hierarchy of one dimensional
collection frame work classes
collection frame workcollection frame work could you please tell me detail the concept of
collection frame work