SHARE
TWEET

zip and map arrays

DulcetAirman Jun 21st, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ch.claude_martin;
  2.  
  3. import java.util.stream.IntStream;
  4.  
  5. public final class SomeClass {
  6.   private static final String FORMAT = "%s, %d, %s%n";
  7.  
  8.   static final class Student {
  9.     private final String name, gender;
  10.     private final int age; // note that you should store the birth year instead
  11.  
  12.     public Student(String name, int age, String gender) {
  13.       this.name = name;
  14.       this.age = age;
  15.       this.gender = gender;
  16.     }
  17.  
  18.     public int getAge() {
  19.       return age;
  20.     }
  21.  
  22.     public String getName() {
  23.       return name;
  24.     }
  25.  
  26.     public String getGender() {
  27.       return gender;
  28.     }
  29.  
  30.     @Override
  31.     public String toString() {
  32.       return String.format(FORMAT, name, age, gender);
  33.     }
  34.   }
  35.  
  36.   public static void main(String[] args) {
  37.     String[] names = { "Alice", "Bob", "Oscar" };
  38.     int[] ages = { 35, 65, 23 };
  39.     String[] gender = { "Cis Female", "Cis Male", "Other" };
  40.  
  41.     assert names.length == ages.length && names.length == gender.length;
  42.  
  43.     IntStream.range(0, names.length).mapToObj(i -> new Student(names[i], ages[i], gender[i]))
  44.         .forEach(System.out::print);
  45.  
  46.   }
  47.  
  48. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top