I am having my final exam tomorrow so i am practicing some questions.But i am stuck at this question.I am given a person class file and a half completed quiz10 file whereby i have to fill up.The quiz10 codes are halfway done(given).
I need to implement a function findPersonWhoseNameStartWith which returns the names of the persons in the list who start with A. But i have no idea how.
Output wanted:
result:April,Adam
public class Person{
private int age;
private String name;
public Person(String name,int age){
this.name=name;
this.age=age;
}
public int getAge(){
return age;
}
public String getName(){
return name;
}
public String toString(){
return "" + name;
}
}
Half given codes(I have indicate which part i have attempted):
import java.util.*;
public class Quiz10{
public static void main(String[] args){
ArrayList<Person>list=new ArrayList<Person>();
list.add(new Person("April",9));
list.add(new Person("Adam",3));
list.add(new Person("bil",9));
list.add(new Person("cpril",9));
list.add(new Person("dpril",9));
ArrayList<Person>result=findPersonWhoseNameStartWith(list,"A");
System.out.println("result:");
//START DOING FROM HERE
for(int i=0;i<list.size();i++){
Person p=list.get(i);
if(p.findPersonWhoseNameStartWith("A");
}
}
You are on the right track. you are right you have to iterate over the list. Now for each entry output it if it starts with 'A'. It's very simple and a single if statement way easier than what you imagine it seems.
// pass your personList and the prefix, return a list of person starting with the prefix you specified
private List<Person> findPersonWhoseNameStartWith(List<Person> personList, String prefix) {
// create a list to store your result
List<Person> matchedList = new ArrayList<Person>();
// TODO iterate personList
// add them to the matchedList if the prefix matches
return matchedList;
}
public List<Person> findAPersonWhoStartsWith(List<Person> persons, String aLetter){
List<String> personsNames = new ArrayList<String>();
if(persons!=null && aLetter!=null && !aLetter.isEmpty()){
for(Person aPerson:persons){
if(aPerson.getName().startsWith(aLetter)){
personsNames.add(aPerson);
}
}
}
return personsNames;
}
Related
I made this Student ArrayList that have name, age and number variable. I manually input 3 student. I want to check if a name already exist in my list. I tried to use list.contains but it seems not working. Are there certain way to do for this kind of ArrayList?
public class CompareToeX {
public static void main(String[] args) {
ArrayList<Student> obj=new ArrayList<Student>();
obj.add(new Student("Peter", 27,1));
obj.add(new Student("John",26,7));
obj.add(new Student("Jack",21,5));
if(obj.contains("Peter")){
System.out.println("Peter on is on the list!");
} else {
System.out.println("Peter is not on the list!");
}
}
}
This is the Student class
public class Student{
private String studentName;
private int age;
private int rollno;
public Student(String studentName, int age, int rollno){
this.studentName=studentName;
this.age=age;
this.rollno=rollno;
}
public String getStudent(){
return studentName;
}
public int getAge(){
return age;
}
public int getRollno(){
return rollno;
}
public void setStudent(String Student){
studentName=Student;
}
public void setAge(int age){
this.age=age;
}
public void setRollno(int rollno){
this.rollno=rollno;
}
}
After I run, the result should show that peter is on the list right. But it goes the other way around. It seems that the contains method is not working.
A student isn't a name.
.contains() will fail here because the string "Peter" is not in the list. It obviously isn't; how could it be? This list only contains Student objects, not Strings, after all.
You'll have to write a for loop, or use streams.
boolean peterIsInTheList = false;
for (var s : students) {
if (s.getName().equals("peter")) peterIsInTheList = true;
}
or
boolean peterIsInTheList = students.stream()
.anyMatch(s -> s.getName().equals("peter"));
Alternatively you could use the idea of a map; change your data structure. Instead of using a list, have a map that maps first names onto student objects:
Map<String, Student> students = new HashMap<>();
students.put("peter", new Student(....));
if (students.containsKey("peter")) {
// peter is in the list
}
You are checking if the String Peter is in the list, or the list only contains Student object. So you can only check if Student is contained inside the list (be sure to implement the right equals/hashcode method if you wish to use .contains).
This will do
obj.stream()
.filter(student -> student.getStudent().contains("Peter"))
.findFirst()
.ifPresentOrElse(student -> System.out.println("Peter on is on the list!"), () -> System.out.println("Peter is not on the list!"));
Being not a default data type, I think you need to override some functions of ArrayList first, like the .compare() one.
It was answered here too
Java: to use contains in a ArrayList full of custom object should I override equals or implement Comparable/Comparator?
I have to code LinkedList with two positions. First is the name and the second is the year.
class Person
{
String name;
int year;
public Person(String name, int year) {
this.name = name;
this.year = year;
}
}
class LinkedListStart {
public static void main (String[] args) throws java.lang.Exception
{
LinkedList<Person> persons = new LinkedList<Person>();
persons.add(new Person("Joe", 2010));
persons.add(new Person("Jane", 2012));
persons.add(new Person("Charly", 1910));
persons.add(new Person("Daisy", 1908));
System.out.println("LinkedList : " + persons);
}
}
There are errors when I am running the code:
LinkedList : [Person#6d06d69c, Person#7852e922, Person#4e25154f, Person#70dea4e]
Is it possible to do it? I want do use for-each loop to find person who is below 1950.
How to code something like this?
You should define a class Person as follows:
class Person{
String name;
int year;
public Person(String name, int year) {
this.name = name;
this.year = year;
}
}
And then create a LinkedList as follows:
LinkedList<Person> persons = new LinkedList<>();
You could combine them both into one String like names.add("Joe 2010") (NOT a recommended, or particularly good approach) or create a Person class to include both of these attributes, but you can't add two elements in the space of one, like you are attempting to do with your code above.
If you go with the Person class route, iterate through each element, and check if Person.year or whatever you choose to call the attribute is less than 1950.
If you go with the String route, iterate through each, split the String on a space to create an array, where the first element is the name and the second is the year, parse an int out of the year String using Integer.parseInt and then check if the year is less than 1950. As #Michael mentioned, combining two strings like this is not recommended
Edit: As for your new problem, Java's LinkedList<T> class is known as a generic class, because it takes a type parameter, such as the Person part in LinkedList<Person>. Because your class is not defined as a generic class, and has the same name as the built-in LinkedList, you are receiving this error when you try to pass in a type parameter, as Java thinks you are trying to use your own LinkedList class. In general, even if you are not using a particular built-in class in your code, it's best not to name your own classes with the same name as built-ins, so collisions like this don't happen. Simply rename your own class, and your code will work
Yes you can create a custom list here is the code,
create a class name it what ever you want, i named it HomeDataModel
public class HomeDataModel {
private String name;
private int year;
public HomeDataModel() {
}
public HomeDataModel(String name,int year) {
name=name;
year= year;
}
public String getName() {
return name;
}
public void setName(String name) {
name= name;
}
public String getYear() {
return year;
}
public void setYear(int year) {
year= year;
}
}
now in your activity where you want to use list,,
//creating a custom list
List<HomeDataModel> listname = new ArrayList<>();
list.add(new HomeDataModel("yourname", "Youryear"));
//code for check how is below 1950
for(int i=0; i<listname.size(); i++){
int check = list.get(i).getYear();
if(check <1950){
String newname = list.get(i).getName(); //the name is stored
Toast.makeText(MapsActivity.this,newname, Toast.LENGTH_SHORT).show();
}
}
There are errors when I am running the code: LinkedList : [Person#6d06d69c, Person#7852e922, Person#4e25154f, Person#70dea4e]
Every class in Java has the toString() method in it by default, which is called if you pass some object of that class to System.out.println(). By default, this call returns the className#hashcode of that object.
Here you have not overridden the toString method that's why it is printing like that by default.Please us below code:
public class Test {
static class Person
{
String name;
int year;
public Person(String name, int year) {
this.name = name;
this.year = year;
}
#Override
public String toString() {
return "Person [name=" + name + ", year=" + year + "]";
}
}
public static void main (String[] args) throws java.lang.Exception
{
LinkedList<Person> persons = new LinkedList<Person>();
persons.add(new Person("Joe", 2010));
persons.add(new Person("Jane", 2012));
persons.add(new Person("Charly", 1910));
persons.add(new Person("Daisy", 1908));
for (Person person : persons) {
System.out.println("Name is::"+person.name+" year is:::"+person.year);
}
//for printing the person override tostring method
System.out.println(persons);
}
}
As the tittle says i want to save each new created object of Person's name in a list:
This is my code so far
package javaapplication4;
import java.util.*;
public class Person {
private String namePerson;
static List personList = new ArrayList();
{
personList.add(getPersonName());
}
public Person(String namePerson){
this.namePerson = namePerson;
}
public void setPersonName(String namePerson){
this.namePerson = namePerson;
}
public String getPersonName(){
return namePerson;
}
public void setPersonList(List personList){
this.personList= personList;
}
public static List getPersonList(){
return personList;
}
each time i am creating a person object its gets added as a 'null' spot in the list (when i use println).
how i change that to the name of the new object Person
like
Person Guy = new Person("NameOfGuy"); then list must be [NameOfGuy].
{
personList.add(getPersonName());
}
The above is called an instance initializer. It is executed before the constructor is executed. At that time, getPersonName will return null as you haven't yet set the value of namePerson.
Move that inside the constructor
public Person(String namePerson){
this.namePerson = namePerson;
this.personList.add(namePerson);
}
Sidenote: It is a bad practice to use raw types. You are using a raw List. It must be as
List<String> personList = new ArrayList<>();
What is a raw type and why shouldn't we use it?
As pointed out by #user7, you are adding the name into the list at the wrong place. What you should be doing is, adding person's name into list while you are creating person's object, i.e. inside your constructor. Replace your constructor with this :
public Person(String namePerson){
this.namePerson = namePerson;
personList.add(namePerson);
}
You can do the job Doing below changes to the Person class:
import java.util.*;
public class Person {
private String namePerson;
static List<String> personList = new ArrayList<>();
public Person(String namePerson) {
this.namePerson = namePerson;
personList.add(this.namePerson);
}
public void setPersonName(String namePerson) {
this.namePerson = namePerson;
}
public String getPersonName() {
return namePerson;
}
public void setPersonList(List personList) {
this.personList = personList;
}
public static List getPersonList() {
return personList;
}
}
Immutable Class with List
package com.text.immutable;
import java.util.Collections;
import java.util.List;
// An immutable class Student
public final class Student
{
final String name;
final int regNo;
final List<String> courses; // want to make Immutable
public Student(String name, int regNo, List<String> courses)
{
this.name = name;
this.regNo = regNo;
this.courses = Collections.unmodifiableList(courses);
}
public String getName()
{
return name;
}
public int getRegNo()
{
return regNo;
}
public List<String> getCourses() {
return courses;
}
}
Testing Immutable Class to Break Immutability
package com.text.immutable;
import java.util.ArrayList;
import java.util.List;
class ImmutablityTest
{
public static void main(String args[])
{
List<String> courses = new ArrayList<String>();
courses.add("java");
courses.add("spring");
courses.add("hibernate");
courses.add("rest");
Student s = new Student("ABC", 101, courses);
System.out.println("Before Update List");
System.out.println(s.getName());
System.out.println(s.getRegNo());
System.out.println(s.getCourses());
courses.add("Hibernate"); // Able to Change which affect final OutCome
//s.getCourses().add("SpringBoot"); // giving Exception in thread "main" java.lang.UnsupportedOperationException
System.out.println("After Update List");
System.out.println(s.getName());
System.out.println(s.getRegNo());
System.out.println(s.getCourses());
}
}
Output is
Before Update List
ABC
101
[java, spring, hibernate, rest]
After Update List
ABC
101
[java, spring, hibernate, rest, Hibernate]
why and how this new Course element added into the List as its from Client Side can be added up any time so how we can fix this issue as this immutable class should not allow to modifying after once created
this.courses = Collections.unmodifiableList(courses);
That creates, as the name says, an unmodifiable list. But that is just a view on the original list. Thus changes to that original list become visible in your "unmodifiable" view.
When in doubt: clone your list, like:
this.courses = new ArrayList<>(courses);
And then ensure that your getter does:
return Collections.unmodifiableList(courses);
Not the best in context of memory, but works:
// An immutable class Student
public final class Student
{
final String name;
final int regNo;
final List<String> courses; // want to make Immutable
public Student(String name, int regNo, List<String> courses)
{
this.name = name;
this.regNo = regNo;
this.courses = new ArrayList(courses);
}
public String getName()
{
return name;
}
public int getRegNo()
{
return regNo;
}
public List<String> getCourses() {
return new ArrayList(courses);
}
}
On input (in constructor) you create copy of list and on output (in getter) you create copy.
read about immutableLists and you'll find that an Immutable and Unmodifiable Are Not the Same.
I guess (from your question) you are expecting an unmodifiable list which you simply don't create...
see this answer for a proper solution
With Collections.unmodifiableList, it creates a wrapper around the original list and that wrapper object is unmodifiable. The original list can still be updated.
So, in order for the List<String> courses list to be immutable, you can use Apache collection common library.
List<String> immutableList =
com.google.common.collect.ImmutableList.of("Geeks", "For","Geeks");
ImmutableList has overridden the List.add method to always throw exception java.lang.UnsupportedOperationException
Second alternative is to create the list inside the constructor itself.
public Student(String name, int regNo, String... args)
{
this.name = name;
this.regNo = regNo;
courses = (List)Arrays.asList(args);
}
And call it like this :
Student s = new Student("ABC", 101, "a","a","a","a");
This question already has answers here:
How to sort List of objects by some property
(17 answers)
Closed 3 years ago.
In this program I sorted strings in Alphabetic way in Simple Array but how can I do the same thing using List or ArrayList, Suppose I have a class Students and I want to order names in an alphabetically way, so how can I do that?
public class CityData{
public static String citynames[]= {"Hyderabad","Karachi","Abtabad","AzadKashmir"};
public static void main(String args[]) {
int size=citynames.length;
String temp=null;
for(int i=0; i<size; i++) {
for(int j=i+1; j<size; j++) {
if(citynames[i].compareTo(citynames[j])>0) {
temp=citynames[i];
citynames[i]=citynames[j];
citynames[j]=temp; }
}
System.out.println(citynames[i]);
}
}
}
Result:
Abtabad
AzadKashmir
Hyderabad
Karachi
You can sort the collections based on your requirements.
If the input objects for collections is implementing the Comparable interface like String, Integer, Double classes, then you can directly use Collections.sort() method from Collections util class
List<String> al = new ArrayList<>();
al.add("Hyderabad");
al.add("Karachi");
al.add("Abtabad");
al.add("AzadKashmir");
al.add("Udupi");
al.add("Jammu");
Collections.sort(al);
Or you can sort the list based on your requirement.(Reverse alphabetical Order)
Collections.sort(al, (str1, str2) -> str2.compareTo(str1));
If you don't want to use the Collections class, then directly use the sort() method present in List interface.
i. Albhabetical Order
al.sort((str1, str2) -> str1.compareTo(str1));
ii. Reverse Albhabetical Order
al.sort((str1, str2) -> str2.compareTo(str1));
The above solution is for the Objects where the class implements the Comparable Interface like String, Integer, Double, etc...
When to sort the Custom Classes, you need to implement sort by Comparator class or Lambda expression for the Comparator.
Consider you have a Student Class, and need to sort by city names. You can use below code.
Student Class
public class Student {
private String name;
private String city;
public Student() {}
public Student(String name, String city) {
this.name = name;
this.city = city;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
#Override
public String toString() {
return "Student -> [name=" + name + ", city=" + city + "]";
}
}
Sorting By Collection Class sort() method
Student st1 = new Student("Samir", "Hyderabad");
Student st2 = new Student("Akbar", "Karachi");
Student st3 = new Student("Ramu", "Abtabad");
Student st4 = new Student("Rahim", "AzadKashmir");
Student st5 = new Student("Sardar", "Udupi");
Student st6 = new Student("Fahad khan", "Jammu");
List<Student> al2 = new ArrayList<>();
al2.add(st1);
al2.add(st2);
al2.add(st3);
al2.add(st4);
al2.add(st5);
al2.add(st6);
//Alphabetical Order
Collections.sort(al2, (std1, std2) -> std1.getCity().compareTo(std2.getCity()));
//Reverse Alphabetical Order
Collections.sort(al2, (std1, std2) -> std2.getCity().compareTo(std1.getCity()));
By using List.sort() method
//Alphabetical Order
al2.sort((std1, std2) -> std1.getCity().compareTo(std2.getCity()));
//Reverse Alphabetical Order
al2.sort((std1, std2) -> std1.getCity().compareTo(std2.getCity()));
What you are looking for is actually pretty simple to do:
Instead of using citynames[i] or citynames[j], for Lists and ArrayLists, you use citynames.get(i) or citynames.get(j).
Just think of List.get() is the same as the brackets you put before for the simple arrays.
Just remember, when you want to set a value, you actually have to use the List.set(int index, Object value).
The same can be said if you are trying to get the length of the List or ArrayList. You can simply replace citynames.length to citynames.size();
public static void main(String args[]) {
int size=citynames.size();
String temp=null;
for(int i=0; i<size; i++) {
for(int j=i+1; j<size; j++) {
if(citynames.get(i).compareTo(citynames.get(j))>0) {
temp=citynames.get(i);
citynames.set(i, citynames.get(j));
citynames.set(j, temp); }
}
System.out.println(citynames.get(i));
}
}
Update: Better Solution:
Note: When using Collections.sort() it is important to make sure the object type of the array implements Comparable. the Collections.sort() uses the compareTo() within the object class to sort the elements.
public class Main(){
import java.util.Collections;
public static void main(String args[]) {
ArrayList<City> citynames = new ArrayList<City>();
citynames.add("Hyderabad");
citynames.add("Karachi");
citynames.add("Abtabad");
citynames.add("AzadKashmir");
Collections.sort(citynames);
for(cityname : citynames)
System.out.println(cityname);
}
}
public class City implements Comparable{
private String name;
// Constructor
public City(String name){
this.name = name;
}
// Getter method
public String getName(){
return name;
}
// compareTo Method
public int compareTo(City other){
return name.compareTo(other.getName());
}
// Other methods may exist
}
For more information: https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html
I do not have the rights to comment yet, hence adding at as an answer.
You can google this easily and also lookup API for sorting collections.
Code in java 8
public class ArrayListSort {
public static String citynames[] = { "Hyderabad", "Karachi", "Abtabad", "AzadKashmir" };
public static void main(String args[]) {
Student stud1 = new Student("Alex", 20);
Student stud2 = new Student("Bryan", 21);
Student stud3 = new Student("Chris", 22);
Student stud4 = new Student("Dan", 23);
List<Student> students = new ArrayList<>(Arrays.asList(stud4, stud3, stud2, stud1));
Collections.sort(students);
for(Student stud: students){
System.out.println(stud.getName());
}
}
}
class Student implements Comparable<Student> {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return this.name;
}
public int getAge() {
return age;
}
#Override
public int compareTo(Student other) {
return this.name.compareTo(other.name);
}
}
Edit 1 to answer your comment, look up Comparable interface