2017-10-04 6 views
-1

Supposons que j'ai une liste de liste comme;Utilisation de Java 8 Stream et Lambda Je souhaite créer une liste spécifique à partir d'une liste de listes. Détail est donné ci-dessous

{ 
    {1-a12-abc,firstname,john}, 
    {2-a12-abc,firstname,tom}, 
    {1-a12-abc,lastname,doe}, 
    {3-a22-abc,city,Delhi} 
} 

On peut penser chaque liste est un peu PersonId contenant, Attribute et attribute value. Je veux avec chaque Id, la liste des attributs et la liste des valeurs correspondantes ensemble.

Utilisation de Java 8 API Stream Je veux une sortie comme ci-dessous:

{ 
     {{1-a12-abc},{firstname,lastname},{john,doe}}, 
     {{2-a12-abc},{firstname},{tom}}, 
     {{3-a22-abc},{city},{Delhi}} 
    } 

où le regroupement sera avec le premier élément de chaque liste comme indiqué ci-dessus. Les deuxième et troisième éléments pour chaque groupe formeront la liste.

S'il vous plaît aider avec la solution.

+0

Regardez 'Stream.map()', 'flux .collect() ',' Collecteurs.groupingBy() '. Lorsque vous êtes coincé, vous pouvez revenir et demander plus de détails. Mais ne vous attendez pas à ce que quelqu'un vous donne une solution complète sans montrer votre propre effort. –

Répondre

1

Ici, il est une version en utilisant Java 8 flux:

// Initial list 
public class AttributeValue { 
    String id; 
    String attribute; 
    String attributeValue; 

    AttributeValue(String id, String attribute, String attributeValue) { 
     this.id = id; 
     this.attribute = attribute; 
     this.attributeValue = attributeValue; 
    } 

    @Override 
    public String toString() { 
     return "{" + id + "," + attribute + "," + attributeValue + "}"; 
    } 

    // ommiting equals() and hashCodes() methods for simplification 
} 

// final list 
public class Record { 
    String id; 
    List<String> attributes; 
    List<String> attributeValues; 

    Record(String id, List<String> attributes, List<String> attributeValues) { 
     this.id = id; 
     this.attributes = attributes; 
     this.attributeValues = attributeValues; 
    } 

    @Override 
    public String toString() { 
     return "{{" + id + "}," 
       + attributes.stream().collect(Collectors.joining(",", "{", "}")) + "," 
       + attributeValues.stream().collect(Collectors.joining(",", "{", "}")) + "}"; 
    } 

    // ommiting equals() and hashCodes() methods for simplification 
} 

private List<Record> transform(List<AttributeValue> values) { 
    return values.stream() 
      .collect(Collectors.groupingBy(value -> value.id, LinkedHashMap::new, Collectors.toList())) 
      .entrySet().stream() 
      .map(pair -> new Record(
       pair.getKey(), 
       pair.getValue().stream().map(value -> value.attribute).collect(Collectors.toList()), 
       pair.getValue().stream().map(value -> value.attributeValue).collect(Collectors.toList()) 
      )) 
      .collect(Collectors.toList()); 
} 

Et les tests unitaires à l'aide junit5 et assertj pour la maîtrise:

@Test 
void parse_a_van_page_with_empty_json_data() throws Exception { 
    List<AttributeValue> initial = Arrays.asList(
      new AttributeValue("1-a12-abc", "firstname", "john"), 
      new AttributeValue("2-a12-abc", "firstname", "tom"), 
      new AttributeValue("1-a12-abc", "lastname", "doe"), 
      new AttributeValue("3-a22-abc", "city", "Delhi") 
    ); 

    List<Record> expected = Arrays.asList(
      new Record("1-a12-abc", Arrays.asList("firstname", "lastname"), Arrays.asList("john", "doe")), 
      new Record("2-a12-abc", Collections.singletonList("firstname"), Collections.singletonList("tom")), 
      new Record("3-a22-abc", Collections.singletonList("city"), Collections.singletonList("Delhi")) 
    ); 

    assertThat(transform(initial)).containsAll(expected); 

    // Some printing 
    System.out.println("initial: "); 
    initial.forEach(System.out::println); 

    System.out.println("expected: "); 
    expected.forEach(System.out::println); 
}