Share

Search Java Code Snippets


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





#Java - Code Snippets for '#Case' - 4 code snippet(s) found

 Sample 1. Switch Case with Enum
Admin
info@buggybread.com
public enum State {
NEVADA, CALIFORNIA, WASHINGTON;
}

public static void method(States stateName) {
switch (stateName) {
case NEVADA:
System.out.println("You are in Nevada");
break;

case CALIFORNIA:
System.out.println("You are in California");
break;

case WASHINGTON:
System.out.println("You are in Washington");
break;
}
}

   Like      Feedback     switch case  switch  case  enum   switch with enums   switch and enums


 Sample 2. Method to convert all elements of a collection ( Set ) to lower case.

Set<String> convertLowerCase(Set<String> set){
   Set<String> newSet = new HashSet();
   for(String s: set){
      newSet.add(s.toLowerCase());
   }
      
   return newSet;
}

   Like      Feedback     string  collections  set  .tolowercase()


 Sample 3. Retry in case of exception

public static final int NUMBER_OF_RETRIES = 5;

try {
   // do something
} catch (Exception e) {
   int count;
   for (count = 1; count <= NUMBER_OF_RETRIES; count++) {
      Thread.sleep(5000);
   } catch (InterruptedException e1) {
      try {
         // do something again
         break;
      } catch (Exception ex) {
   }
}

   Like      Feedback     exception handling   exception   Retry in case of exception   for loop   for   Thread.sleep   InterruptedException


 Sample 4. Usage of Apache Commons CaseInsensitiveMap

Map<String, String> map = new CaseInsensitiveMap<String, String>();
map.put("US", "Washington");
map.put("us", "Washington DC");
      
System.out.println(map); // Prints {us=Washington DC} as Keys are case insensitive

   Like      Feedback     CaseInsensitiveMap  Map with Case insensitive keys  apache commons  apache commons collections



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



comments powered by Disqus