Showing posts with label List. Show all posts
Showing posts with label List. Show all posts

August 28, 2019

How to Convert Array to ArrayList & Enum Example in Java

/**
 *
 */
package com.konzern.transformatore.enums;

/**
 * @author konzernites
 * @version 1.0
 *
 */
public enum CobolDivision {

IDENTIFICATION_DIVISION("IDENTIFICATION DIVISION."),
DATA_DIVISION("DATA DIVISION."),
PROCEDURE_DIVISION("PROCEDURE DIVISION."),
ENVIRONMENT_DIVISION("ENVIRONMENT DIVISION.");

private String division = null;

private CobolDivision(String division) {
this.division= division;
}

public String getDivision() {
return division;
}


}


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

import com.konzern.transformatore.enums.CobolDivision;

/**
 * 8281808029
 */

/**
 * @author Consumerfed
 * 
 *
 */
public class EnumsTestClass {

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

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

for (CobolDivision cobolDivison : CobolDivision.values()) {
cobolDivisions.add(cobolDivison.getDivision());
System.out.println(cobolDivison.getDivision());
}
System.out.println(cobolDivisions.size());

}

}


Output

IDENTIFICATION DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
ENVIRONMENT DIVISION.
4


Tik Toc Toe in python, two player game, playing with computer complete source code available soon...



Amazon choice the best affordable mobile smart phone

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]

Facebook comments