Showing posts with label java collections. Show all posts
Showing posts with label java collections. Show all posts

September 26, 2018

Removing duplicate values from ArrayList in Java

/**
 * This is a sample java program
 * that demonstrate removing duplicate elements from an ArrayList
 *
 * 2 ways - one by using set and other by Iterating through each objects
 *
 * List Interface is implemented by ArrayList, LinkedList, Vector and Stack classes
 *
 */
package com.belazy.collections;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Saurav
 *
 *
 * @version 1.6
 *
 */
public class RemoveDuplicates {

/**
* @param args
*/
public static void main(String[] args) {

List<String> orgList = new ArrayList<String>();
orgList.add("SACHIN");
orgList.add("SACHIN");
orgList.add("ANJALI");
orgList.add("SARA");
orgList.add("ARJUN");

List<String> processedList = new ArrayList<String>();

for(String str:orgList){
if(!processedList.contains(str)){
processedList.add(str);
}
}


Set<String> newSet = new LinkedHashSet<String>(orgList);

System.out.println(" Orginal List : "+orgList);
System.out.println(" After Removing duplicates : "+processedList);
System.out.println(" New Set : "+newSet);



}

}



Output

 Orginal List : [SACHIN, SACHIN, ANJALI, SARA, ARJUN]
 After Removing duplicates : [SACHIN, ANJALI, SARA, ARJUN]
 New Set : [SACHIN, ANJALI, SARA, ARJUN]

January 23, 2018

To remove a value from arraylist without iterating

/**
 * To remove a primitive data type for an Arraylist
 * without out iterating all the elements
 */
package com.belazy.generics;

import java.util.ArrayList;
import java.util.List;

/**
 * @author belazy
 *
 */
public class ArrayListRemoveSample {

/**
* @param args
*/
public static void main(String[] args) {

List<Integer> numberList = new ArrayList<Integer>();
numberList.add(1);
numberList.add(11);
numberList.add(2018);
numberList.add(911);
numberList.add(25);

System.out.println("--- before ---");
for(Integer thisNumber : numberList){
System.out.print(" "+thisNumber+" ");
}


numberList.remove(new Integer(911));

System.out.println("\n--- After ---");
for(Integer thisNumber : numberList){
System.out.print(" "+thisNumber+" ");
}
}

}

May 14, 2017

Java Technical Question in bayt.com

Ross is an event organizer. He has received data regarding the participation of employees in two different events ( say event One and event Two ). Some employees have participated in only one event and others have participated in both events. Ross now needs to count the number of employees who have taken part in both events. The record received by Ross consist of employee ids, which are unique. Write a program that accepts the employee ids participating in each event ( the first line relates to the first event and the second line relates to the second event). The program should print the number of common employee ids in both the events.

Suppose the following input is given to the program, where each line represents a different event.

1001,1002,1003,1004,1005
1106,1008,1005,1003,1016,1017,1112

Now the common employee ids are 1003 and 1005 so the program should give the output as:2

java source code

import java.util.ArrayList;
import java.util.List;

/**
 * solutions for question one
 */

/**
 * @author shimjith consumerfed
 *
 */
public class EventOrganizer {

private List<Integer> eventOne = null;
private List<Integer> eventTwo = null;
private static int count = 0;

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
EventOrganizer main = new EventOrganizer();
main.getEmployeeDetails();
main.showEmployeeDetails();
main.findEmployeeDetails();
System.out.println("The employee present in both events :"+EventOrganizer.count);

}

// to get employees present in both arrays
private void findEmployeeDetails() {
// TODO Auto-generated method stub
for (int row: eventOne){

if(eventTwo.contains(row)){
count = count +1;
}
}

}

private void showEmployeeDetails() {
// TODO Auto-generated method stub
System.out.println("Employee ids of event one "+eventOne);
System.out.println("Employee ids of event two "+eventTwo);
}

private void getEmployeeDetails() {
// You can use scanner class to get values to event one and two
eventOne = new ArrayList<Integer>();
eventTwo = new ArrayList<Integer>();
eventOne.add(1001);
eventOne.add(1002);
eventOne.add(1003);
eventOne.add(1004);
eventOne.add(1005);
eventTwo.add(1106);
eventTwo.add(1008);
eventTwo.add(1005);
eventTwo.add(1003);
eventTwo.add(1016);;
eventTwo.add(1017);
eventTwo.add(1112);

}

}


out put

April 15, 2014

College Administration project source code

College Administration project


: Insert delete and edit student details

Java Example for student manipulations, insertion, deletion, updates and search to an arraylist

