Class Pair<T1,T2>
java.lang.Object
com.frostwire.jlibtorrent.Pair<T1,T2>
- Type Parameters:
T1- the type of the first elementT2- the type of the second element
Generic pair container for holding two related values of different types.
Pair mirrors the C++ std::pair template, providing a simple way to
combine two heterogeneous values into a single object. This is commonly used in
jlibtorrent for returning multiple values from methods and storing key-value pairs.
Understanding Pair Usage:
Pairs are used throughout jlibtorrent to represent:
- Key-Value Pairs: Configuration settings, tracker/peer info
- Coordinate Pairs: Range boundaries like (start, end)
- Status Tuples: Combined result and status information
- Native Interop: Mapping between Java and C++ pair types
Creating Pairs:
// String-String pair (e.g., name-value configuration) Pair<String, String> setting = new Pair<>(\"key\", \"value\"); System.out.println(\"Key: \" + setting.first); System.out.println(\"Value: \" + setting.second); // String-Integer pair (e.g., name-port) Pair<String, Integer> endpoint = new Pair<>(\"example.com\", 6881); System.out.println(\"Host: \" + endpoint.first); System.out.println(\"Port: \" + endpoint.second); // Generic types Pair<Integer, Long> range = new Pair<>(0, 1024L); System.out.println(\"Start: \" + range.first); System.out.println(\"End: \" + range.second);
Using Pairs in Collections:
// Store multiple tracker/peer pairs
java.util.List<Pair<String, Integer>> nodes =
new java.util.ArrayList<>();
nodes.add(new Pair<>(\"router.bittorrent.com\", 6881));
nodes.add(new Pair<>(\"router.utorrent.com\", 6881));
nodes.add(new Pair<>(\"dht.transmissionbt.com\", 6881));
for (Pair<String, Integer> node : nodes) {
System.out.println(\"Node: \" + node.first + \":\" + node.second);
}
Native Conversion Methods:
Pair provides methods to convert between Java and native C++ pair types:
// Convert String-String pair to native type
Pair<String, String> javaPair = new Pair<>(\"key\", \"value\");
com.frostwire.jlibtorrent.swig.string_string_pair nativePair =
javaPair.to_string_string_pair();
// Convert String-Integer pair to native type
Pair<String, Integer> config = new Pair<>(\"port\", 6881);
com.frostwire.jlibtorrent.swig.string_int_pair nativeConfig =
config.to_string_int_pair();
Common Use Cases in BitTorrent:
// DHT nodes (hostname:port pairs)
Pair<String, Integer> dhtNode = new Pair<>(\"router.bittorrent.com\", 6881);
// Trackers (url:tier pairs)
Pair<String, Integer> tracker = new Pair<>(
\"http://tracker.example.com/announce\",
0 // tier 0 = primary
);
// Web seeds (url:priority pairs)
Pair<String, Integer> webSeed = new Pair<>(
\"http://example.com/torrent\",
1 // priority
);
Important Notes:
- Pairs are immutable after creation (fields are final)
- Both type parameters must be specified for type safety
- Native conversion methods only support specific combinations (String-String, String-Integer)
- Use diamond operator (
<>) for cleaner syntax in Java 7+
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
-
Field Details
-
first
the first element -
second
the second element
-
-
Constructor Details
-
Pair
-