0

I have a set of a set variable that (I guess) contains Strings returned from a query (that's all I know and need to retrieve) defined in this way:

private Set<Set<Type_A>> variable_A;
variable_A = query.getVarA();

Now I need a getter that returns an array of these Strings, and tried with the code below with no success, due to the its return type I guess.

I can't also debug my code because it's a javabean used by a jsp file and Eclipse won't give me a clue on how to proceed. Is there a way to fix this? I tried with public void getVarA() and public ArrayList<String> getVarA()

public Set<Axiom> getVarA() {
    Iterator itr1 = variable_A.iterator();
    Set set;
    while(itr1.hasNext()) { 
        set = (HashSet)itr1.next();
    }                 
    return set; 
}
GondraKkal
  • 87
  • 2
  • 16

2 Answers2

0

Could be something like this, though I'm not entirely sure what is it your trying to achieve. Hope this helps ;)

public class Foo<T> {

    private final Set<Set<T>> setsOfSets = new HashSet<>();

    public static void main(String[] args) {
        final Foo<String> tester = new Foo<>();
        Set<String> set1 = new HashSet<>(Arrays.asList("A", "B"));
        Set<String> set2 = new HashSet<>(Arrays.asList("C", "D"));
        Set<String> set3 = new HashSet<>(Arrays.asList("E", "F"));
        tester.addRecord(set1);
        tester.addRecord(set2);
        tester.addRecord(set3);

        System.out.println(tester.getFirstSet());
        System.out.println(tester.getFirstSet());
        System.out.println(tester.getFirstSet());
        System.out.println(tester.getFirstSet());
    }

    public void addRecord(Set<T> record) {
        setsOfSets.add(record);
    }

    public List<T> getFirstSet() {
        Iterator<Set<T>> iterator = setsOfSets.iterator();
        while (iterator.hasNext()) {
            final Set<T> set = iterator.next();
            iterator.remove();
            return new ArrayList<>(set);
        }
        //if there are no sets present
        return null;
    }
}
Kristaps
  • 141
  • 1
  • 11
  • I think this works for a set of simple Strings like you did, but I'm not entirely sure about the content of the sets (probably it is defined in another file but I can't access to that because I compile with Maven that takes care of many stuff like missing libraries and other cases.. Your code let me understand a little more of sets as I'm trying to learn how to manage them, but doesn't quite solve my problem sadly :\ – GondraKkal Mar 02 '17 at 14:26
  • My example should work with any type defined when creating final Foo tester = new Foo<>(); Can you show me a use case for your class, when you call the method public Set getVarA(). When class instance that will contain your set of sets you define the type. Which should be retrieved it could easily contain some other POJO. – Kristaps Mar 03 '17 at 08:43
0

What your getVarA does is that is iterates through variable_a and returns last item of this iteration as a raw Set.

If you are using java8 you can use streams and their flatMap operation to collect all values of subsets into a single set.

public Set<Type_A> getVarA() {             
    return variable_A.stream().flatMap(Set::stream).collect(Collectors.toSet());
}

Otherwise you can just iterate with foreach and return collected result.

public Set<Type_A> getVarA() {
    Set<Type_A> result = new HashSet<>();
    for (Set<Type_A> subset : variable_A) {
        result.addAll(subset);
    }
    return result;
}
Community
  • 1
  • 1
Januson
  • 4,533
  • 34
  • 42
  • Tried your code but got an error in the 'for' line: ~~~~ `incompatible types: java.util.Set cannot be converted to java.util.Set` ~~~~ and also some other errors about: `no suitable method found for addAll(java.util.Set)` - - - EDIT: same error with both suggestions – GondraKkal Mar 02 '17 at 11:20
  • That is because you have a Set and you are trying to convert it to Set. If you have a Set and you call its addAll method its argument has to be another Set. – Januson Mar 02 '17 at 11:46
  • I edited the foreach example to remore misplaced String type. – Januson Mar 02 '17 at 11:52
  • Ok, I can compile now with maven and launch the webapp I'm modifing with the getter in your code and that's quite good, but when I use getVarA in the jsp file that recalls the javabean I get this: `[SubClassOf( )]`. What to do now? It should be strings for what I know! – GondraKkal Mar 02 '17 at 14:23
  • Can you post the class you are trying to get from sets? – Januson Mar 02 '17 at 15:18
  • No I cannot, that's my problem ^^ Have to find an alternative way to get it, if possible – GondraKkal Mar 02 '17 at 15:45