Det Java.util.List er en børnegrænseflade af Kollektion . Det er en ordnet samling af objekter, hvor duplikerede værdier kan gemmes. Da List bevarer indsættelsesrækkefølgen, tillader den positionsadgang og indsættelse af elementer. List Interface er implementeret af ArrayList , LinkedList , Vektor og Stak klasser.
List er en grænseflade, og forekomsterne af List kan oprettes på følgende måder:
List a = new ArrayList(); List b = new LinkedList(); List c = new Vector(); List d = new Stack();>
Nedenfor er følgende måder at initialisere en liste på:
-
Brug af List.add() metoden
Da listen er en grænseflade, kan man ikke direkte instansiere den. Imidlertid kan man oprette objekter af de klasser, der har implementeret denne grænseflade, og instansiere dem.
Få klasser, der har implementeret List-grænsefladen, er Stack, ArrayList, LinkedList, Vector etc.
Syntaks:
List list=new ArrayList(); List llist=new LinkedList(); List stack=new Stack();>
Eksempler:
import>
java.util.*;>
import>
java.util.function.Supplier;>
>
public>
class>
GFG {>
>
public>
static>
void>
main(String args[])>
>
{>
>
>
// For ArrayList>
>
List list =>
new>
ArrayList();>
>
list.add(>
1>
);>
>
list.add(>
3>
);>
>
System.out.println(>
'ArrayList : '>
+ list.toString());>
>
>
// For LinkedList>
>
List llist =>
new>
LinkedList();>
>
llist.add(>
2>
);>
>
llist.add(>
4>
);>
>
System.out.println(>
'LinkedList : '>
+ llist.toString());>
>
>
// For Stack>
>
List stack =>
new>
Stack();>
>
stack.add(>
3>
);>
>
stack.add(>
1>
);>
>
System.out.println(>
'Stack : '>
+ stack.toString());>
>
}>
}>
>
>
Produktion:ArrayList : [1, 3] LinkedList : [2, 4] Stack : [3, 1]>
Double Brace Initialisering kan også bruges til at udføre ovenstående arbejde.
Syntaks:
List list=new ArrayList(){{ add(1); add(2); add(3); }};>
Eksempler:
import>
java.util.*;>
>
public>
class>
GFG {>
>
public>
static>
void>
main(String args[])>
>
{>
>
>
// For ArrayList>
>
List list =>
new>
ArrayList() {{>
>
add(>
1>
);>
>
add(>
3>
);>
>
} };>
>
System.out.println(>
'ArrayList : '>
+ list.toString());>
>
>
// For LinkedList>
>
List llist =>
new>
LinkedList() {{>
>
add(>
2>
);>
>
add(>
4>
);>
>
} };>
>
System.out.println(>
'LinkedList : '>
+ llist.toString());>
>
>
// For Stack>
>
List stack =>
new>
Stack() {{>
>
add(>
3>
);>
>
add(>
1>
);>
>
} };>
>
System.out.println(>
'Stack : '>
+ stack.toString());>
>
}>
}>
>
>
Produktion:ArrayList : [1, 3] LinkedList : [2, 4] Stack : [3, 1]>
-
Brug af Arrays.asList()
- Oprettelse af uforanderlig liste
Arrays.asList() opretter en uforanderlig liste fra et array. Derfor kan den bruges til at instansiere en liste med et array.
Syntaks:
List list=Arrays.asList(1, 2, 3);>
Eksempler:
import>
java.util.Arrays;>
import>
java.util.List;>
>
public>
class>
GFG {>
>
public>
static>
void>
main(String args[])>
>
{>
>
>
// Instantiating List using Arrays.asList()>
>
List list = Arrays.asList(>
1>
,>
2>
,>
3>
);>
>
>
// Print the list>
>
System.out.println(>
'List : '>
+ list.toString());>
>
}>
}>
>
>
Produktion:List : [1, 2, 3]>
- Oprettelse af foranderlig liste
Syntaks:
List list=new ArrayList(Arrays.asList(1, 2, 3));>
Eksempler:
import>
java.util.ArrayList;>
import>
java.util.Arrays;>
import>
java.util.List;>
>
public>
class>
GFG {>
>
public>
static>
void>
main(String args[])>
>
{>
>
>
// Creating a mutable list using Arrays.asList()>
>
List list =>
new>
ArrayList(>
>
Arrays.asList(>
1>
,>
2>
,>
3>
));>
>
>
// Print the list>
>
System.out.println(>
'List : '>
+ list.toString());>
>
>
list.add(>
5>
);>
>
>
// Print the list>
>
System.out.println(>
'Modified list : '>
+ list.toString());>
>
}>
}>
latex skriftstørrelser
>
>
Produktion:List : [1, 2, 3] Modified list : [1, 2, 3, 5]>
- Oprettelse af uforanderlig liste
-
Brug af Collections-klassemetoder
Der er forskellige metoder i klassen Samlinger, der kan bruges til at instansiere en liste. De er:
-
Brug af Collections.addAll()
Samlinger klasse har en statisk metode addAll() som kan bruges til at initialisere en liste. Collections.addAll() tage et vilkårligt antal elementer ind, efter at det er angivet med den samling, som elementerne skal indsættes i.
Syntaks:
List list = Collections.EMPTY_LIST; Collections.addAll(list = new ArrayList(), 1, 2, 3, 4);>
Eksempler:
import>
java.util.*;>
>
public>
class>
GFG {>
>
public>
static>
void>
main(String args[])>
>
{>
>
>
// Create an empty list>
>
List list =>
new>
ArrayList();>
>
>
// Instantiating list using Collections.addAll()>
>
Collections.addAll(list,>
1>
,>
2>
,>
3>
,>
4>
);>
>
>
// Print the list>
>
System.out.println(>
'List : '>
+ list.toString());>
>
}>
}>
>
>
Produktion:List : [1, 2, 3, 4]>
-
Brug af Collections.unmodifiableList()
Collections.unmodifiableList() returnerer en liste, som ikke kan ændres, dvs. den kan hverken tilføje eller slette et element. Ethvert forsøg på at ændre listen vil resultere i et UnsupportedOperationExample.
Syntaks:
List list = Collections .unmodifiableList(Arrays.asList(1, 2, 3));>
Eksempel 1:
import>
java.util.*;>
>
public>
class>
GFG {>
>
public>
static>
void>
main(String args[])>
>
{>
>
>
// Creating the list>
>
List list = Collections.unmodifiableList(>
>
Arrays.asList(>
1>
,>
2>
,>
3>
));>
>
>
// Print the list>
>
System.out.println(>
'List : '>
+ list.toString());>
>
}>
}>
>
>
Produktion:List : [1, 2, 3]>
Eksempel 2:
import>
java.util.*;>
>
public>
class>
GFG {>
>
public>
static>
void>
main(String args[])>
>
{>
>
>
try>
{>
>
// Creating the list>
>
List list = Collections.unmodifiableList(>
>
Arrays.asList(>
1>
,>
2>
,>
3>
));>
>
>
// Print the list>
>
System.out.println(>
'List : '>
+ list.toString());>
>
>
// Trying to modify the list>
>
System.out.println(>
'Trying to modify the list'>
);>
>
list.set(>
0>
, list.get(>
0>
));>
>
}>
>
>
catch>
(Exception e) {>
>
System.out.println(>
'Exception : '>
+ e);>
>
}>
>
}>
}>
>
>
Produktion:List : [1, 2, 3] Trying to modify the list Exception : java.lang.UnsupportedOperationException>
-
Brug af Collections.singletonList()
Collections.singletonList() returnerer en uforanderlig liste, der kun består af ét element.
Syntaks:
List list = Collections.singletonList(2);>
Eksempel 1:
import>
java.util.*;>
>
public>
class>
GFG {>
>
public>
static>
void>
main(String args[])>
>
{>
>
>
// Creating the list>
>
List list = Collections.singletonList(>
2>
);>
>
>
// Print the list>
>
System.out.println(>
'List : '>
+ list.toString());>
>
}>
}>
>
>
Produktion:List : [2]>
-
-
Brug af Java 8 Stream
Med introduktionen af Stream og funktionel programmering i Java 8 kan man nu konstruere enhver strøm af objekter og derefter samle dem som en liste.
Syntaks:
1. List list = Stream.of(1, 2, 3) .collect(Collectors.toList()); 2. List list = Stream.of(1, 2, 3) .collect(Collectors.toCollection(ArrayList::new)); 3. List list = Stream.of(1, 2, 3, 4) .collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));>
Eksempler:
import>
java.util.*;>
import>
java.util.stream.Collectors;>
import>
java.util.stream.Stream;>
>
public>
class>
GFG {>
>
public>
static>
void>
main(String args[])>
>
{>
>
>
// Creating a List using Syntax 1>
>
List list1 = Stream.of(>
1>
,>
2>
,>
3>
)>
>
.collect(Collectors.toList());>
>
>
// Printing the list>
>
System.out.println(>
'List using Syntax 1: '>
>
+ list1.toString());>
>
>
// Creating a List using Syntax 2>
>
List list2 = Stream>
>
.of(>
3>
,>
2>
,>
1>
)>
>
.collect(>
>
Collectors>
>
.toCollection(ArrayList::>
new>
));>
>
>
// Printing the list>
>
System.out.println(>
'List using Syntax 2: '>
>
+ list2.toString());>
>
>
// Creating a List using Syntax 3>
>
List list3 = Stream>
>
.of(>
1>
,>
2>
,>
3>
,>
4>
)>
>
.collect(>
>
Collectors>
>
.collectingAndThen(>
>
Collectors.toList(),>
>
Collections::unmodifiableList));>
>
>
// Printing the list>
>
System.out.println(>
'List using Syntax 3: '>
>
+ list3.toString());>
>
}>
}>
>
>
Produktion:List using Syntax 1: [1, 2, 3] List using Syntax 2: [3, 2, 1] List using Syntax 3: [1, 2, 3, 4]>
-
Brug af Java 9 List.of()
Java 9 introducerede List.of()-metoden, som tager et vilkårligt antal argumenter ind og konstruerer en kompakt og ikke-modificerbar liste ud af dem.
Syntaks:
List unmodifiableList = List.of(1, 2, 3);>
Eksempler:
import>
java.util.List;>
>
public>
class>
GFG {>
>
public>
static>
void>
main(String args[])>
>
{>
>
>
// Creating a list using List.of()>
>
List unmodifiableList = List.of(>
1>
,>
2>
,>
3>
);>
>
>
// Printing the List>
>
System.out.println(>
'List : '>
>
+ unmodifiableList.toString());>
>
}>
}>
>
>
PRODUKTION:
[1, 2, 3]>