Home » Java Advanced » Guava

Guava

Schnelles Initialisieren von Listen, Sets und Maps

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

public class LerneGuava1 {
    @Test
    public void liste(){
        //List
        List<String> guavaList1 = ImmutableList.of("Walther", "Hans", "Erich", "Paul");
        List<String> guavaList2 = Lists.newArrayList("Walther", "Hans", "Erich", "Paul");

        List<String> javaList1 = Arrays.asList("Walther", "Hans", "Erich", "Paul");
        
        //Sets:
        Set<String> guavaSet1 = ImmutableSet.of("Walther", "Hans", "Erich", "Paul");
        Set<String> guavaSet2 = Sets.newHashSet("Walther", "Hans", "Erich", "Paul");
        
        Set<String> javaSet = new HashSet<String>(Arrays.asList("Walther", "Hans", "Erich", "Paul"));

        //Maps:
        Map<String, Integer> nameWithAge = ImmutableMap.of("Hans", 62, "Walter", 61, "Erich", 64);
        assertThat(nameWithAge.get("Hans").equals(62));
    }
}

Hinterlasse einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert