collection
class Studnt{}
public class StudentData {
public static void main(String[] args) {
List<Studnt> stuList=new ArrayList<Studnt>();
Studnt s1=new Studnt("name",70);
Studnt s2=new Studnt("anil",54);
Studnt s3=new Studnt("name",75);
Studnt s4=new Studnt("name",45);
Studnt s5=new Studnt("anil",74);
Studnt s6=new Studnt("hi",74);
stuList.add(s1);
stuList.add(s2);
stuList.add(s3);
stuList.add(s4);
stuList.add(s5);stuList.add(s6);
int sum=0;
for(Studnt std:stuList){
for(Studnt std1:stuList){
String name1=std1.getName();
String name=std.getName();
int ma=std.getMarks();
if(name1.equals(name)){
sum=sum+ma;
ma=sum;
System.out.println(name+"::"+ma);
break;
}else{
System.out.println(name+"==="+ma);
break;
}
}
}}
plz give me sol this type of out put
anil::128
hi::74
name::190
View Answers
July 21, 2011 at 2:45 PM
import java.util.*;
class Studnt{
String name;
int marks;
Studnt(String name,int marks){
this.name=name;
this.marks=marks;
}
public String getName(){
return name;
}
public int getMarks(){
return marks;
}
}
public class StudentData{
private static String[] getUniqueElements(String[] allStrs){
String[] temp = new String[allStrs.length];
int count = 0;
for(int j = 0; j < allStrs.length; j++){
if(isUnique(allStrs[j], temp))
temp[count++] = allStrs[j];
}
String[] uniqueStrs = new String[count];
System.arraycopy(temp, 0, uniqueStrs, 0, count);
return uniqueStrs;
}
private static boolean isUnique(String s, String[] array){
for(int j = 0; j < array.length; j++){
if(array[j] != null && s.equals(array[j]))
return false;
}
return true;
}
public static void main(String[] args) {
List<Studnt> stuList=new ArrayList<Studnt>();
Studnt s1=new Studnt("name",70);
Studnt s2=new Studnt("anil",54);
Studnt s3=new Studnt("name",75);
Studnt s4=new Studnt("name",45);
Studnt s5=new Studnt("anil",74);
Studnt s6=new Studnt("hi",74);
stuList.add(s1);
stuList.add(s2);
stuList.add(s3);
stuList.add(s4);
stuList.add(s5);
stuList.add(s6);
int i=0;
String names[]=new String[stuList.size()];
for(Studnt std:stuList){
names[i]=std.getName();
i++;
}
int sum=0;
String name[]=getUniqueElements(names);
int tot[]=new int[name.length];
for(int k=0;k<name.length;k++){
for(Studnt std:stuList){
if(std.getName().equals(name[k])){
sum+=std.getMarks();
}
}
tot[k]=sum;
}
for(int k=name.length-1;k>=0;k--){
if(k==0){
System.out.println(name[0]+" "+(tot[0]));
}
else{
System.out.println(name[k]+" "+(tot[k]-tot[k-1]));
}
}
}
}
Related Tutorials/Questions & Answers:
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
Advertisements
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
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 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
Collection frameworkCollection framework what are the real life examples of using
Collection in java
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
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 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
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
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 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
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 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
garbage collection in java
.style1 {
color: #FFFFFF;
}
Garbage
Collection in Java
In java... through garbage
collection. In other words it
deallocate memory automatically which have no longer use. This technique is
known as Garbage
Collection 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
on collection mapping - Hibernateon
collection mapping differences b/w
collection mappings tags
list,set,bag Hi Friend,
Differences:
A set is a
collection in which no item occurs more than once.
A bag is a
collection in which items may
Collection classes in javaCollection classes in java Normally a database is used... is the reason using java
collection classes saved/stored the data/content.I don't understand, what is the idea using java
collection classes in project
Collection classes in javaCollection classes in java Normally a database is used... is the reason using java
collection classes saved/stored the data/content.I don't understand, what is the idea using java
collection classes in project
Anthem DVD Collection
Anthem DVD
Collection
DVD
collection based on the Ajax Anthem.Net project
Read full descriptionADS_TO_REPLACE_1
struts collection problem in Mozillastruts
collection problem in Mozilla I used the to get list of records, if records are more than 15-20 then this is not seen properly with vertical scroll bar on Mozilla. Plz help me
Filter collection in Java 8Filter
collection in Java 8 Hi,
I have following
collection...,
Example of Filter
collection in Java 8
You can use the following code in Java 8... the data not containing "Java".
Thanks
Hi,
We have huge
collection 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
Java Collection API - Java TutorialsJava
Collection API
Collection was added to Java with J2SE 1.2 release.
Collection framework is
provided in 'java.util.package'.
All collections... implementations
of the
collection interfaces. Also, they are reusable data structures