Module: Java::java::util::List

Defined in:
core/src/main/ruby/jruby/java/java_ext/java.util.rb

Overview

Note:

Only explicit (or customized) Ruby methods are listed here, instances will have all of their Java methods available.

Ruby extensions for java.util.List instances. All of the Java::java::util::Collection methods are available.

See Also:

Instance Method Summary (collapse)

Instance Method Details

- (Java::java::util::List, ...) [](i1, i2 = nil)

Note:

Like with an `Array` will return nil for “out-of-bound” indexes.

Element retrieval (slicing) similar to `Array#[]`.

Examples:

list = java.util.ArrayList.new ['foo', 'bar', 'baz']
list[0] # 'foo'
list[-2] # 'bar'
list[10] # nil
list[1, 2] # ['bar','baz'] sub-list
list[0..2] # ['foo','bar'] sub-list

Returns:



175
176
177
# File 'core/src/main/ruby/jruby/java/java_ext/java.util.rb', line 175

def [](i1, i2 = nil)
  # stub implemented in org.jruby.javasupport.ext.JavaUtil.java
end

- (Object) []=(i, val)

Set an element or multiple elements like `Array#[]=`.

Examples:

list = java.util.ArrayList.new ['foo', 'bar', 'baz']
list[-3] = 1 # [1, 'bar', 'baz']
list[4] = 4 # [1, 'bar', 'baz', nil, 4]
list[1..2] = 3 # [1, nil, 3, 4]

Returns:



186
187
188
# File 'core/src/main/ruby/jruby/java/java_ext/java.util.rb', line 186

def []=(i, val)
  # stub implemented in org.jruby.javasupport.ext.JavaUtil.java
end

- (Object) first(count = nil) Also known as: ruby_first

Note:

Might collide with #getFirst on some list impls in which case you want to alias its ruby_ name so that the Ruby version is used e.g. `java.util.LinkedList.class_eval { alias first ruby_first }`

`Enumerable#first`

Examples:

list = java.util.ArrayList.new [1, 2, 3]
expect( list.first ).to eq 1
expect( list.first(2).to_a ).to eq [1, 2]
list = java.util.LinkedList.new
expect( list.ruby_first ).to be nil

Since:

  • 9.1



258
259
260
# File 'core/src/main/ruby/jruby/java/java_ext/java.util.rb', line 258

def first(count = nil)
  # stub implemented in org.jruby.javasupport.ext.JavaUtil.java
end

- (Integer, ...) index(obj = nil, &block)

Return an index for the first occurrence of the matched element (similar to `Array#index`).

Examples:

list = java.util.LinkedList.new [ 'foo', 'bar', 'foo' ]
list.index 'foo' # 0
list.index 'baz' # nil
list.index { |e| e.start_with?('ba') } # 1

Returns:

  • (Integer, nil, Enumerator)


197
198
199
# File 'core/src/main/ruby/jruby/java/java_ext/java.util.rb', line 197

def index(obj = nil, &block)
  # stub implemented in org.jruby.javasupport.ext.JavaUtil.java
end

- (Object) last(count = nil) Also known as: ruby_last

Note:

Might collide with #getLast on some list impls in which case you want to alias its ruby_ name so that the Ruby version is used e.g. `java.util.LinkedList.class_eval { alias last ruby_last }`

`Array#last`

Examples:

list = java.util.Vector.new [1, 2, 3]
expect( list.last ).to eq 3
expect( list.last(2).to_a ).to eq [2, 3]

Since:

  • 9.1



271
272
273
# File 'core/src/main/ruby/jruby/java/java_ext/java.util.rb', line 271

def last(count = nil)
  # stub implemented in org.jruby.javasupport.ext.JavaUtil.java
end

- (Integer, ...) rindex(obj = nil, &block)

Return an index for the last occurrence of the matched element (similar to `Array#rindex`).

Examples:

list = java.util.LinkedList.new [ 'foo', 'bar', 'foo' ]
list.rindex 'foo' # 2
list.rindex 'baz' # nil
list.rindex { |e| e.start_with?('ba') } # 1

Returns:

  • (Integer, nil, Enumerator)


208
209
210
# File 'core/src/main/ruby/jruby/java/java_ext/java.util.rb', line 208

def rindex(obj = nil, &block)
  # stub implemented in org.jruby.javasupport.ext.JavaUtil.java
end

- (Java::java::util::List) sort(&block) Also known as: ruby_sort

Note:

Since Java 8 this method collides with the built-in *java.util.List#sort* in which case you want to alias its ruby_ name for list types where you want to have the Ruby version available e.g. `java.util.ArrayList.class_eval { alias sort ruby_sort }`

Sort a (mutable) list, returning a new instance of same type.

Examples:

list = java.util.ArrayList.new [ 22, 1, 333 ]
list.sort # [ 1, 22, 333 ] (java.util.ArrayList) ... only works on Java 7 by default!

Returns:



220
221
222
223
224
225
226
# File 'core/src/main/ruby/jruby/java/java_ext/java.util.rb', line 220

def sort(&block)
  # stub implemented in org.jruby.javasupport.ext.JavaUtil.java
  # comparator = block ? RubyComparators::BlockComparator.new(block) : RubyComparators::SpaceshipComparator.new
  # list = self.dup
  # java::util::Collections.sort(list, comparator)
  # list
end

- (Java::java::util::List) sort!(&block)

Sort a (mutable) list, in place.

Examples:

list = java.util.Vector.new [ '22', '1', '333' ]
list.sort! { |a, b| a.length <=> b.length } # [ '1', '22', '333' ]

Returns:



234
235
236
237
238
239
# File 'core/src/main/ruby/jruby/java/java_ext/java.util.rb', line 234

def sort!(&block)
  # stub implemented in org.jruby.javasupport.ext.JavaUtil.java
  # comparator = block ? RubyComparators::BlockComparator.new(block) : RubyComparators::SpaceshipComparator.new
  # java::util::Collections.sort(self, comparator)
  # self
end

- (Array) to_a Also known as: to_ary

Returns:

  • (Array)

See Also:



243
244
245
# File 'core/src/main/ruby/jruby/java/java_ext/java.util.rb', line 243

def to_a
  # stub implemented in org.jruby.javasupport.ext.JavaUtil.java
end