Search Java Code Snippets

  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.

#Java - Code Snippets for '#Integer' - 12 code snippet(s) found

 Sample 1. Check if an integer is odd or even using ternary operator

public class BuggyBreadTest {
    public static void main(String[] args) {
       int x = 5;
       boolean isEven = x%2 == 0 ? true:false;
          System.out.println("Odd"); // prints Odd

   Like      Feedback     odd even  ternary operator

 Sample 2. Get the Maximum number out of the Integer List, using Lambda Expression

// Declare and Initialize the Collection

List<Integer> intList = new ArrayList<Integer>();

// Add Elements


System.out.println(; // Prints 1

   Like      Feedback     lambda expression   collections   set  hashset  generics  stream  reducer   Math::max

 Sample 3. Find whether a given integer is odd or even without use of modules operator in java

public static void main(String ar[])
   int n=5;
      System.out.println("Even Number ");
      System.out.println("Odd Number ");

   Like      Feedback     integer

 Sample 4. ArrayList of Optional Integers

List<Optional<Integer>> intList = new ArrayList<Optional<Integer>>();

// Add Elements


   Like      Feedback     optional  java 8   list   arraylist   list of optional integers   arraylist of optional  java.util.Optional

Subscribe to Java News and Posts. Get latest updates and posts on Java from
Enter your email address:
Delivered by FeedBurner
 Sample 5. Method to get a map of words and their count by passing in the string

Map<String,Integer> wordCountMap = new TreeMap();
String[] words = text.split(" ");
Set<Integer> countSet;
for(String word: words) {
wordCountMap.put(word.toLowerCase(), wordCountMap.get(word.toLowerCase()).intValue() + 1);
} else {
wordCountMap.put(word.toLowerCase(), 1);

countSet = new TreeSet(Collections.reverseOrder());

for(Integer inte: countSet) {
for(Entry<String,Integer> entry: wordCountMap.entrySet()){
if(entry.getValue() == inte) {

   Like      Feedback     map  word count  map of string and integer  containsKey  string split  treeset  Collections.reverseOrder  set addAll   entry

 Sample 6. Generate a Random Integer from 0 to 100

double randomNo = Math.random();

   Like      Feedback     random number   random number generation  Math.random()

 Sample 7. Convert from Long object to Integer object

Long longValue = 20l;
Integer int = (int)(long)longValue;

   Like      Feedback     long to int  Long to Integer  casting  casting long to integer

 Sample 8. Assign value to BigInteger upon validating the value using BigIntegerValidator ( Apache Commons )

BigIntegerValidator bigIntegerValidator = BigIntegerValidator.getInstance();
BigInteger bigInteger = bigIntegerValidator.validate("1AD2345");
System.out.println(bigInteger); // prints null as the validation fails because of non numeric characters

   Like      Feedback     Validate a Number  Apache Commons  Assign if the Number is valid  BigInteger

 Sample 9. Check if the numerical value is within a specified range using BigIntegerValidator ( Apache Commons )

System.out.println(bigIntegerValidator.isInRange(12, 0, 100)); // prints true because the value 12 falls in range 0-100

   Like      Feedback     Check if the numerical value is within a specified range  Apache Commons  BigIntegerValidator

Subscribe to Java News and Posts. Get latest updates and posts on Java from
Enter your email address:
Delivered by FeedBurner
 Sample 10. Write a program / method that takes an array of integer and return the difference between the smallest and largest element ?

static int diff(int[] x){
   int smallest = x[0];
   int largest = x[0];
   for(int element: x){
      if(element < smallest){
         smallest = element;
      } else if(element > largest){
         largest = element;
      } else {
   return largest-smallest;

   Like      Feedback     difference between the smallest and largest element   array

 Sample 11. Code Sample / Example / Snippet of java.math.BigInteger

    private X509Certificate generateRootCertificate(String commonName, Date notBefore, Date notAfter) throws Exception {

X500Name issuer = new X500Name(commonName);

BigInteger serial = BigInteger.probablePrime(16, new Random());

SubjectPublicKeyInfo pubKeyInfo = convertToSubjectPublicKeyInfo(m_caKey.getPublic());

X509v3CertificateBuilder builder = new X509v3CertificateBuilder(issuer, serial, notBefore, notAfter, issuer, pubKeyInfo);

builder.addExtension(new Extension(Extension.basicConstraints, true, new DEROctetString(new BasicConstraints(true))));

X509CertificateHolder certHolder = JcaContentSignerBuilder(SIGNATURE_ALGORITHM).build(m_caKey.getPrivate()));

return new JcaX509CertificateConverter().getCertificate(certHolder);


   Like      Feedback      java.math.BigInteger

 Sample 12. Code Sample / Example / Snippet of org.apache.bcel.classfile.ConstantInteger

    public int getValueInt()


if (super.getElementValueType() != PRIMITIVE_INT) {

throw new RuntimeException(

"Dont call getValueString() on a non STRING ElementValue");


final ConstantInteger c = (ConstantInteger) getConstantPool().getConstant(idx);

return c.getBytes();


   Like      Feedback      org.apache.bcel.classfile.ConstantInteger

Subscribe to Java News and Posts. Get latest updates and posts on Java from
Enter your email address:
Delivered by FeedBurner

comments powered by Disqus