The program explains how to use java arraylist,java bean class, java enumerator and how to create a model class for a project

Main class



import com.nij.ca.view.InputClass;


public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
      
        InputClass inputClass_ins = new InputClass();
        inputClass_ins.getInput();

    }

}

---------------------------------------------

Student bean : StudentBean.java



package com.nij.ca.bean;

public class StudentBean {
   
    private String studentName = null;
    private int studentRegNo =0;
   
   
   
    public String getStudentName() {
        return studentName;
    }
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
    public int getStudentRegNo() {
        return studentRegNo;
    }
    public void setStudentRegNo(int studentRegNo) {
        this.studentRegNo = studentRegNo;
    }
   

}


-----------------------------------------

ChoiceEnum.java


package com.nij.ca.Enum;

public enum ChoiceEnum {
   
    INSERT,VIEW,EDIT,SEARCH,DELETE,EXIT

}


------------------------------------

Inputclass.java

package com.nij.ca.view;

import java.util.List;
import java.util.Scanner;

import com.nij.ca.Enum.ChoiceEnum;
import com.nij.ca.bean.StudentBean;

public class InputClass {
   
    StudentManipulation studentManipulation_ins = null;
    Scanner scanner_ins = null;
    ChoiceEnum userChoice;
   
   

    public void getInput() {
   
        scanner_ins = new Scanner(System.in);
        studentManipulation_ins = new StudentManipulation();
        int totalStudent =0;

        do {
            System.out.println("\t\t STUDENT MANIPULATION \n\n");
            System.out.println(" INSERT\n VIEW\n EDIT\n SEARCH\n DELETE\n EXIT ");
          
            System.out.println(" Enter user choice :");
            try{
            userChoice = userChoice.valueOf(scanner_ins.next());
            }
            catch(IllegalArgumentException iae)
            {
                System.out.println(" check the choice you entered (Caps Lock)");
            }
          
            switch (userChoice) {
          
            case INSERT:
                studentManipulation_ins.insert();
                break;
            case VIEW:
                studentManipulation_ins.view();
                break;
            case EDIT:
                System.out.println(" edit ..");
                break;
            case SEARCH:
                studentManipulation_ins.searchRegNo();
                break;
            case DELETE:
                studentManipulation_ins.delete();
                break;
            case EXIT:
                System.exit(0);
                break;

            default:
                break;
            }
          
          
          
        } while (userChoice != null);
    }

}


---------------------------------------

StudentManipulation.java

package com.nij.ca.view;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

import com.nij.ca.bean.StudentBean;

public class StudentManipulation {
   
    //List<StudentBean> studentBean_lst = null;
    Collection<StudentBean> studentBean_lst = null;
    Scanner scanner_ins = null;
    StudentBean studentBean_ins = null;
    int regNo =0;
   
    public StudentManipulation(){
        studentBean_lst = new ArrayList<StudentBean>();
        //studentBean_lst = new LinkedList<StudentBean>();
    }

    public void insert() {
      
        scanner_ins = new Scanner(System.in);
        studentBean_ins = new StudentBean();
      
        System.out.println(" Enter the name of the student ");
        studentBean_ins.setStudentName(scanner_ins.next());
      
        System.out.println(" Enter regno of the student ");
        studentBean_ins.setStudentRegNo(scanner_ins.nextInt());
      
        studentBean_lst.add(studentBean_ins);
      
        System.out.println(" Data Inserted Succesfully  ");
      
    }

    public void view() {
        System.out.println(" \n\n\t LIST OF DATA \n ");
        for(StudentBean student : studentBean_lst)
        {
            System.out.println("\t"+(((ArrayList<StudentBean>) studentBean_lst).indexOf(student)+1)+" :) Name :"+student.getStudentName());
            System.out.println("\t"+(((ArrayList<StudentBean>) studentBean_lst).indexOf(student)+1)+" :) ID   :"+student.getStudentRegNo() );
            System.out.println("\n");
        }
    }

    public void delete() {
        System.out.println(" Enter the regNo to Delete ");
        regNo = scanner_ins.nextInt();
        String name = null;

          for(StudentBean student: studentBean_lst)
          {
              if(student.getStudentRegNo() == regNo)
              {
                  name = student.getStudentName();
                  studentBean_lst.remove(student);
              }
          }
        System.out.println("  DATA DELETED : "+name);
    }

    public void searchRegNo() {
        // TODO Auto-generated method stub
        System.out.println("Search by regNo ");
      
    }

   
}


Please copy and paste the code to your eclipse ide. 


http://javabelazy.blogspot.in/

Facebook comments