One Java Generics feature to combine three types of loops

I wonder if there is ONE universal JAVA function to MIX these essentially the same 4 for the loop in

Additional arguments:

public static <E> void loopA(E... list) {
    String str="";
    for (E val : list) {
      str+= val.toString();            
    }        
}

Array Type E:

public static <E> void loopB(E[] list) {
    String str="";
    for (E val : list) {
      str+= val.toString();            
    }        
}

Set Collection Type E:

public static <E> void loopC(Set<E> list) {
    String str="";
    for (E val : list) {
      str+= val.toString();            
    }        
}

List collection type E:

public static <E> void loopD(List<E> list) {
    String str="";
    for (E val : list) {
      str+= val.toString();            
    }        
}

ABOVE 4 in ONE of this:

public static <E> void loopSigma(...){
    String str="";
    for (E val : list) {
      str+= val.toString();            
    }        
}
+4
source share
5 answers

If you are using Java 8, you can do it like this:

public static <E> String loop(Collection<E> collection) {
    return collection.stream()
        .map(Object::toString)
        .reduce((a, b) -> a + b)
        .get();
}

Unfortunately, the / vararg array is not Collection, but vararg is an array, so there must be one other method for them:

public static <E> String loop(E... elements) {
    return loop(Arrays.asList(elements));
}
-2
source

I would save the method with the varargs parameter and present the method with the parameter Collection<E>(which will be called from the varargs method).

For instance:

public static <E> void loopA(E... list) {
    loopSigma(Arrays.asList(list)); 
}

public static <E> void loopSigma(Collection<E> collection){
    String str="";
    for (E val : collection) {
      str += val.toString();
    }        
}

, Java8, loopSigma() :

String str = collection.stream().collect(Collectors.joining());
+2

A B , B . C D Collection<E>.

"" , ():

public static <E> void loop(Collection<E> coll) {
   String str=""; //better use a StringBuilder here
   for (E val : coll) {
     str+= val.toString();            
   }        
}   

public static <E> void loop(E... list) {
  loop( Arrays.asList(list) );
}
+1

Java 8, Stream<E> , loopSigma, joining.

public static <E> Stream<E> toStream(E... list) {
    return Arrays.stream(list);
}

public static <E> Stream<E> toStream(Collection<E> list) {
    return list.stream();
}

public String loopSigma(Stream<E> stream) {
    return stream.map(Object::toString).collect(Collectors.joining());
}

Set<E> thingy = ...;
String string = loopSigma(YourStreamUtils.toStream(thingy));

, , .

+1

, :

public static <E> void loopSigma(E... list){
    String str="";
    for (E val : list) {
      str+= val.toString();            
    }        
}

public static <E> void loopSigma(Collection<E> list){
    String str="";
    for (E val : list) {
      str+= val.toString();            
    }        
}

.

But if you have these two methods in some utility. You can call this function and the right choice will be selected based on the type you provide. Thus, it will be transparent to you, which is used in terms of use.

0
source

All Articles