javastringgenericsbounded-wildcardbounded-types

Can we restrict particular string case while loading it to Arraylist object?


A file consisting of many words, where some words are purely in 'UPPERCASE', some words are purely in 'lowercase' and rest are in 'miXeDCasE'? I want to load these words to Arraylist object but it should not allow to load words in UPPERCASE.

Can we achieve this with Java Generics ? How ?

For Ex: as we can achieve only number can be inserted in below Arraylist object with help of bounded types concept of Java Generics.

> ArrayList <T extends Number> al = new ArrayList<T extends Number>();

Solution

  • I don't thing generics provide such functionality for your requirement but you can achieve this easily with following logic if using generics is not mandatory for you

    String word = "SomeWordFromYourFile";
    String wordToUpperCase = word.toUpperCase();
    if(!wordToUpperCase.equals(word)) {
         list.add(word);
    }